
(op arg1
arg2...)
(+ 3 4) ; 3 + 4 (+ 3 4 7 10) ; 3 + 4 + 7 + 10 (* 3 (+ 4 5)) ; 3 * 4 + 5 (if #t 3 4) ; 3—#t is true (define (fac x) (if (<= x 1) 1 (* x (fac (- x 1))))) (fac 10) ; 3628800 '(1 2 3) ; The list(1 2 3)—we don't want to execute1
(define x 1) (set! x '(1 2 3)) ; x can hold an int or a list
(map (λ (x) (* x x)) '(1 2 3)) ; (1 4 9)
(if cond expr1 expr2)(and expr1 expr2 ...), (or expr1
expr2 ...) (cond (cond1 expr1)(cond2
expr2)...(else expr))
else is optional[ ] for the length 2 lists
(cond [(< x 0) -1] [(= x 0) 0] [else 1])
(let ([var1 expr1] [var2
expr2]...)
expr1 expr2 ... exprn)
(λ (args) expr)
(λ (x y) (* 0.5 (+ x y)))
lambda
instead, or try Ctrl+\
racket program.
<Multi_key> <g> <l> : "λ"
{ | x y | 0.5 * (x + y) } # Ruby
{ (x: Double, y: Double) => 0.5 * (x + y) } // Scala
function(x, y) { return 0.5 * (x + y) } // JavaScript
(map (λ (x) (* x x)) '(1 2 3)) ; (1 4 9)
(define var expr)(define (fun arg1 arg2 ...) expr)
(define x 1) (define (square x) (* x x))
let instead
(let ((x 1)
(square (λ (x) (* x x))))
Rest of program using x and square)
Actually, not quite—need a different form of let for
recursive functions
first,
rest, consempty or has a head and tail
(define lst '(1 4 9)) (first lst) ; 1 (rest lst) ; '(4 9) (rest (rest (rest lst))) ; '() or empty
cons to build lists
(cons 0 lst) ; '(0 1 4 9) (cons 0 (cons 1 (cons 4 (cons 9 empty)))) ; the same list
(define (sum lst)
(if (empty? lst)
0
(+ (first lst) (sum (rest lst)))))
cons to recursively build lists
(define (squares n)
(if (= n 0)
empty
(cons (* n n) (squares (- n 1)))))
lst.rest doesn't make a new list—it is a reference to
the second cell


drracket or drracket.exe.doc/quick directory of your Racket
installation)circles so that (circles
n) draws a row of n circles, like this: oooooooooo
Hint: (hc-append (circle 10) (circles ...))
How can you tell that the nested define binding in the
first part of step 5 is really local?
let instead of let*
in the last part of step 5? Why?series function so that it can make a series of arbitrary
sizes, i.e.
(series circle '(5 10 20))
has the same effect as the original version, but we can also do
(series circle '(5 10 20 10 5))
What is the code of your function?
(rainbow (square 5)). Look carefully—it's a quoted
list of shapes, not a painted shape. How do you change
series so that you get a list of shapes instead of a combined
shape?