Skip to content

Instantly share code, notes, and snippets.

@oubiwann
Last active April 4, 2021 05:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oubiwann/8388910 to your computer and use it in GitHub Desktop.
Save oubiwann/8388910 to your computer and use it in GitHub Desktop.
Playing with Haskell prefix notation
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
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
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>
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]
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>
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
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>
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