-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestHaskell.hs
More file actions
34 lines (26 loc) · 828 Bytes
/
testHaskell.hs
File metadata and controls
34 lines (26 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import Data.List
{-
sortFile :: FilePath -> IO ()
sortFile filename =
do input <- readFile filename
l <- lines input
sortedString <- sort l
writeFile filename sortedString
-}
data Expr = Val Int | Div Expr Expr
eval :: Expr -> Maybe Int
eval (Val i) = Just i
eval (Div x y) = cases (eval x) (\n -> cases (eval y) (\m -> safeDiv n m))
eval' :: Expr -> Maybe Int
eval' (Val i) = Just i
eval' (Div x y) = do
n <- eval x
m <- eval y
n `safeDiv` m
-- | Cases used as binding operator >>=
cases :: Maybe Int -> (Int -> Maybe Int) -> Maybe Int
cases m f = case m of
Nothing -> Nothing
Just x -> f x
safeDiv :: Int -> Int -> Maybe Int
safeDiv m n = if n == 0 then Nothing else Just $ div m n