Skip to content

Commit

Permalink
adjustments to functions.hs
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-martin committed Oct 16, 2019
1 parent 379d3ef commit a87fb4f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 52 deletions.
70 changes: 18 additions & 52 deletions functions.hs
Original file line number Diff line number Diff line change
@@ -1,69 +1,35 @@
vals = (3, 7)
next x = x + 1

f x = (x, x + 1)
hypotenuse x y = sqrt (x^2 + y^2)

g :: (Num a) => a -> (a, a)
g x = (x+1, x)
greet name = "hello" ++ " " ++ name

-- to show that type signatures are optional
greetings :: String -> String
greetings name = "hello" ++ " " ++ name
greet2 :: String -> String
greet2 name = "hello" ++ " " ++ name

greet name = "hello" ++ " " ++ name
greetNext x = (next x, greet (show (next x)))

-- pattern matching
hello :: String -> String
hello "Olafur" = "hello, Olafur!"
hello "Olafur" = "hello, Olafur!"
hello "Rocamadour" = "hey!"
hello _ = "Nice to meet you!"


myDouble x = (if x > 145 then x else x*2) + 1
myDouble' x = if x > 145
then x
else x*2

-- show how to define functions using lambdas
notLambda = \x y -> x + y

-- recursion
factorial :: (Intergal a) => a -> a
factorial 0 = 1
factorial n = n * factorial (n-1)

hello x = greet x

main =
do
let (a, b) = vals
putStrLn (show a)
putStrLn (show b)
putStrLn (show (next 4))
putStrLn (show (next (next 4)))

let (_, c) = f 4
putStrLn (show c)
putStrLn (show (hypotenuse 3 4))

let (d, _) = g 4
putStrLn (show d)
putStrLn (greet "world")
putStrLn (greet2 "world")

let y = myDouble 1
putStrLn (show y)
let y' = myDouble 146
putStrLn (show y')
let z = myDouble' 1
putStrLn (show z)
let z = myDouble' 146
putStrLn (show z')
putStrLn (show (greetNext 7))

let (x, y) = greetNext 7
putStrLn (show x)
putStrLn y

putStrLn (greet "world")
putStrLn (greetings "world")
putStrLn (hello "Olafur")
putStrLn (hello "Rocamadour")
putStrLn (hello "Jane")

let t = notLambda 1 2
putStrLn (show t)

let k = factorial 5
putStrLn (show k)

-- when lambdas are convenient
map (\x -> x*x - 1) [1..5]
11 changes: 11 additions & 0 deletions outputs/functions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
5
6
5.0
hello world
hello world
(8,"hello 8")
8
hello 8
hello, Olafur!
hey!
hello Jane
3 changes: 3 additions & 0 deletions partial-application.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
map next [1..5]
map (\x -> x + 1) [1..5]

-- CURRYING:
-- f :: X -> Y -> Z -> A
-- is the same as
Expand Down
1 change: 1 addition & 0 deletions tools/outputs.nix
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ in
(run "crypto-hashing.txt" ../crypto-hashing.hs {})
(run "dynamic.txt" ../dynamic.hs {})
(run "for-loops.txt" ../for-loops.hs {})
(run "functions.txt" ../functions.hs {})
(run "hashing.txt" ../hashing.hs {})
(run "hello-world.txt" ../hello-world.hs {})
(run "mutable-references.txt" ../mutable-references.hs {})
Expand Down

0 comments on commit a87fb4f

Please sign in to comment.