Last active
October 1, 2016 13:49
-
-
Save BillyBadBoy/e320aec5c797a3e460db910877749ceb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
----------------------------------------------------------------------- | |
-- [1] | |
-- Implement 'vecLen', which finds the length of an n-dimensional vector | |
-- length (a1, a2, ..., an) = sqrt (a1^2 + a2^2 + ... + an^2) | |
-- use: zipWith, sqrt, sum, * | |
vecLen :: [Double] -> Double | |
vecLen v = ??? | |
-- expected behaviour: | |
-- vecLen [3.0, 4.0] == 5.0 | |
----------------------------------------------------------------------- | |
-- [2] | |
-- Implement 'matches' which finds the number of matches between 2 lists | |
-- matches [3,6,8,4,6] [8,3,8,4,4] = 2 since the 3rd and 4th items match | |
-- use: zipWith, sum | |
matches :: [Int] -> [Int] -> Int | |
matches xs ys = ??? | |
-- expected behaviour: | |
-- matches [3,6,8,4,6] [8,3,8,4,4] == 2 | |
----------------------------------------------------------------------- | |
-- [3] | |
-- Implement numRepeats, which counts consecutive repeated items | |
-- e.g numRepeats [7,7,7,5,7,4,4] = 3 since 7 repeats twice and 4 once | |
-- use: matches, tail | |
numRepeats :: [Int] -> Int | |
numRepeats xs = ??? | |
-- expected behaviour: | |
-- numRepeats [] == 0 | |
-- numRepeats [7,7,7,5,7,4,4] == 3 | |
----------------------------------------------------------------------- | |
-- [4] | |
-- Implement isIn, an alternative version of elem | |
-- use: zipWith, repeat, or, don't use elem, comprehensions | |
-- (note: repeat x = [x,x,x,x...]) | |
isIn :: Int -> [Int] -> Bool | |
isIn x xs = ??? | |
-- expected behaviour: | |
-- 7 `isIn` [] == False | |
-- 7 `isIn` [1,2,3] == False | |
-- 7 `isIn` [0..] == True | |
----------------------------------------------------------------------- | |
-- [5] | |
-- Implement 'unique', which checks if a list's elements are distinct | |
-- use: comprehension, zip, and, ||, ==, /=, don't use elem, isIn | |
-- hint: use zip to annotate each item with its index | |
unique :: [Int] -> Bool | |
unique xs = ??? | |
-- expected behaviour: | |
-- unique [] == True | |
-- unique [1,2,3] == True | |
-- unique [1,2,1] == False | |
----------------------------------------------------------------------- | |
-- [6] | |
-- Implement 'doToAll', an alternative version of map | |
-- use: zipWith, repeat, don't use comprehensions, map | |
doToAll :: (Int -> Int) -> [Int] -> [Int] | |
doToAll f xs = ??? | |
-- expected behaviour: | |
-- doToAll (*2) [] == [] | |
-- doToAll (*2) [1,2,3] == [2,4,6] | |
----------------------------------------------------------------------- | |
-- [7] | |
-- Implement 'poly', which evaluates polynomials | |
-- 'coeffs' argument is coefficients from biggest to smallest | |
-- e.g. [3,2,1] means 3x^2 + 2x + 1 e.g. [7,0,-1] means 7x^2 - 1 | |
-- use: ^, zipWith, sum | |
poly :: [Double] -> Double -> Double | |
poly coeffs x = ??? | |
-- expected behaviour: | |
-- poly [3,2,1] 1 == 6.0 (3 + 2 + 1) | |
-- poly [3,2,1] 10 == 321.0 (3*10^2 + 2*10 + 1) | |
-- poly [1,0,0,0,0,0,0,0,0,0,0] 2 = 1024 (2^10) | |
----------------------------------------------------------------------- | |
-- [8] | |
-- Implement restrict, an alternative version of filter | |
-- p is a predicate (i.e. a test) to apply to each char in a String | |
-- e.g. restrict (\c -> c `elem` "aeiou") "haskell" = "ae" | |
-- Note: comprehensions can include guards and let declarations to the | |
-- right of the | symbol (in addition to generators). These make it easy | |
-- to implement filter: [ c | c <- haskell, (c `elem` "aeiou") ] | |
-- But these haven't been covered in the mooc, so they're out of scope | |
-- for this question ! | |
-- use: list comprehensions (but no guards or lets), don't use filter | |
-- hint: use 2 list comprehensions: | |
-- one for: "haskell" -> [[],['a'],[],[],['e'],[],[]] | |
-- one for: [[],['a'],[],[],['e'],[],[]] -> ['a','e'] == "ae" | |
restrict :: (Char -> Bool) -> String -> String | |
restrict p s = ??? | |
-- expected behaviour: | |
-- restrict (\c -> c `elem` "aeiou") "haskell" == "ae" | |
----------------------------------------------------------------------- | |
-- [9] | |
-- Implement 'restrictPair' another filter function | |
-- Similar to 'restrict' but works on char pairs instead of chars | |
-- restrictPair (\(a,b) -> a == b) [('x','y),('x','x)] = [('x','x)] | |
-- copy your implementation of 'restrict' with a new type signaure | |
restrictPair :: ??? | |
restrictPair p s = ??? | |
-- expected behaviour: | |
-- restrictPair (\(a,b) -> a == b) [('x','y'),('x','x')] == [('x','x')] | |
----------------------------------------------------------------------- | |
-- [10] | |
-- Implement 'scramble' which uses one list to lookup items in another | |
-- e.g. scramble "6432597810" "5682139704" maps 6 -> 5, 4 -> 6, 3 -> 8 etc | |
-- you may assume that the lists don't contain duplicates | |
-- use: zip, restrictPair, fst, snd, head | |
scramble :: String -> String -> Char -> Char | |
scramble s1 s2 c = ??? | |
-- expected behaviour: | |
-- scramble "6432597810" "5682139704" '6' = '5' | |
-- scramble "6432597810" "5682139704" '3' = '8' | |
----------------------------------------------------------------------- | |
-- [11] | |
-- 'doToChars' - identical to 'doToAll' except it works with chars | |
-- e.g. doToChars (\c -> '*') "haskell" == "*******" | |
-- copy your implementation of 'doToAll' with a new type signaure | |
doToChars :: ??? | |
doToChars f xs = ??? | |
-- expected behaviour: | |
-- doToChars (\c -> '*') "haskell" == "*******" | |
----------------------------------------------------------------------- | |
-- [12] | |
-- Implement code which scrambles/unscrambles numeric text | |
-- e.g code True "6432597810" "5682139704" "1234" = "0286" | |
-- e.g code False "6432597810" "5682139704" "0286" = "1234" | |
-- the boolean argument specifies whether to scamble or unscramble | |
-- use 'doToChars' 'scramble' | |
code :: Bool -> String -> String -> String -> String | |
code direction s1 s2 text = ??? | |
-- expected behaviour: | |
-- let scrambler = code True "6432597810" "5682139704" | |
-- let unscrambler = code False "6432597810" "5682139704" | |
-- scrambler "1234" == "0286" //encode | |
-- unscrambler "0286" == "1234" //decode | |
----------------------------------------------------------------------- | |
-- [13] | |
-- Implement 'headOverHeels' which swaps first/last strings in a list | |
-- e.g. headOverHeels ["three","two","one"] = ["one","two","three"] | |
-- use: length, head, tail, drop, take, don't use last, !! | |
-- n.b. ensure it works with lists of length 0 and 1. | |
headOverHeels :: [String] -> [String] | |
headOverHeels s = ??? | |
-- expected behaviour: | |
-- headOverHeels [] == [] | |
-- headOverHeels ["one"] == ["one"] | |
-- headOverHeels ["one","two"] == ["two","one"] | |
-- headOverHeels ["three","two","one"] == ["one","two","three"] | |
----------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
map
was not covered in week 2. You can usezipWith
instead to solve qu1.