0% found this document useful (0 votes)
841 views2 pages

CS61A Study Sheet

This document provides a cheat sheet for the CS 61A Scheme Midterm 1. It lists various Scheme functions and concepts. Some key functions and concepts mentioned include: - member? to check if an element is in a list - define to define functions - cond for conditional evaluation - lambda for anonymous functions - if for conditional evaluation - accumulate, keep, every for higher-order functions operating on lists - cons, car, cdr for working with pairs - append, map, filter for combining/transforming lists - let for binding values

Uploaded by

bdillpickle
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
841 views2 pages

CS61A Study Sheet

This document provides a cheat sheet for the CS 61A Scheme Midterm 1. It lists various Scheme functions and concepts. Some key functions and concepts mentioned include: - member? to check if an element is in a list - define to define functions - cond for conditional evaluation - lambda for anonymous functions - if for conditional evaluation - accumulate, keep, every for higher-order functions operating on lists - cons, car, cdr for working with pairs - append, map, filter for combining/transforming lists - let for binding values

Uploaded by

bdillpickle
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CS 61A Scheme Midterm 1 Cheat Sheet.

<word> is like 'word <sentence> is like '(a b c)

1. (member? <word> <sentence>) 2. (word <word> <word>) 3. (define (<name> <formal parameters>) <body>) [SICP 1.1.4] 4. ((equal? <word/sentence> <word/sentence>) <true procedure>) 5. ( cond ((<condition>) (<true procedure>)) ((<condition>) (<true procedure>)) ( else (<false procedure>) ) ) 6. ( if (<condition>) (<true procedure>) (<false procedure>) ) 7. (sentence <word/sentence> <word/sentence> ...) (se <word/sentence> <word/sentence> ...) 8. (butfirst <word/sentence>) 9. (butlast <word/sentence>) 10. (= <number> <number>) Utility Functions 11. ((empty? <word/sentence>) <true procedure>) 1. close-enough? 12. (count <word/sentence>) (define (close-enough? x y) 13. (last <word/sentence>) (< (abs (- x y)) 0.001)) 14. (<= <number> <number>) 15. (and (<condition>) (<condition>) ...) 2. sort [LEC 5] 16. (or (<condition>) (<condition>) ...) (define (sort sent) 17. (not <condition>) (if (empty? sent) 18. (abs <number>) `() 19. (sqrt <number>) (insert (first sent) 20. (sort (bf sent))))) 21. ( expt <number> <number> ) (define (insert num sent) 22. ( lambda (<formal parameters>) <procedure> ) [1.3.2] [lecture 3, (cond ((empty? sent) (se num sent)) special form] ((< num (first sent)) 23. (let ( (<variable> (<procedure>) (se num sent)) (<variable> (<procedure>) (...) (else ) (se (first sent) <body>) (insert num (bf sent)))))) ;(let ((<var> <exp>)) <body>) ---> ((lambda (<var>) <body>) <exp>) ;This simplifies to (1/2)N(N-1). [3.23] 24. (every <procedure> <sentence/word>) [high order function] 25. (number? <anything>) ;returns #t if the argument is a number 3. Open/Close Parenthesis 26. (floor <number>) ;#t if open parentheses equals to close parentheses 27. (ceiling <number>) (define (parenok? w) 28. (prime? <number>) (define (check v open) 29. (keep <predicate> <sentence/word >) [Lecture 7, high order (cond ((empty? v) (= open 0)) function] ((equal? (first v) '"(") (check (bf v) (+ open 1))) 30. (nth <number> <sentence>), (nth 0 '(1 2 3)) -> 1 [lecture 7] ((equal? (first v) '")") (check (bf v) (- open 1))) 31. (define <pair name> (cons <any> <any>)) (else (check (bf v) open)))) 32. (car <pair name>) (check w 0)) 33. (cdr <pair name>) 34. (list <any> <any>...) [SICP 2.2.1] 4. CONS, CAR, CDR [2.1.4] 35. (let* ( (<variable> (<procedure>) (define (cons x y) (<variable> (<procedure>) (define (dispatch m) ) (cond ((= m 0) x) <body>); like every let ((= m 1) y) 36. (append <list> <list> ...) ;return a list of elements (else (error "Argument not 0 or 1 -- CONS" m)))) 37. (map <procedure> <list>) dispatch) 38. (length <list>) (define (car z) (z 0)) 39. (filter <predicate> <list>) (define (cdr z) (z 1)) 40. (pair? <any>) 41. (atom? <anything not a list>) 5. 42. (remainder <number> <number>) LIST <-> SENTENCE 43. (quotient <number> <number>) map <-> every 44. (gcd <number> <number> <number> ...) null? <-> empty? 45. (random <number>) car <-> first 46. (set! <name> <new-value>) cdr <-> butfirst 47. (begin <exp1> <exp2> ... <expk>) filter <-> keep 48. ((eq? <thing1> <thing2>) <expression> ) ;test if it is the same as a pointer 6.1. MAP, the every for list [2.2.1] 49. (put <op> <type> <item>) 50. (get <op> <type>) [2.43] (define (map proc items) 51. (assoc <word> <list of pairs>) ; (assoc 'a ((b d) (e 3) (a 7))) -> (a (if (null? items) 7), #f is not found nil 52. (random n) ;returns a number between 0 and n (cons (proc (car items)) 53. (define-class (<class name> <instantiation variables>) (map proc (cdr items))))) 54. (method (<methodname> <args>) (map abs (list -10 2.5 -11.6 17)) 55. (usual <word>) ;call the immediate parent method (10 2.5 11.6 17) 56. (set! <var><var>) ; special form (map (lambda (x) (* x x)) 57. (set-car! <pair>) ; not special form (list 1 2 3 4)) 58. (set-cdr! <pair>) ; not special form (1 4 9 16) 59. (vector-ref <vector> <index>) 60. (vector-set! <vector> <index> <anything>) 6.2 MAP, add one to every numbers in the list[Lecture 10] 61. (vector-length <vector>) (define (inc x) (+ x 1)) 62. (stream-car <stream>) 63. (stream-cdr <stream>) (define (inc-deep-list DL)

64. (stream-null? <stream>) 65. (cons-stream <procedure> <procedure>) ;special form where the 2nd procedure is delayed 66. (cons-stream <things> <stream>) ;a special form 67. (show-stream <stream>) or (ss <stream> <num>) 68. (interleave <stream> <stream>) ;not a special form, may go into infinite loop 69. (append-stream <stream> <stream>) 70. (stream-map <procedure> <thing> <steram>) 71. (stream-filter 72. (stream-append <stream> <stream>) ;the 1st stream has to be finite 73. (delay <procedure>) ;special form, same thing as (lambda () <exp>) 74. (amb 75. (require 76. (try-again

(map (lambda (x) (if (list? x) (inc-deep-list x) (inc x))) DL)) (inc-deep-list '(0 (1 2 3) 4)) (1 (2 3 4) 5) 6.3 Map, general function ... 7. FOR-LOOP, HW 2a (define (cont-frac n d k) (define (helper i) (if (> i k) 0 (/ (n i) (+ (d i) (helper (+ i 1)))))) (helper 1)) 8. INSERT-AFTER a word into a list, error if the item is not found (define (insert-after item mark lst) (if (equal? (car lst) mark) (cons (car lst) (cons item (cdr lst))) ;(append (list car lst) item) (cdr lst)) (cons (car lst) (insert-after item mark (cdr lst))) );if );define
9. LIST (define (list? L) (cond ((null? L) #t) ((pair? L) (list? (cdr L))) (else #f)))

(define (accumulate combiner null-value term a next b) (define (iter a result) (if (> a b) result (iter (next a) (combiner (term a) result)))) (iter a null-value)) ;; sum and product (define (sum term a next b) (accumulate + 0 term a next b)) (define (product term a next b) (accumulate * 1 term a next b)) (define (accumulate op null-val sent) (if (empty? sent) null-val (op (first sent) (accumulate op null-val (bf sent))))) > (accumulate + 0 `(2 5 3 4 1)) 15 >(accumulate word `d `(a b c)) abcd 3. > EVERY

(define (every f sent) (if (empty? sent) '() (se (f (first sent)) (every f (butfirst sent)) )))

(every (lambda (x) (word x s)) (tricky hobbits)) (trickys hobbitss) > (every (lambda (x) (+ x 1)) 1234) (2 3 4 5) (every (lambda (x) (word x 's)) 'tricky) (ts rs is cs ks ys) Special Forms 1. Define 2. If 3. Cond 4. Lambda Iterative Example (define (exp a b) (if (= b 0) 1 (* a (exp a (- b 1))))) Recursive Example define (exp a b) (define (helper count result) (if (= count b) result (helper (+ count 1) (* a result)))) (helper 0 1))

Note for midterm High Order Functions=every/keep/accumulate 1.1. KEEP > (keep (lambda (x) (= x 1)) (1 2 1 2)) (1 1) (define (keep pred sent) (cond ((empty? sent) ()) ((pred (first sent)) (se (first sent) (keep pred (bf sent)))) (else (keep pred (bf sent))))) ;when parameter is sent (define (keep proc sent) (if (empty? sent) '() (if (proc (first sent)) (se (first sent) (keep proc (bf sent))) (keep proc (bf sent))))) (accumulate combiner null-value term a next b) > (accumulate * 1 (lambda (x) x) 1 (lambda (x) (+ x 1)) 5) 120 > (accumulate se `(foo) (lambda (x) x) 1 (lambda (x) (+ x 1)) 5) (1 2 3 4 5 foo) 1.2 KEEP > (keep (lambda (x) (or (even? x) (equal? x 1))) (1 2 3 4)) (1 2 4) > (keep (lambda (x) (member? x (x y z))) syzygy) yzyy 2. ACCUMULATE ;; recursive form (define (accumulate combiner null-value term a next b) (if (> a b) null-value (combiner (term a) (accumulate combiner null-value term (next a) next b)))) ;; iterative form

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy