Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
haskell_exam
-- 1.(a) f a b = sum [x | (x, y) <- zip a b, x `mod` y == 0] -- 1.(b) g [] _ = 0 g _ [] = 0 g [x] [y] = if x `mod` y == 0 then x else 0 g (x:xs) (y:ys) = g [x] [y] + g xs ys -- 2.(a) p a | intList == [] = 0 | otherwise = maximum intList where intList = [read [x] :: Int | x <- a, x `elem` "0123456789"] -- 2.(b) q [] = 0 q [x] = if x `elem` "0123456789" then read[x] :: Int else 0 q (x:xs) = max (q [x]) (q xs) -- 2.(c) r a | intList == [] = 0 | otherwise = maximum intList where intList = [read [x] :: Int | x <- filter (`elem` "0123456789") a] -- 3.(a) data Move = Go Int -- move the given distance in the current direction | Turn -- reverse direction | Dance -- dance in place, without changing direction deriving (Show,Eq) data Command = Nil -- do nothing | Command :#: Move -- do a command followed by a move deriving (Show,Eq) type Position = Int data Direction = L | R deriving (Show,Eq) type State = (Position, Direction) --isFacingLeft L = True --isFacingLeft R = False reverseDirection L = R reverseDirection R = L state :: Move -> State -> State state (Go x) (p, d) = if d == L then (p - x, L) else (p + x, R) state Turn (p, d) = (p, reverseDirection d) state Dance s = s -- 3.(b) trace :: Command -> State -> [State] trace Nil s = [s] trace (c :#: m) s = x ++ [state m (last x)] where x = trace c s -- 3.(c) visited :: State -> [State] -> Bool visited (p, _) ss = p `elem` (map fst ss) dancify :: Command -> Command dancify Nil = Nil dancify (c :#: Dance) = (dancify c) :#: Dance dancify (c :#: m) | visited (state m (last x)) x = (dancify c) :#: m :#: Dance | otherwise = (dancify c) :#: m where x = trace c (0,R) main = print $ dancify (Nil :#: Go 3 :#: Turn :#: Go 2 :#: Go 1 :#: Turn :#: Go 4)
run
|
edit
|
history
|
help
0
project euler 10, haskell
Search
Quicksort in Haskell
Kleiner dan tien
=)
Métodos simples
Kleintjes
project euler 9, haskell
haskell fibonacci
Monoid and friends