Call Expression
Scheme programs consist of expressions, which can be:
- Primitive expressions: 2, 3.3, true, +, quotient, …
- Combinations: (quotient 10 2), (not true), …
Numbers are self-evaluating; symbols are bound to values
Call expressions include an operator and 0 or more operands in parentheses

question mark:
> (integer? 2.2)
#f
> (integer? 2)
#tSpecial Forms
A combination that is not a call expression is a special form.
ifexpression:(if <predicate> <consequent> <alternative>)- Evaluate the predicate expression
- Evaluate either the consequent or alternative
andandor:(and <e1> <e2> ...)(or <e1> <e2> ...)
It also have short circuit property.
- Binding symbols:
(define <symbol> <expression>)
- New procedure:
(define (<symbol> <formal parameters>) <body>)
(define (abs x)
(if (< x 0)
(- x)
x))Scheme’s environment system is same to Python.
Example:
(define (average x y)
(/ (+ x y) 2))
(define (sqrt x)
(define (update guess)
(if (= (square guess) x)
guess
(update (average guess (/ x guess)))))
(update 1))So we can create a recursive procedure in Scheme.
condspecial form:(cond (<condition> <consequent>)(<condition> <consequent>)...(else <alternative>)- Only one will be chosen.
- It does have a value that can be printed etc.
beginspecial form: combine multiple expressions into one.(begin <subexpression> <subexpresion> ...)- All of them will be evaluated and called.
- Return the value of last expression.
letspecial form: bind symbols to values temporarily(just for one expression)(let ((<symbol> <expression>) (<symbol> <expression>)...<body>))
Lambda Expressions
Lambda expressions evaluate to anonymous procedure.
(lambda (<formal-parameters>) <body>)
Example: Two equivalent expression to define a plus4 procedure.
(define (plus4 x) (+ x 4))
(define plus4 (lambda (x) (+ x 4)))(Shorthand)
An operator can be a call expression, too:
((lambda (x y z)
(+ x y (square z)))
1 2 3)We can also repeat:
(define (f x total)
(if (< x 10)
(f (+ x 2) (+ total (* x x)))
total
)
)define doesn’t return the function, but lambda does.
mu Expressions
It is not standard in Scheme but appears in other variants of Lisp.
(mu ([param] ...) <body> ...)Creates a new mu procedure with params as its parameters and the body expressions as its body. When the procedure this form creates is called, the call frame will extend the environment the mu is called in.
Lists
Linked list.
cons: Two-argument procedure that creates a linked list- The second one must be a linked list, so that we connect the first with the existed argument linked list. So it actually create linked list from the end.
- Or it is an ill list, and will be presented as Pair:
(cons 1 '(2 3)) ; OK:return (1 2 3)
(cons '(2 3) 1) ; :return ((2 3) . 1),not a well-formed list.car: Procedure that returns the first element of a listcdr: Procedure that returns the rest of a listnil: The empty listlist: Create a list, shorthand of(cons ... (cons ... (...)))
Macros
Macros allow us to define new special forms in the language.
A macro is an operation performed on the source code of a program before evaluation.
define-macro Special Form
(define-macro (twice expr)
(list 'begin expr expr))
> (twice (print 2))
; 2
; 2Evaluation procedure of a macro call expression:
- Evaluate the operator sub-expression, which evaluates to a macro
- Call the macro procedure on the operand expressions without evaluating them first
- Evaluate the expression returned from the macro procedure
So a macro take in expressions and return expressions instead of taking in values and returning values.
For Macro
(define-macro (for sym vals expr)
(list 'map (list 'lambda (list sym) expr) vals)
)