![]() |
Jupyter at Bryn Mawr College |
|
|
Public notebooks: /services/public/dblank / CS245 Programming Languages / 2014-Fall / Assignments |
Due: Please bring two hardcopies to class Thursday, Sept 18, 2014
In the following problems, please define the function, and test it to make sure it works. I have provided a few examples, but you should test your function thoroughly to make sure that it works.
If it doesn't work in some instances, you should demonstrate where it fails. Otherwise, I will assume that you don't know that it fails.
remove-first
takes an item and a list, and removes the first occurence of item in the list.
(define remove-first
(lambda (item lyst)
(remove-first 'a '())
(remove-first 'a '(a))
(remove-first 'a '(a a a))
(remove-first 'a '(b c a))
(remove-first 'a '(a a b c a))
(remove-first 'a '(b c d))
remove-all
takes an item and a list and removes all occurences of item from the list.
(define remove-all
(lambda (item lyst)
(remove-all 'a '())
(remove-all 'a '(a))
(remove-all 'a '(a a a))
(remove-all 'a '(b a c a d a))
remove-nth
takes a number (1-based indexing), an item, and a list. It removes the nth instance of item in the list.
(define remove-nth
(lambda (n item lyst)
(remove-nth 1 'a '(a b a c a d))
(remove-nth 2 'a '(a b a c a d))
(remove-nth 3 'a '(a b a c a d))
substitute
takes an item to search for, an item to replace it, and a list. The function will find all instances of the old item and replace it with the new item.
(define substitute
(lambda (old new lyst)
(substitute 'old 'new '())
(substitute 'old 'new '(old))
(substitute 'old 'new '(old old a b c old))
substitute*
works just like substitute
but will find all instances no matter how deeply hidden they are inside sublists of the given list.
(define substitute*
(lambda (old new lyst)
(substitute* 'old 'new '((old)((((old apple bannan)) (a b c old d e))) old test ((word)) (old) old))
infix->prefix
will convert expressions given in infix notation to prefix notation. It gives an error if you attempt to operate on an empty list.
(define infix->prefix
(lambda (expr)
(cond
((null? expr) (error 'infix->prefix "Cannot process an empty expression"))
(infix->prefix 1)
(infix->prefix '(1 + 2))
(infix->prefix '((1 + 2) * (8 / 9)))
(infix->prefix '())
eval-infix
will evaluate infix expressions. It should handle +, *, /, and -. It should give an error otherwise.
(define eval-infix
(lambda (e)
(eval-infix 42)
(eval-infix '(1 + 1))
(eval-infix '(2 * (3 + 7)))
(eval-infix '((8 / 9) * (3 + 7)))
(eval-infix '(1 ^ 1))