scheme
DESCRIPTION
scheme programmingTRANSCRIPT
![Page 1: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/1.jpg)
The Little Schemer, Ch 1
Concepts:
" atom" list" S−expression" car" cdr" cons" null?" atom?" eq?
![Page 2: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/2.jpg)
The Laws
The Law of car: The primitive car is defined only for non−empty lists
The Law of cdr: The primitive cdr is defined only for non−empty lists. The cdr of a any non−empty list
is always another list.
The Law of cons: The primitive cons always takes two arguments. The second argument must be a list.
The result is a list.
![Page 3: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/3.jpg)
The Laws, cont.
The Law of null?: The primitive null? Is defined only for lists.
The Law of eq?: The primitive eq? Takes two arguments. Each must be a non−numeric atom.
![Page 4: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/4.jpg)
The Little Schemer, Ch 2
(define lat?(lambda (l)(cond((null? l) #t)((atom? (car l)) (lat? (cdr l)))(else #f))))
![Page 5: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/5.jpg)
Functions
(define member?(lambda (a lat)(cond((null? lat) #f)(else (or (eq? a (car lat)) (member? a (cdr lat)))))))
![Page 6: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/6.jpg)
Functions
(define member?(lambda (a lat)(cond((null? lat) #f)((eq? a (car lat) #t)(else (member? a (cdr lat))))))
![Page 7: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/7.jpg)
Function Skeleton
(define function(lambda (parameters)(cond(q1 ans)(q2 ans) ...(else ans)
![Page 8: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/8.jpg)
The First Commandment
(preliminary)
Always ask null? As the first question in expressing an function.
![Page 9: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/9.jpg)
The Second Commandment
Use cons to build lists.
![Page 10: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/10.jpg)
The Third Commandment
When building a list, describe the first typical element, and then cons it onto the natural recursion.
![Page 11: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/11.jpg)
The C Method
main (char a[], char lat[][]) {for (int i = 0; i < alen(lat); i++) {
if (strcmp(a, lat[i])) return 1;
}return 0;
}
![Page 12: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/12.jpg)
Eating a Loaf of Bread
Are you done? Then Stop.Else, cut a slice and eat it.
Repeat.
(define ea t−a−loaf (lambda ( loaf) (cond ((null? loaf) ’done) (else ( begin (eat (car lo af ))
(eat −a−loaf (cdr loaf) ) ) )))
![Page 13: scheme](https://reader035.vdocuments.us/reader035/viewer/2022072001/563db7b8550346aa9a8d516f/html5/thumbnails/13.jpg)
The Little Schemer, Ch 3
(define rember (lambda (a lat) (cond ((null? lat) ’()) ((eq? a (car lat)) (cdr lat)) (else (cons (car lat) (rember a (cdr lat))))))