Lecture 16 Scheme - Elementary
These are my notes for SICP(Structure and Interpretation of Computer Programs). Hope they’ll be of some help to you.
Scheme is a Dialect of Lisp
Scheme Expressions
Scheme programs consist entirely of two types of expressions.
-
Atomic expressions (Atoms: primitive values that cannot be broken up into smaller parts)
-
Self-evaluating: numbers, booleans
3, 5.5, -10, #t, #f -
Symbols: names bound to values
+, modulo, list, x, foo, hello-world -
Combinations
(<operator> <operand1><operand2>...)- A combination is either a call expression or a special form expression.
Call expressions
Syntax:(<operator> <operand1><operand2>...)
A call expression applies a procedure to some arguments.
How to evaluate call expressions:
- Evaluate the operator to get a procedure.
- Evaluate all operands left to right to get the arguments.
- Apply the procedure to the arguments.
Call expressions include an operator and 0 or more operands in the parentheses.
>(quotient 10 2)
5
>(quotient (+ 8 7) 5)
3
>( + (* 3
(+ (* 2 4)
(+ 3 5))
(+ (- 10 7)
6)))
“quotient” names Scheme’s built-in integer division procedure(i.e function)
Combinations can span multiple lines (spacing doesn’t matter)
Special form expressions
Assigning values to names
The define special form assigns a value to a name: (define <name><expr>)
How to evaluate:
- Evaluate the given expression
- Bind the resulting value to the given name in the current frame.
- Return the names as a symbol.
scm> (define x (+ 3 4))
x
scm> x
7
scm> (define x (+ x 5))
x
scm> x
12
Control flow
The if special form allows us to evaluate an expression based on a condition: (if <predicate> <if-true><if-false>)
How to evaluate:
- Evaluate the
<predicate>. - If
<predicate>evaluates to anything but#f, evaluate<if-true>and return the value. Otherwise, evaluate<if-false>if provided and return the value.
#fis the only Falsy value in Scheme.
scm> (if #t 3 5)
3
scm> (if 0 (+ 1 0)(/ 1 0))
1
scm> (if (> 10 1)(* 5 6))
30
scm> (if (not 4) 1 (if #f 5 6))
6
Define functions with names
The second version of define is a shorthand for creating a function with a name: (define (<name> <param1><param2>...)<body>)
How to evaluate:
- Create a lambda procedure with the given parameters and body.
- Bind it to the given name in the current frame.
- Return the function name as a symbol.
scm> (define (square x) (* x x))
square
scm> square
(lambda (x) (*x x))
scm> (square 4)
16
scm> (square 10)
100
Lambda Expressions
The lambda special form returns a lambda procedure. (lambda (<param1><param2>...)<body>)
How to evaluate:
-
Create a lambda procedure with the given parameters and body.
The body expression is evaluated when the lambda procedure is applied.
-
Return the lambda procedure.
scm> (lambda (x) (* x x))
(lambda(x) (* x x) 5)
scm> (lambda(x) (* x x) 5)
25
scm> (define square (lambda (x) (* x x)))
square
scm> (square 4)
16
Two equivalent expressions:
(define (plus4 x) (+ x 4))
(define plus4 (lambda (x) (+ x 4)))
An operator can be a call expression too:
((lambda (x y z) (+ x y (square z))) 1 2 3)
Evaluates to the x + y + z^2 procedure.
Examples
Factorial
Recall the factorial function, which takes in an integer n and computes the product of all the integers from 1 to n.
Let’s try to write it in Scheme!
Scheme has no special form that allows for iteration, so we have to use recursion.
Ideas:
- Base case: if n is 0 or 1, just return 1
- Recursive case: Return the factorial of the previous number multiplied by n
- Use the if special form to capture our two cases:
if <pred><if-true><if-false>)
(define (fact x)
(if (<= x 1)
1
(* x (fact (- x 1)))))
Combinations can be split across multiple lines.
No explicit return statement!
Counting up
Let’s write a function count-up that takes in an integer n and prints all the integers from 1 to n.
Ideas:
- We need to keep track of the current element, k. k starts at 1.
- Since we have to use recursion, we can write a helper function to keep track of k.
- Print k at the beginning of every call and only make a recursive call to print more numbers if k is less than n.
(define (count-up n)
(define (counter k)
(print k)
(if (< k n)
(counter (+ k 1))))
(counter 1))
If there is more than one expression in the body, the function returns the value of the last expression.
Summary
- Scheme programs consist only of expressions, all of which can be categorized into either atomic expressions or combinations.
- Combinations are either call expressions or special form expressions, and they differ in the value of the operator.
- Scheme call expressions are evaluated just like they are in Python, but each special form has its own rules of evaluation.
- The special forms we learned today are
if,define, andlambda. - Writing some procedures in Scheme will require recursion; there is no special form for iteration.
本文介绍了Scheme语言的基本概念,包括原子表达式、组合表达式、特殊形式表达式等,并通过实例讲解了如何使用Scheme进行递归计算。

382

被折叠的 条评论
为什么被折叠?



