-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConditionals.hs
32 lines (25 loc) · 1.08 KB
/
Conditionals.hs
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
module Conditionals where
conditional_examples = do
putStrLn ""
putStrLn "conditional_examples"
putStrLn ""
-- We can declare anonymous functions (lambdas) with the '\' notation:
let doubler = \x -> x * 2
putStrLn $ "doubler 3 = " ++ show (doubler 3)
-- Every if statement must contain an else
let doubleEvenNumber y = if (y `mod` 2 /= 0)
then y
else y * 2
putStrLn $ "doubleEvenNumber 3 = " ++ show (doubleEvenNumber 3)
putStrLn $ "doubleEvenNumber 4 = " ++ show (doubleEvenNumber 4)
-- We can use case statements (Note that if we want do >, <, in range,
-- etc. then you need to use Guards ('|') (See functions.hs))
let doSomethingExciting :: Int -> String
doSomethingExciting n = case n of
16 -> "Start driving"
18 -> "Start voting"
21 -> "Start drinking in US"
_ -> "Nothing exciting"
putStrLn $ "doSomethingExciting 17 = " ++ show (doSomethingExciting 17)
putStrLn $ "doSomethingExciting 18 = " ++ show (doSomethingExciting 18)
putStrLn "-----------------"