7. Review 1
Chapter 6
Common difficult topics: lambda and fold.
Generally familiar topics, but don’t forget: map and filter.
-
The function
r1
is supposed to remove multiples of a given numberm
from a listnums
, and then output a list whose items arem
times the squares of the remaining items.r1 :: Int -> [Int] -> [Int] r1 m nums = []
Write a single (long) line of code that defines
r1
.Example:
r1 3 [10,12,13,15] == [3*10*10,3*13*13]
-
Review the signature (and purpose) of a general folding function!
How would you write a folding function to take in a list of words and produce one long word with underscores after each one?
r2 ["Bram","Stoker","Dracula"] == "Bram_Stoker_Dracula_"
-
Write a folding function
r3h
that will do all of the work of ther1
function (first question) and add up the items in the output list.r3 :: Int -> [Int] -> Int r3 m nums = []
Chapter 6 Hint
There are two possible directions to take to make r3
:
- Allow the helper to take in the an additional parameter,
m
. - Make the helper inside a
let
orwhere
.
Chapter 7
-
Which sorting function is the most “fundamental”?
-
What is the difference between using
comparing
and using<
(less than)? -
Unusual string sorting. In this sorting method, the front of a list is the highest priority and length is more important than letters. Write a function to use in
sortBy
that results in longer words appearing first in the list and break ties in length by ordinary alphabetical order.
-
High priority messages are important, but long messages take time to send. Given a list of
(Priority,String)
, put it in order based by taking the square of the length of the message and (integer) dividing by the priority.-
Write a function to compute this number.
-
What would be an appropriate sorting method?
q7 :: [(Int,String)] -> [(Int,String)] q7 pns = [] -- you write
-