...
Code Block |
---|
;; Given
(define-struct add (left right))
(define-struct sub (left right))
(define-struct mul (left right))
(define-struct div (left right))
;; an Arithmetic-Expression (AExp) is either:
;; - a number ;
;; - (make-add l r) where l,r are AExps;
;; - (make-sub l r) where l,r are AExps;
;; - (make-mul l r) where l,r are AExps; or
;; - (make-div l r) where l,r are AExps,
;; Remember that the define-struct function also automatically defines stucture recognizer functions, e.g.
;; add?, sub?, mul? and div?
;; Note: the structure recognizer function, number?, can be used to test if a value is a number.
|
Using this data definition, the arithmetic expression above corresponds to the structure ae1
defined by
...
- Develop the function
eval: AExp -> N
where(eval ae)
returns the number denoted by the expressionae
. For example,(eval ae1)
should return-51
, and(eval ae2)
should return16
.unmigrated-wiki-markup - \[Challenge\] Assume that our expression language includes many basic operations, not just the four supported by {{
AExp
}}. We would want a single representation for the application of a binary operator to arguments and use a separate data definition enumerating all of our operations. Rewrite the preceding data definitions, examples, and the function {{eval
}} using for this. As a further challenge, extend your data definition to accommodate unary operations including negation and absolute value as unary operators.
Files and Directories
The following are data definitions are idealized (for the sake of simplicity) representations of files and directories (folders). These definitions follow the Windows convention of attaching a name to a file. They also collapse the definition of the directory type into a clause in the definition of a file, which makes the set f definitions more compact but obfuscates how to write functions that process directories (instead of files). For this reason, none of the following exercises uses a directory as the primary input to a function.
...