Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
haskell_exam_demo
main = print $ "100" -- 1.(a) f a b = sum [x | (x, y) <- zip a b, x `mod` y == 0] {-- main = do print $ f [6,9,2,7] [2,3,5,1] print $ f [6,9,2] [2,3,5,1] print $ f [1,2,3,4,5] [5,4,3,2,1] print $ f [10,20,30,40] [3,4,5,6,7] --} -- 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 {-- main = do print $ g [6,9,2,7] [2,3,5,1] print $ g [6,9,2] [2,3,5,1] print $ g [1,2,3,4,5] [5,4,3,2,1] print $ g [10,20,30,40] [3,4,5,6,7] --} -- 2.(a) p a | intList == [] = 0 | otherwise = maximum intList where intList = [read [x] :: Int | x <- a, x `elem` "0123456789"] {-- main = do print $ p "Inf1-FP" print $ p "Functional" print $ p "1+1=2" print $ p "3.157/3 > 19" --} -- 2.(b) q [] = 0 q [x] = if x `elem` "0123456789" then read[x] :: Int else 0 q (x:xs) = max (q [x]) (q xs) {-- main = do print $ q "Inf1-FP" print $ q "Functional" print $ q "1+1=2" print $ q "3.157/3 > 19" --} -- 2.(c) r a | intList == [] = 0 | otherwise = maximum intList where intList = [read [x] :: Int | x <- filter (`elem` "0123456789") a] {-- main = do print $ r "Inf1-FP" print $ r "Functional" print $ r "1+1=2" print $ r "3.157/3 > 19" --} -- 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 {-- main = do print $ state (Go 3) (0,R) print $ state (Go 3) (0,L) print $ state Turn (-2,L) print $ state Dance (4,R) --} -- 3.(b) trace :: Command -> State -> [State] trace Nil s = [s] trace (c :#: m) s = x ++ [state m (last x)] where x = trace c s {-- main = do print $ trace (Nil) (3,R) print $ trace (Nil :#: Go 3 :#: Turn :#: Go 4) (0,L) print $ trace (Nil :#: Go 3 :#: Dance :#: Turn :#: Turn) (0,R) print $ trace (Nil :#: Go 3 :#: Turn :#: Go 2 :#: Go 1 :#: Turn :#: Go 4) (4,L) --} -- 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 = do print $ dancify Nil print $ dancify (Nil :#: Go 3 :#: Turn :#: Go 4) print $ dancify (Nil :#: Go 3 :#: Dance :#: Turn :#: Turn) print $ dancify (Nil :#: Go 3 :#: Turn :#: Go 2 :#: Go 1 :#: Turn :#: Go 4) --}
run
|
edit
|
history
|
help
0
equiv_strings
NQueens
Lambda Parser with long ids
appending and prepending list
sumer
perfect numbers
(⁄⁄>⁄▽⁄<⁄⁄)
Cubed
Token
Kleintjes