map
, reduce
, and sort
.
'(val1 val2 ... valn)
- a list of n
values.
(list exp1 exp2 ... expn)
- create a list by evaluating each of the
expressions and then joining together their values.
(make-list n val)
- make a list of n
copies of val
.
(range n)
- create a list of all the natural numbers strictly less
than n
(starting with 0
).
(map fun lst)
- apply the function to each element of the list.
(map fun (list val1 val2 ... valn))
gives you
(list (fun val1) (fun val2) ... (fun valn))
.
(reduce fun lst)
- reduce the list to a single value by repeatedly
replacing each pair of neighboring values with the result of applying
fun
to that pair of values.
(map fun lst1 lst2)
- create a new list by applying the function to
corresponding pairs of elements from the two lists. You can also use
map
with more than two lists.
(tally-value lst val)
- determine how many times val appears in lst
.
(tally lst pred?)
- determine the number of values in lst
for which
pred?
holds.
(sort nums <)
- reorder the elements in a list of real numbers from smallest to largest.
(sort nums >)
- reorder the elements a list of real numbers from largest to smallest.
(sort strings string-ci<?)
- reorder the elements in a list of strings from
alphabetically first to alphabetically last.
(sort strings string-ci>?)
- reorder the elements in a list of strings from
alphabetically last to alphabetically first.
(length lst)
- Determine how many elements are in a list.
(reverse lst)
- Create a new list with the elements in the opposite
order.
(append lst1 lst2)
- Join two lists together.
(take lst n)
- Build a new list consisting of the first n
elements
of lst
.
(drop lst n)
- Build a new list consisting of all but the first n
elements of lst
.
(list-ref lst n)
- Extract element n
of the list. (Remember that
lists start with element 0.)
(index-of val lst)
- Determine the position of val
in lst
. (It
turns out the position is how many values need to be dropped
from lst
to reach val
.)
(indexes-of val lst)
- Find all the indices of the value in the list.
(o f g)
- Create a new procedure that applies g
and then applies f
to the result.
(o f g h)
- Create a new procedure that applies h
, then g
to the
result of h
, then f
to the result of g
.
(section f <> val)
- Create a new procedure that takes one parameter
and calls f
on that parameter and val
.
a. Review the list of procedures above.
b. If you have not done so already, you may want to open a separate tab or window in your browser for the reading on list basics and the new reading on processing homogeneous lists.
c. Grab the latest version of the loudhum
package by donig one
of the following:
i. Open a terminal window and then type the following.
$ /home/rebelsky/bin/csc151/update
ii. From the File menu in DrRacket, select Install Package and then enter https://github.com/grinnell-cs/loudhum.git.
d. In your definitions window, require the loudhum
library.
(require loudhum)
e. Add the following to your definitions pane.
; A list of movie ratings for The Large Illness
(define large-illness-ratings
(list 7 4 0.2 -3 6 5.5 5 6 -1.5 54/7 0))
; Some words, as strings
(define story-beginning
(list "Once" "upon" "a" "time" "in" "a" "land" "not" "so" "far" "away"))
; Some names, as symbols
(define names
(list 'millicent 'octavius))
; A few letters, as strings
(define letters
(list "a" "b" "c" "d" "e"))
; The words in Jane Eyre (plus whatever Project Gutenberg adds)
(define eyre-words (file->words "/home/rebelsky/Desktop/pg1260.txt"))
f. Add the names of about six other people (e.g., your group and the two groups nearest you) to the list of names.
a. Predict the results of evaluating each of the following expressions.
(list 2 1)
(make-list 1 2)
(make-list -1 2)
(map - (range 2))
(map - (range 2) (list 2 1))
(map range (list 2 1))
b. Check your predictions with DrRacket.
Write a set of instructions that allow you to compute the average rating given The Large Illness.
The ratings associated with The Large Illness include both whole numbers and fractional numbers. Suppose that “those on high” decide that only whole-number ratings are permitted.
a. Write an instruction that rounds each rating to the nearest integer.
(Hint: You’ll want to use map
and round
.)
b. Write an instruction that rounds each rating down.
c. Write an instruction that rounds each rating up.
d. Determine the effects of each strategy on the average rating.
As you may recall from the preparation, story-beginning
contains
a short list of strings and eyre-words
contains a much longer list
of strings.
a. Write expressions that determine how many words are in each list.
b. Write an expression that determines the length of each word
in story-beginning
.
c. Write an expression that determines the average word length
in story-beginning
.
d. Write an expression that determines the average word length
in eyre-words
.
e. Write an expression that determines the longest word length
in eyre-words
.
a. Write an expression that determines how many times the word “a”
appears in story-beginning
.
b. Write an expression that determines how many times the word “a”
appears in eyre-words
.
c. Write an exprssion that determines how many times the word “Eyre”
appears in eyre-words
.
d. Write an exprssion that determines how many times the word “Reed”
appears in eyre-words
.
e. Write expressions that determine how many times 4-letter, 5-letter,
and 10-letter words appear in eyre-words
.
a. Determine and then explain what the following procedure does.
(define combine
(lambda (s1 s2)
(string-append s1 " and " s2 " and " s1)))
b. What do you expect for the following?
> (reduce combine letters)
d. Check your answer experimentally.
e. What do you expect if you type the same expression again?
f. Check your answer experimentally.
g. What do you expect for the following?
> (reduce-left combine letters)
?
> (reduce-right combine letters)
?
i. Check your answer experimentally
a. Write an expression to sort the ratings from lowest to highest.
b. Write an expression to sort the ratings from highest to lowest.
c. Write an expression to alphabetically sort the words in story-beginning
.
d. Write an expression to alphabetically sort the names. Note that the names are symbols, so you will need to do some conversion.
a. Write an expression to find the three alphabetically last words
in story-beginning
.
b. Write an expression to find the ten alphabetically last words
in eyre-words
.
a. In looking at the alphabetically last words in eyre-words
, we
discovered that zenith
appears exactly once in Jane Eyre. Using
index-of
, determine which word it is. (That is, what word number.)
b. Using list-ref
, determine what words come immediately before
and after zenith
.
c. In an earlier exercise, we noted that the word “Eyre” appears about 100
times in Jane Eyre. Using indexes-of
, determine where those
100 appearances are.
d. What does the following expression do? (Conceptually.)
> (map (o (section list-ref eyre-words <>) add1) (list 100 200 300 400))
e. Using a similar expression, determine the words that immediately follow “Eyre”.
f. Using a similar expression, determine the words that immediately precede “Eyre”.
If you find that you have extra time, you might consider any of the following problems.
In an exercise, you determined the number of 4, 5, and 10-character
words that appear in Jane Eyre. Using map
, tally-val
, and
range
, write a single expression that determines how many 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, and 12-letter words there are in Jane Eyre.
Consider the following list.
(define ones (make-list 10 1))
a. How many different values do you think you can get from (reduce - ones)
?
b. Check your prediction experimentally.
If you find that you have extra time, explore any of the procedures not covered in the exercises above.