Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Playing with Haskell prefix notation

View 01-simple.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Prelude> ((+) 1 2)
3
Prelude> ((+) 1 2 3 4 5 6)
 
<interactive>:3:2:
No instance for (Num (a0 -> a1 -> a2 -> a3 -> t0))
arising from a use of `+'
Possible fix:
add an instance declaration for (Num (a0 -> a1 -> a2 -> a3 -> t0))
In the expression: ((+) 1 2 3 4 5 6)
In an equation for `it': it = ((+) 1 2 3 4 5 6)
...
 
Prelude> ((+) `foldl` 0) [1..6]
21
Prelude> ((sum) [1..6])
21
View 01-simple.hs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Prelude> ((==) 1 1)
True
Prelude> ((==) 1 2)
False
Prelude> ((/=) 1 1)
False
Prelude> ((/=) 1 2)
True
Prelude> ((&&) True False)
False
Prelude> ((&&) True True)
True
Prelude> ((||) True False)
True
Prelude> ((succ) 1)
2
View 01-simple.hs
1 2 3 4 5 6 7 8 9 10 11 12
Prelude> ((++) [1,2] [3,4])
[1,2,3,4]
Prelude> ((:) 1 [2,3])
[1,2,3]
Prelude> ((head) [1,2,3,4])
1
Prelude> ((tail) [1,2,3,4])
[2,3,4]
Prelude> import Data.List
Prelude Data.List> ((\\) [1,2,3,4] [1,4])
[2,3]
Prelude Data.List>
View 01-simple.hs
1 2 3 4
Prelude> [((*) x 2) | x <- [1..10]]
[2,4,6,8,10,12,14,16,18,20]
Prelude> [((*) x y) | x <- [2,5,10], y <- [8,10,11], ((>) ((*) x y) 50)]
[55,80,100,110]
View 01-simple.hs
1 2 3 4 5 6 7 8 9 10
Prelude> doubleMe x = ((+) x x)
Prelude> ((doubleMe) 21)
42
Prelude> doubleUs x y = ((+) ((doubleMe) x) ((doubleMe) y))
Prelude> ((doubleUs) 5 20)
50
Prelude> doubleSmallNumber x = (if ((>) x 100) then x else ((*) x 2))
Prelude> ((doubleSmallNumber) 100)
200
Prelude>
View 01-simple.hs
1 2 3 4 5 6 7
Prelude> (\ x -> ((+) x 1)) 4
5
Prelude> (\ x y -> ((+) x y)) 3 5
8
Prelude> f x = (\ y -> ((+) x y))
Prelude> (f 10) 25
35
View 01-simple.hs
1 2 3 4 5 6 7 8 9
Prelude> import Data.List
Prelude Data.List> values = [1,2,2,3,4,5,99,66,6,7,8,8,9]
Prelude Data.List> (\ x -> (reverse (sort (nub (filter odd x))))) values
[99,9,7,5,3,1]
Prelude Data.List> ((.) ((.) ((.) reverse sort) nub) (filter odd)) values
[99,9,7,5,3,1]
Prelude Data.List> (reverse . sort . nub . (filter odd)) values
[99,9,7,5,3,1]
Prelude Data.List>
View 01-simple.hs
1 2 3 4 5 6 7 8 9
Prelude> [1, 2, 3] >>= \x -> [x + 1, x - 1] >>= \y -> return (y * x)
[2,0,6,2,12,6]
Prelude> ((>>=)
[1, 2, 3]
(\ x ->
(>>=)
[((+) x 1), ((-) x 1)]
(\ y -> return ((*) y x))))
[2,0,6,2,12,6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.