The answer expression expression is the expression evaluated if a given question expression is evaluated as true.
(cond [(> 3 3) "more"]
[(< 3 3) "less"]
[else "same"])
In this example "more", "less" and "same" are all answer expressions
The question expression is the expression evaluated to determine if the answer expression should be evaluated. Question expressions are always evaluated to either true or false.
(cond [(> 3 3) "more"]
[(< 3 3) "less"]
[else "same"])
In this example,
(< 3 3)and(> 3 3)are question expressions.
An if statement is flow control statement that chooses a command depending on a expression.
(define c 10)
(if (< c 0)
"positive"
"negative")
In this example
(if (< c 0) "positive" "negative")is the if statement
The argument is the value(s) which result from evaluating the operands in the function call.
(bulb (string-append "r" "ed"))
In the context of bulb it's argument is "red" as
(string-append "r" "ed")evaluates to "red".
Atomic distinct describes a type that represents itself.
(@htdd Example)
;; Example is "example-1"
(@dd-template-rules atomic-distinct)
(define (fn-for-example e) (...))
"red"falseemptyA constant definition is the definition of a constant value
(define BALL (circle BALL-RADIUS "solid" "white"))
A constant name is the name given to a constant value
(define BALL (circle BALL-RADIUS "solid" "white"))
(define MTS (rectangle WIDTH HEIGHT "solid" "green"))
(place-image BALL 10 10 MTS)
In this example both
BALLandMTSare both constant names
A data example is an example of the type of data a data definition represents
(@htdd Time)
;; Time is Natural
;; interp. number of clock ticks since start of game
(define START-TIME 0)
(define OLD-TIME 1000)
(@dd-template-rules atomic-non-distinct)
(define (fn-for-time t)
(... t))
In this example both
(define START-TIME 0)and(define OLD-TIME 1000)are data examples ofTime
DD Template Rules are a series of rules that are used as a guide to create Data Definitions. In the beginning of every Data Definition you can specify which rules to use so that if someone else is reading your definition, they can quickly understand it and use it in their code.
(@htdd Time)
;; Time is Natural
;; interp. number of clock ticks since start of game
(define START-TIME 0)
(define OLD-TIME 1000)
(@dd-template-rules atomic-non-distinct)
(define (fn-for-time t)
(... t))
In this example
atomic-non-distinctis a dd Template Rule
(@htdd ListOfBall)
;; ListOfBall is one of:
;; - empty
;; - (cons Ball ListOfBall)
;; interp. a list of balls
(define LOB-1 empty)
(define LOB-2 (cons (make-ball (/ WIDTH 2) (/ HEIGHT 2) 4 -3) empty))
(define LOB-3 (cons (make-ball 1 2 4 -3) (cons (make-ball 3 5 4 -3) empty)))
(@dd-template-rules one-of
atomic-distinct
compound
ref
self-ref)
(define (fn-for-lob lob)
(cond
[(empty? lob) (...)]
[else (... (fn-for-ball (first lob))
(fn-for-lob (rest lob)))]))
In this example
atomic-distinct,compound,ref, andself-refare all dd template rules
Expressions are anything that can be evaluated to a value. Expressions always take the form of either (<primitive> <expression>...) or just a single <value>.
(+ 2 3)(* 4 5)(sqrt (+ (sqr 3) (sqr 4)))1#i1.4142135623730951The Function Body is the executed portion of a function
(define (convert-fn c)
(* 9/5 c))
In this example
(* 9/5 c)is the function-body ofconvert-fn.
A function call expression is an expression that represents calling a function with some arguments
(circle 10 "solid" "red")
A function definition is the full definition of a function which includes the function body.
(define (convert-fn c)
(* 9/5 c))
A function example is the tests written before a function (usually check-expect's).
(@htdf preference?)
(@signature Rating -> Boolean)
;; produces true if a rating reflects a clear opinion
(check-expect (preference? "n/a") false)
(check-expect (preference? 1) true)
(check-expect (preference? 3) false)
(check-expect (preference? 5) true)
; (define (preference? rating) true) ; this is the stub
(@template-origin Rating)
(@template
(define (preference? r)
(cond [(and (string? r) (string=? r "n/a")) (...)]
[else
(... r)])))
(define (preference? r)
(cond [(and (string? r) (string=? r "n/a")) false]
[else (not (= r 3))]))
In this example
(check-expect (preference? "n/a") false),(check-expect (preference? 1) true),(check-expect (preference? 3) false), and(check-expect (preference? 5) true)are all function examples.
A function name is a name given to a function definition or function call expression.
(define (area c)
(* 9/5 c))
In this example
areais the function name
A metadata annotation is a piece of information added to help the programmer use the function or data structure. Usually, this is used for the beginning of Data Definitions or Function Definitions. Metadata annotations
(@problem 1)
(@htdf main)
(@signature Ball -> Ball)
;; start the game, call with (main B1)
;; <no tests for main functions>
(@template-origin htdw-main)
(define (main b)
(big-bang b
(on-draw render-ball) ;Ball -> Image
(on-tick next-ball) ;Ball -> Ball
(on-mouse handle-mouse)));Ball Integer Integer MouseEvent -> Ball
In this example
(@problem 1),(@htdf main),(@signature Ball -> Ball), and(@template-origin htdw-main)are all metadata annotations.
The expressions following the function name in a function call expression are called operands.
(define (bulb c)
(color 40 "solid" c))
(bulb (string-append "r" "ed"))
In this example
(string-append "r" "ed")is the operand ofbulband"r"and"edare the operands ofstring-append.
A parameter is an identifier (or name) used in a function declaration that represents the input values.
(define (bulb c)
(color 40 "solid" c))
(bulb (string-append "r" "ed"))
In this example
cis the parameter of bulb.
Structure definitions are constructs that provide a way for storing data with different pieces and naming these individual pieces. This is helpful when you want to store different types of data but group them together for ease of use.
(struct book (title author pages))
A template describes the basic structure or backbone of the function independent of its details.
(@htdd DinnerOrder)
;; DinnerOrder is one of:
;; - "No dinner"
;; - "Chicken"
;; - "Pasta"
;; interp. "No dinner" means the passenger does not want dinner,
;; the other values are dinner options
(@dd-template-rules one-of
atomic-distinct
atomic-distinct
atomic-distinct)
(define (fn-for-dinner d)
(cond [ (string=? d "No dinner") (...) ]
[ (string=? d "Chicken") (...) ]
[ (string=? d "Pasta") (...) ]))
In this example the entire function definition
fn-for-dinnerwould be the template for functions that operate onDinnerOrder.
A type comment is used to describe the usage of a specific type for a parameter to a function or for a variable definition.
(@htdd DinnerOrder)
;; DinnerOrder is one of:
;; - "No dinner"
;; - "Chicken"
;; - "Pasta"
;; interp. "No dinner" means the passenger does not want dinner,
;; the other values are dinner options
(@dd-template-rules one-of
atomic-distinct
atomic-distinct
atomic-distinct)
(define (fn-for-dinner d)
(cond [ (string=? d "No dinner") (...) ]
[ (string=? d "Chicken") (...) ]
[ (string=? d "Pasta") (...) ]))
In this example
DinnerOrder is one of:,- "No dinner- "Chicken"- "Pasta"all make up the type comment forDinnerOrder
The type name is the name / identifier given to a specific type
(@htdd DinnerOrder)
;; DinnerOrder is one of:
;; - "No dinner"
;; - "Chicken"
;; - "Pasta"
;; interp. "No dinner" means the passenger does not want dinner,
;; the other values are dinner options
(@dd-template-rules one-of
atomic-distinct
atomic-distinct
atomic-distinct)
(define (fn-for-dinner d)
(cond [ (string=? d "No dinner") (...) ]
[ (string=? d "Chicken") (...) ]
[ (string=? d "Pasta") (...) ]))
In this example
DinnerOrderwould be the type name