Lab #2

CS245: Programming Languages

Blank, Fall 2016

Due: Thursday, Sept 15, 2016

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. That means, add more cells for each problem.

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.

1. Problem: remove-first

remove-first takes an item and a list, and removes the first occurrence of item in the list. Finish the code in the next cell, and use the following cells to test that it works. Add additional cells to test more variations, or document where it doesn't work correctly.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
(remove-first 'a '())
In [ ]:
(remove-first 'a '(a))
In [ ]:
(remove-first 'a '(a a a))
In [ ]:
(remove-first 'a '(b c a))
In [ ]:
(remove-first 'a '(a a b c a))
In [ ]:
(remove-first 'a '(b c d))

2. Problem: remove-all

remove-all takes an item and a list and removes all occurrences of item from the list.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
(remove-all 'a '())
In [ ]:
(remove-all 'a '(a))
In [ ]:
(remove-all 'a '(a a a))
In [ ]:
(remove-all 'a '(b a c a d a))

3. Problem: remove-nth

remove-nth takes a number (0-based indexing), an item, and a list. It removes the nth instance of item in the list.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
(remove-nth 0 'a '(a b a c a d))
In [ ]:
(remove-nth 1 'a '(a b a c a d))
In [ ]:
(remove-nth 2 'a '(a b a c a d))

4. Problem: substitute

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.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
(substitute 'old 'new '())
In [ ]:
(substitute 'old 'new '(old))
In [ ]:
(substitute 'old 'new '(old old a b c old))

5. Problem: substitute*

substitute* works just like substitute but will find all instances no matter how deeply hidden they are inside sublists of the given list.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
(substitute* 'old 'new '((old)((((old apple bannan)) (a b c old d e))) old test ((word)) (old) old))

6. Problem: infix->prefix

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.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
(infix->prefix 1)
In [ ]:
(infix->prefix '(1 + 2))
In [ ]:
(infix->prefix '((1 + 2) * (8 / 9)))
In [ ]:
(infix->prefix '())

7. Problem: eval-infix

eval-infix will evaluate infix expressions. It should handle +, *, /, and -. It should give an error otherwise.

In [ ]:
# YOUR CODE HERE
raise NotImplementedError()
In [ ]:
(eval-infix 42)
In [ ]:
(eval-infix '(1 + 1))
In [ ]:
(eval-infix '(2 * (3 + 7)))
In [ ]:
(eval-infix '((8 / 9) * (3 + 7)))
In [ ]:
(eval-infix '(1 ^ 1))

Reflections

The reflection cell is the most import cell in this lab... in all labs. In your reflection, you should:

  • Reflect! Think and comment on what you have learned this lab, and this week. Connect the ideas onto your own life, and experiences.
  • Feel free to comment on what you found challenging, and what you found intuitive.
  • How does Scheme differ from Python?

YOUR ANSWER HERE