160 lines
2.8 KiB
Plaintext
160 lines
2.8 KiB
Plaintext
Lsip - Common Lisp
|
|
\ - Scheme
|
|
|
|
The structure of a SCheme program:
|
|
-all programs and data are expressions
|
|
|
|
expression -> atom|list
|
|
atrom -> nr | string | id | char | bool
|
|
list -> (expr_seg)
|
|
expr_seg -> expression | expression expr-seg
|
|
|
|
Interpreter : "read-eval-print" loop
|
|
> 1
|
|
1
|
|
|
|
C: f(x,y) profix
|
|
a+b infix
|
|
|
|
Scheme everything is profix
|
|
(Cambridgo polish notation)
|
|
(f x y)
|
|
(x a b)
|
|
|
|
> (+ 2 3)
|
|
5
|
|
|
|
Evaluation
|
|
-Constant atoms - eval to themselvs
|
|
42
|
|
3.14
|
|
"hello"
|
|
#/a -character 'a'
|
|
#/t -boad.value true
|
|
#/f - -- -- false
|
|
|
|
-Identifiers - eval to the value bound to them
|
|
> (define a 7)
|
|
a -> 7
|
|
> a
|
|
> +
|
|
#<procedure +> +-> -----
|
|
|
|
-Lists - eval to the function calls
|
|
-First elem. must eval. to a fct.
|
|
-recursivaly eval. each arg.
|
|
-apply the fct. to evaluated args.
|
|
> (-7 1)
|
|
|
|
> (*(+ 2 3) (/ 6 2))
|
|
| 5 3
|
|
#<procedure*>
|
|
15
|
|
|
|
> (+)
|
|
0
|
|
> (+ 2)
|
|
2
|
|
> (+ 2 3 4)
|
|
9
|
|
> (/ 20 5 2)
|
|
2
|
|
|
|
>(1 2 3)
|
|
|
|
Error: attempt to apply non-procedure 1
|
|
|
|
Preventing evaluation
|
|
-quote
|
|
> (quote (1 2 3))
|
|
(1 2 3)
|
|
Alternative notation:
|
|
> '(1 2 3)
|
|
(1 2 3)
|
|
|
|
(define a 7)
|
|
a => 7
|
|
'a => A
|
|
(+ 2 3) => 5
|
|
'(+ 2 3) = > (+ 2 3)
|
|
((+ 2 3)) => Error: attampt to apply non-pror. 5
|
|
5
|
|
|
|
(list 'her (+ 2 1) "sons")
|
|
=> (her 3 "sons")
|
|
|
|
'(her (+ 2 1) "sons") =>
|
|
=>(her (+ 2 1) "sons")
|
|
|
|
(list '(+ 2 1) (+ 2 1)) =>
|
|
=> ((+ 2 1) 3)
|
|
|
|
Special Forms
|
|
-Have special rules about eval.
|
|
(quote (1 2 3)) ; does not eval its arg.
|
|
(define a 7) ; binds a to 7, does not eval. a
|
|
-Petite Chez Scheme
|
|
|
|
List operations
|
|
-Cons - returns a list built from head (1st elm) and tail (list of other the list)
|
|
(cons 'a '(b c d)) => (a b c d)
|
|
(cons 'a '()) => (a)
|
|
(cons '(a b) '(c d)) => ((a b) c d)
|
|
(cons 'a (const 'b '() =>
|
|
| |
|
|
a (b)
|
|
-car -returns the head of a list
|
|
(car '(a b c d)) => a
|
|
(car '(a)) => a
|
|
(car '((a b) c d)) => (a b)
|
|
|
|
-cdr -returns the list without 1st elem.(tail)
|
|
(cdr '(a b c d)) => (b c d)
|
|
(cdr '(a b)) => (b)
|
|
(cdr '(a)) => ()
|
|
(cdr '(a(bc))) => ((bc))
|
|
|
|
-null? -returns #/t if list is empty
|
|
-list -returns a list built from its args.
|
|
(list 'a 'b 'c) => (a b c)
|
|
(list 'a) => (a)
|
|
(list '(a b c)) => ((a b c))
|
|
(list '(a b) 'c) => ((a b) c)
|
|
|
|
why is list needed?
|
|
-> list of values of vars
|
|
(define x 1)
|
|
(define y 2)
|
|
(x y) => Error...
|
|
'(x y) => (x y)
|
|
(list x y) => (1 2)
|
|
| |
|
|
1 2
|
|
|
|
(const x (list y)) => (1 2)
|
|
|
|
-length -returns the length of a list
|
|
(length '(1 3 5 7)) => 4
|
|
(length '((a b) c)) => 2
|
|
|
|
-reverse -returns the list reversed
|
|
(reverse '(1 3 5)) => (5 3 1)
|
|
(reverse '((a b) c)) => (c (a b))
|
|
|
|
-append -returns the concatintation fo the list args
|
|
(append '(1 3)) '(7 9 11) => (1 3 7 9 11)
|
|
(append '(a) '()) => (a)
|
|
(append '(a b) '((c d) e) => (a b (c d) e)
|
|
|
|
(const '(a b) '(c d)) => ((a b) c d)
|
|
(list '(a b) '(c d)) => ((a b) (c d))
|
|
(append '(a b) '(c d)) => (a b c d)
|
|
|
|
Type Predicates:
|
|
(boolean? x)
|
|
char?
|
|
string?
|
|
number?
|
|
list?
|
|
procedure?
|