Make sure your code works in MIT Scheme, as I will be executing it in that interpreter. Check out the Scheme/Lisp Resources page to get started with MIT Scheme.
> (build-list 5) => (5 4 3 2 1)
Write a Scheme function called build-list-increasing that builds
and returns a list of increasing numbers. For example,
> (build-list-increasing 5) => (1 2 3 4 5)
My implementation of build-list-increasing is about 5 lines.
Don't use the built-in reverse function in your solution.
We will represent an a-list by a list of two item lists. The first item in each sublist is the key, and the second item is the value associated with this key. Consider the following list. It associates the symbol RICHIE with SUCCESSFUL-DIRECTOR, FONZIE with COOL and RALPH-MALPH with DORK.
'((RICHIE SUCCESSFUL-DIRECTOR) (FONZIE COOL) (RALPH-MALPH DORK))
Below are some functions for adding associations to an a-list and looking
up the value associated with a particular key.
; alist-add - return alist augmented with key/val
(define (alist-add key val alist)
(cons (list key val) alist))
; alist-first-key - return first key in alist x
(define (alist-first-key x) (car (car x)))
; alist-first-val - return first value in alist x
(define (alist-first-val x) (car (cdr (car x))))
; alist-find - return first value associated with key in alist
(define (alist-find key alist)
(if (null? alist) ()
(if (eqv? key (alist-first-key alist)) (alist-first-val alist)
(alist-find key (cdr alist)))))
Here is some code to use the above code.
> (define happy-days
(alist-add 'RICHIE
'SUCCESSFUL-DIRECTOR
(alist-add 'FONZIE
'COOL
(alist-add 'RALPH-MALPH
'DORK
()))))
> (alist-find 'RALPH-MALPH happy-days) => 'DORK
> (alist-find 'MR-CUNNINGHAM happy-days) => ()
My implementation is about 6 lines long.
> (alist-remove 'fonzie happy-days)
=> ((richie successful-director) (ralph-malph dork))
My implementation is about 5 lines long.
> (alist-keys 22 '((a 11) (b 22) (c 22))) => (b c)
My implementation is about 5 lines long.
> (alist-map '((a 1) (b 2) (c 3)) (lambda (k v) v)) => (1 2 3)
> (alist-map '((a 1) (b 2) (c 3)) (lambda (k v) k)) => (a b c)
My implementation is about 4 lines long.