![]() |
Jupyter at Bryn Mawr College |
|
|
Public notebooks: /services/public/dblank / CS245 Programming Languages / 2014-Fall / Notes |
(define in->pre
(lambda (exp)
(cond
((atom? exp) exp)
(else (list (cadr exp)
(in->pre (car exp))
(in->pre (caddr exp)))))))
(in->pre '(1 + (2 * 6)))
(define eval-infix
(lambda (e)
(eval (in->pre e))))
(eval-infix '(1 + 1))
(define my-list?
(lambda (thing)
(cond
((null? thing) #t)
((atom? thing) #f)
(else (my-list? (cdr thing))))))
(my-list? '())
(my-list? '(1))
(my-list? '(1 . 2))
(my-list? 1)
(my-list? '(1 2 3 4 5 6 . 9))
(define mylength
(lambda (list)
(cond
((null? list) 0)
(else (+ 1 (mylength (cdr list)))))))
(mylength '())
(mylength '(1))
(mylength '(1 2 3))
(define divide-and-leftover
(lambda (m n)
(d-and-l m n 0)))
(define d-and-l
(lambda (m n count)
(cond
((> n m) (list count m))
(else (d-and-l (- m n) n (+ 1 count))))))
(divide-and-leftover 8 4)
(divide-and-leftover 8 5)
(divide-and-leftover 100 5)
(divide-and-leftover 100 4)
(divide-and-leftover 100 3)
;; Define a procedure set? that determines if it is
;; composed of unique atoms.
(define set?
(define my-sum
(my-sum '())
(my-sum '(12))
(my-sum '(12 23))
(define exactly-two?
(lambda (a list)
(exactly-two? 'a '())
(exactly-two? 'a '(a a))
(exactly-two? 'a '(a b c d))
(exactly-two? 'a '(b c a d e a))
(define exactly-two-all?
(lambda (list)
(other list list)))
(define other
(lambda (l1 l2)
(cond
((null? l1) #t)
((= (count (car l1) l2) 2) (other (cdr l1) l2))
(else #f))))
(define count
(lambda (a list)
(cond
((null? list) 0)
((eq? (car list) a) (+ 1 (count a (cdr list))))
(else (count a (cdr list))))))
(exactly-two-all? '(a a b))
(exactly-two-all? '(b a a b))
(exactly-two-all? '(b a a b))
;; Write a procedure that returns the number of
;; parens in a Scheme list.
(define count-parens
(lambda (item)
(cond)))
;; In: (count-parens 1)
;; Out: 0
;; In: (count-parens '())
;; Out: 2
;; In: (count-parens '((a) (b)))
;; Out: 6
;; Hint: it isn't really counting parens!