Skip to content

Instantly share code, notes, and snippets.

@thomaschrstnsn
Created November 9, 2014 12:59
Show Gist options
  • Save thomaschrstnsn/08c0320c89319f17b219 to your computer and use it in GitHub Desktop.
Save thomaschrstnsn/08c0320c89319f17b219 to your computer and use it in GitHub Desktop.
Example of testing several implementations of the same function #fp101x lecture 6
check impls test = map (\(f,i) -> (i, test f))
(zip impls [1..(length impls)])
filterCorrect trs = map (\(i, _) -> i) correct
where correct = filter (\(i, b) -> b) trs
all1 p xs = and (map p xs)
all2 p xs = False -- map p (and xs)
all3 p = and . map p
all4 p = not . any (not . p)
all5 p = \xs -> False -- map p . and
all6 p xs = foldl (&&) True (map p xs)
all7 p xs = foldr (&&) False (map p xs)
all8 p = foldr (&&) True . map p
alls :: [(a -> Bool) -> [a] -> Bool]
alls = [all1, all2, all3, all4, all5, all6, all7, all8]
allTest f = (map (f id) xss) == (map (all id) xss)
where xss = [[False], [True], [False, False], [True, False], [True, True], [True]]
correctAlls = filterCorrect $ check alls allTest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment