CS 61A Scheme
Scheme
symbols
booleans
t true
f false
strings
‘hello world!
numbers
no need to say
calculations
(+ 1 2) 3 (the same: +-*/<>=)
(modulo 35 4) 3
logic
- (if
[if-false])
Scheme:
1 | (if (> x 3) |
python:
1 | if x > 3: |
- cond
1 | (cond |
- and & or
and (and x1 x2 …)
or (or x1 x2 …)
symbols
booleans
t true
f false
strings
‘hello world!
numbers
no need to say
calculations
(+ 1 2) 3 (the same: +-*/<>=)
(modulo 35 4) 3
logic
- (if
[if-false])
Scheme:
- begin: bring few expressions together
1 | (cond ((> x 10) (begin (print 'big) (print 'guy))) |
defination
parameter:
- (define
)
(define x 2)
functions(precedure in Scheme):
(define (
(define (add_four x) x+4)
(lambda (
) ) (temporate binding) let() expression
(define c (let (a 3) (b (+ 2 2)))(sqrt (+ (a a) ( b b))))
link lists
• cons: Two-argument procedure that creates a linked list
• car: Procedure that returns the first element of a list
• cdr: Procedure that returns the rest of a list
• nil: The empty list
list: join the lists/elements together
1 | >(cons 1 (cons 2 nil)) |
quotation marks
quotations (‘a)
cons(1, a) error!
cons(1, ‘a) (1, a)
quasiquotations (`a)
‘(a ,(+ b 1)) => (a (unquote (+ b 1))
`(a ,(+ b 1)) => (a 5)
for generating scheme programs
unquote (,a)
programs are languages
scheme sentences are lists
scm> (list ‘quotient 10 2)
(quotient 10 2)
scm> (eval (list ‘quotient 10 2))
5
for example:to generate while statement for scheme
python:
1 | x = 2 |
scheme:
1 | (begin |
