Chapter 02 Quiz 3 (Test) ANSWERS
-- 1
em xs = [(x,z^2-z+1) | x <- xs, let z = 3*x-1]
- The elements are
[4,9,14,19,24,29,34]
so there are 7.
-- 3
vowelStart word = (head word `elem` "aeiou")
-- 4
pigLatinV word = word ++ "way"
-- 5
pigLatinC word = (tail word) ++ [head word] ++ "ay"
-- 6.
piggy words =
[ pigLatinV word | word <- words, vowelStart word] ++
[ pigLatinC word | word <- words, not (vowelStart word) ]
-
This will work. zip stops when the shortest list ends.
-
Two steps:
tr [2,7,20] = 2 * tr [20] = 2*5 = 10
-- 9.
almostOn coords = [(x,y) | (x,y)<-coords,
let correct_y = (x-7)^2-50,
abs(y - correct_y) <= 1]
-- could also write the last condition as
-- correct_y - 1 <= y && y <= correct_y + 1
check9 = almostOn [(7,-49),(0,0),(15,14),(8,3)]
correct9 = [(7,-49),(0,0),(15,14)]
-- 10.
-- most people are not going to get number 10
fii :: String -> Char -> Int
fii_step1 word = zip word [0..]
fii_step2 step1 wanted = [num | (letter, num) <- step1, letter == wanted]
fii word wanted_letter = head (fii_step2 (fii_step1 word) wanted_letter)
fii_advanced word wanted_letter =
fii_step2 ((fii_step1 word) ++ [(wanted_letter,-1)])
wanted_letter
-- Programming on computer
mapd :: [Int] -> Int
mapd_step1 nums = zip nums (tail nums)
mapd_step2 step1 = [x*y | (x,y) <- step1]
mapd_step3 step2 = maximum step2 - minimum step2
mapd nums = mapd_step3 (mapd_step2 (mapd_step1 nums))
check_mapd = mapd [10,2,5,18]
correct_mapd = 80