Chapter 2: Getting Started
Expression
is Basic Syntax Structure
List
: zero or more items enclosed in parentheses
let-expressions
and let-variables
( let ( ( x 3 ) ( y 4 ) )
( let ( ( z ( + y ( * x 2 ) ) ) )
( + ( * 10 z ) z )
)
)
result is
110.
Lambda Expressions
( let ( ( square ( lambda (x) ( * x x ) ) ) ) ( list ( square 4 ) ( square 5 ) ) )
; example to demonstrate Scheme binding in specific situation.
;
; Pete Sanderson 13 Oct 1997
;
; Situation is: Binding for x at procedure application differs
; from binding for x at procedure definition.
; Question is: Which one prevails?
( let ((x 2))
( let ((fun (lambda (y) (* x y))))
( let ((x 3))
(fun 5) ; apply procedure; which x?
)
)
)
Lambda expression formal parameters
Conditional Expressions
Recursion
( define fact
(lambda (n)
(if (> n 0) (* n (fact (- n 1) ) ) 1 ) ) )
Begin expressions
(begin (display "howdy")
(newline)
(display "doody") )
Assignment
Example of acceptable use:
; Count of procedure applications.
; Be aware that count initialized only once.
(define count 0)
(define sigma
(lambda (n)
(set! count (+ count 1))
(if (> n 0)
(+ n (sigma (- n 1)))
0)))
Example of acceptable use:
; Maintenance of local state.
; Note binding of next in procedure definition.
(define take_a_number
(let ((next 0))
(lambda ()
(set! next (if (= next 99) 1 (+ next 1)))
next
)))