Symbolic Processing in Scheme II: class notes
(define (dec->bin x L)
(if (= x 0) L
(if (odd? x)
(dec->bin (/ x 2) (cons 1 L))
(dec->bin (/ x 2) (cons 0 L)))))
(dec->bin 26 '())
->(dec->bin 13 '(0))
->(dec->bin 6 '(1 0))
->(dec->bin 3 '(0 1 0))
->(dec->bin 1 '(1 0 1 0))
->(dec->bin 0 '(1 1 0 1 0))
-> '(1 1 0 1 0)
(define (bin->dec L x)
(if (null? L) x
(bin->dec (rest L) (+ (first L) (* 2 x)))))
(bin->dec '(1 0 0 1 0) 0)
->(bin->dec '(0 0 1 0) (+ 1 (* 2 0)))
->(bin->dec '(0 0 1 0) 1)
->(bin->dec '(0 1 0) (+ 0 (* 2 1)))
->(bin->dec '(0 1 0) 2)
->(bin->dec '(1 0) (+ 0 (* 2 2)))
->(bin->dec '(1 0) 4)
->(bin->dec '(0) (+ 1 (* 2 4)))
->(bin->dec '(0) 9)
->(bin->dec '() (+ 0 (* 2 9)))
->(bin->dec '() 18)
-> 18
(define (sq x) (* x x))
-> (map sq '(1 2 3 4 5))
-> '((sq 1) (sq 2) (sq 3) (sq 4) (sq 5))
-> '(1 4 9 16 25)
-> (apply + '(1 2 3 4 5))
-> (+ 1 2 3 4 5)
-> 15
-> (apply + (map sq '(1 2 3 4 5)))
-> (apply + '(1 4 9 16 25))
-> 55
(define (li x)
(string-append "" x ""))
(define (ul x)
(string-append
""
(apply string-append (map li x))
"
"))
(ul '("a" "b" "c"))
->(string-append "" (apply string-append (map li '("a" "b" "c"))) "
")
->(string-append "" (apply string-append (list (li "a") (li "b") (li "c"))) "
")
->(string-append "" (apply string-append (list "- a
" "- b
" "- c
"))) "
")
->(string-append "")
->""