1. Write a recursive function INSERT-AFTER that takes three arguments: an ITEM to insert, an ELEMENT, and a LIST. It should return a new list containing all elements in the LIST with the new ITEM inserted after the first occurrance of the ELEMENT. If the ELEMENT does not appear in the list, then the ITEM should be added to the end of the list. Examples:
(insert-after 'c 'b '(a b d e)) => (a b c d e) (insert-after 'a 'z '(a b c d)) => (a b c d a)Your function should be non-destructive. That is, it should return a new list rather than destructively modifying the old list. The new list may share structure with the old list.
2. Write a function HIST that takes a list of numbers and returns a list of dotted pairs. The first element of each dotted pair should be a number from the list of numbers, and the second element should be the number of times that the number occurred in the input list. Examples:
(hist '(1 2 1 3 1 5 2 6)) => ((1 . 3) (2 . 2) (3 . 1) (5 . 1) (6 . 1))This says that the number 1 appeared 3 times, the number 2 appeared 2 times, and the numbers 3, 5, and 6 each appeared one time. The items do not need to appear in any particular order. Note: You may destructively modify the input list (e.g., by sorting it).
3. Write a function RANDOM-CHOOSE that takes two arguments: a LIST of
items and a NUMBER. It should return a list containing NUMBER
elements drawn randomly from LIST without replacement. In other
words, RANDOM-CHOOSE should select a random subset of LIST (of size
NUMBER) and return it as a list. You should use the Floyd algorithm,
which works as follows. Let n be the length of the list and m be the
number of items to be chosen. Then the first element in the LIST
should be in the answer with probability m/n. So we "flip a coin"
with probability of heads m/n. If the coin "comes up" heads, we
include the element, and this leaves us a recursive problem of
selecting m-1 items from the n-1 remaining elements of LIST. If the
coin "comes up" tails, we skip the element, which leaves us the
recursive problem of selecting m items from the rest of LIST (which
now has n-1 elements). Note that the form (random 1.0)
returns a number R chosen uniformly at random such that
0 <= R < 1.
4. CONS-cell diagrams. Draw a cons cell diagram for each of the following S-expressions:
(((a) b) c) (a (b c (d)) e (f . g))Example:
(a b . c) can be drawn as --------- --------- | * | *-|----> | * | * | --|------ --|---|-- | | | V V V a b c
5. (Borrowed from Mike Wellman.) Write a function called FRINGE that takes one argument: a general S expression containing SYMBOL's. It should traverse the S expression and return a new list containing all of the symbols in S in left-to-right order. Example:
(fringe '(a (b c (d)) e (f g))) => (a b c d e f g)
6. Write a function MAKE-COUNTER that returns a closure. Each time this closure is invoked, it should increment an internal counter and display the number of times it has been called. Example:
(setq c1 (make-counter)) (setq c2 (make-counter)) (funcall c1) I have been called 1 times. (funcall c1) I have been called 2 times. (funcall c2) I have been called 1 times.