- Lose the fear of parentheses
- There are a number of interesting Lisp-derived languages (such as Clojure), and we want to get over the syntax barrier once and for all

- Metaprogramming
- Ruby lets you manipulate the object structure, but Scheme lets you manipulate all of your code

- Continuations
- An advanced control flow construct that can be useful for web and UI applications

- Parentheses syntax:
*Everything*is a list`(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 execute`1`

- Dynamically typed
(define x 1) (set! x '(1 2 3)) ; x can hold an int or a list

- Functional
(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`))- The
`else`

is optional - If you like, use
`[ ]`

for the length 2 lists(cond [(< x 0) -1] [(= x 0) 0] [else 1])

- The
`(let ([`

`var1 expr1`]`[``var2 expr2`]...)`expr1 expr2`)*...*expr_{n}- Defines local variables
- Evaluates all expressions (for side effects)
- Yields value of last expression

`(λ (args) expr)`

(λ (x y) (* 0.5 (+ x y)))

- If your keyboard doesn't have a λ key, use
`lambda`

instead, or try Ctrl+\- In Linux, add this to your ~/.XCompose; see here for
background . This works inside the
`racket`

program.<Multi_key> <g> <l> : "λ"

- In Linux, add this to your ~/.XCompose; see here for
background . This works inside the
- Like
{ | 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

- Useful for passing to higher-order functions
(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))

- Added to current scope
- Can use
`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

- Very different from C++ linked lists. No iterators
- Three primitives:
`first`

,`rest`

,`cons`

- A list is either
`empty`

or has a head and tail(define lst '(1 4 9)) (first lst) ; 1 (rest lst) ; '(4 9) (rest (rest (rest lst))) ; '() or empty

- Use
`cons`

to build lists(cons 0 lst) ; '(0 1 4 9) (cons 0 (cons 1 (cons 4 (cons 9 empty)))) ; the same list

- Use recursion for list functions
(define (sum lst) (if (empty? lst) 0 (+ (first lst) (sum (rest lst)))))

- Use
`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- Works because lists are immutable
- Did you recognize these slides?

- You work with a buddy
- One of you (the coder) codes, the other (the scribe) types up answers
- Switch roles each lab
- The scribe submits lab work to the Sakai site

- Install Racket from http://racket-lang.org. (Racket is a Scheme dialect with a pretty nice environment.)
- Start the interactive Dr. Racket console by
launching
`drracket`

or`drracket.exe`

. - Work through steps 2 and 3 of http://docs.racket-lang.org/quick/index.html
(which is also inside the
`doc/quick`

directory of your Racket installation) - As instructed, try the F1 key. How do you get the circle and square aligned at the top?

- Implement a function
`circles`

so that`(circles n)`

draws a row of n circles, like this: ooooooooooHint:

`(hc-append`

`(circle 10) (circles ...))`

- Work through step 5 of the tutorial.
How can you tell that the nested

`define`

binding in the first part of step 5 is really local? - What happens when you use
`let`

instead of`let*`

in the last part of step 5? Why?

- Work through step 6 of the tutorial. Then improve on the
`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?

- Work through step 7. The text makes a big deal out of the fact that the λ expression is “lexically scoped”. What term do we normally use to describe such a function?
- Work through step 8. Look at the nifty list that you get from
`(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? - Having a list is so much more useful. You can now make a vertical drawing of them, with one line of code. How?