TEACH LISTQUESTIONS A. Sloman Feb 1984
=== LIST PROCESSING EXERCISES ========================================
1) Write a procedure, called LISTIFY, such that:
listify([a b c d]) =>
** [[a] [b] [c] [d]]
2) Write a procedure, called DOUBLEL, such that:
doublel([2 5 13 7]) =>
** [4 10 26 14]
3) Write a procedure, called COUNTW, such that:
countw([the man opened the door], "the") =>
** 2
COUNTW takes a list and a word and counts the number of times the word
occurs in the list. When you've define the procedure check whether it also
works for something other than a word as the second argument, e.g. a number.
4) Write a procedure, called COUNTP, such that:
countp([the man ate 3 apples and 2 pears], isinteger) =>
** 2
countp([the man ate 3 apples and 2 pears], isword) =>
** 6
COUNTP takes a list and a predicate (a procedure of one argument which
produces the result TRUE or FALSE) and counts the number of items in the
list which 'satisfy' the procedure, i.e. make it come out true.
5) Write a procedure, called ALLBEFORE, such that:
allbefore([the man opened the door], "opened") =>
** [the man]
allbefore([a b c d e f g h], "e") =>
** [a b c d]
ALLBEFORE takes a list and a word and returns a list of all the elements
of the list which precede the the given word.
6) Write a procedure, called ALLAFTER, such that:
allafter([the man opened the door], "opened") =>
** [the door]
allafter([a b c d e f g h], "e") =>
** [f g h]
7) Write a procedure, called ADDUP, so that:
addup([10 3 5 4]) =>
** 22
ADDUP takes a list of numbers and produces as its result their sum.
8) Write a procedure, called MORE, so that:
more([10 3 5 4], [2 1 5 2 1]) =>
** [10 3 5 4]
MORE takes two lists of numbers and returns the one whose elements add
up to the most.
9) Write a procedure, called REPEATED, such that:
repeated([a b c c d e f f g h]) =>
** [c f]
repeated([a b c d d c b a]) =>
** [d]
repeated([a b c d]) =>
** []
REPEATED takes a list of items and returns a list of those elements of
the original list which are followed by themselves.
10) Write a procedure, called MERGE, which takes two lists and
interleaves them.
merge([a b c d], [1 2 3 4]) =>
** [a 1 b 2 c 3 d 4]
11) Write a procedure, called INSERT, so that:
insert(34,[12 15 25 36 52 100]) =>
** [12 15 25 34 36 52 100]
INSERT takes a number and an ordered list. It returns a new list similar
to the input except that it includes the given number.
12) Write a procedure, called ORDER, such that:
order([52 100 12 34 52 36 25 15]) =>
** [12 15 25 34 36 52 52 100]
ORDER takes an unordered list and returns a sorted one. There is a
library procedure SORT which does this.
13) Pretend ADD REMOVE PRESENT and LOOKUP don't exist in the POP11
system and that you have to define them, using the names STORE FORGET
KNOWN and RECALL. Assume the procedure MATCHES is already available.
Assume all information is stored in a list called MEMORY. So
vars memory; [] -> memory;
store([john hates teaching]);
memory ==>
** [[john hates teaching]]
store([aaron likes teaching]);
memory ==>
** [[aaron likes teaching]
[john hates teaching]]
vars x;
recall([aaron likes ?x]);
x =>
** teaching
recall([ == likes eating]) =>
;;; MISHAP RECALL ERROR - PATTERN NOT FOUND
;;; INVOLVING [== likes eating]
known([aaron hates ?x]) =>
** false
known([?x hates teaching]) =>
** [john hates teaching]
NOTE
The last example implies that -known- is defined differently
from -present-, since the latter would return TRUE, not the item
found to match the pattern.
forget([ == likes == ]);
memory ==>
** [[john hates teaching]]
Write the procedures illustrated, and test them.
14) Write a procedure called PICPOINTS which will make a list of all the
points marked in a turtle picture. Assume the bounds of the picture are
in the variables XMAX and YMAX. (If necessary read TEACH TURTLE or
TEACH VTURTLE first.)
vars xmax ymax;
25 -> xmax; 20 -> ymax;
newpicture(xmax, ymax);
jumpto(5,5); draw(3);
picpoints() ==>
** [[5 5] [6 5] [7 5] [8 5]]
TEACH PICTURES elaborates on this exercise.
TEACH SETS and TEACH SETS2 give more practice to illustrate the
sorts of techniques used in this file.
--- C.all/teach/listquestions
--- Copyright University of Sussex 1988. All rights reserved. ----------