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 = t ++ [state m (last t)] where t = 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 t)) t = (dancify c) :#: m :#: Dance | otherwise = (dancify c) :#: m where t = trace c (0,R) main = print $ dancify (Nil :#: Go 3 :#: Turn :#: Go 2 :#: Go 1 :#: Turn :#: Go 4)
run
|
edit
|
history
|
help
0
Producter
haskell linked list
Calculate Integral of function
Sierpinski Triangle as lazy list
Token
perfect numbers
mySqrt
preparednessQuotient
ReaderPractice
Haskell remove item by index