This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*Main> let leaf = Leaf 1 | |
*Main> leaf | |
Leaf 1 | |
*Main> Children[Leaf 1, Leaf 2] | |
Children [Leaf 1,Leaf 2] | |
*Main> let tree = Children[Leaf 1, Children [Leaf 2, Leaf 3]] | |
*Main> tree | |
Children [Leaf 1,Children [Leaf 2,Leaf 3]] | |
*Main> let (Children (h:t)) = tree | |
*Main> h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
data Tree a = Children [Tree a] | Leaf a deriving (Show) | |
depth (Leaf _) = 1 | |
depth (Children c) = 1 + maximum (map depth c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
data Suit = Spades | Hearts deriving (Show) | |
data Rank = Ten | Jack | Queen | King | Ace deriving (Show) | |
type Card = (Rank, Suit) | |
type Hand = [Card] | |
value :: Rank -> Integer | |
value Ten = 10 | |
value Jack = 11 | |
value Queen = 12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prelude> 1 /= 2 | |
True | |
Prelude> (>=) 1 2 | |
False | |
Prelude> (&&) (3 < 8) (False == False) | |
True | |
Prelude> (||) (18 == 17) (9 >= 11) | |
False | |
Prelude> not (5 * 2 == 10) | |
False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*Main> take 5 (lazyFib 0 1) | |
[1,1,2,3,5] | |
*Main> take 5 (fib) | |
[1,1,2,3,5] | |
*Main> take 5 (drop 20 (lazyFib 0 1)) | |
[10946,17711,28657,46368,75025] | |
*Main> fibNth 3 | |
2 | |
*Main> fibNth 6 | |
8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
lazyFib x y = x:(lazyFib y (x + y)) | |
fib = lazyFib 1 1 | |
fibNth x = head (drop (x - 1) (take (x) fib)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prelude> let prod x y = x * y Prelude> prod 3 4 | |
12 | |
Prelude> :t prod | |
prod :: (Num a) => a -> a -> a | |
Prelude> let double = prod 2 | |
Prelude> let triple = prod 3 | |
Prelude> double 3 | |
6 | |
Prelude> triple 4 | |
12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prelude> map (\x -> x * x) [1, 2, 3] | |
[1,4,9] | |
Prelude> let squareAll list = map square list where square x = x * x | |
[1,4,9] | |
Prelude> map (+ 1) [1, 2, 3] | |
[2,3,4] | |
Prelude> :t (+ 1) | |
(+ 1) :: Num a => a -> a | |
Prelude> filter odd [1, 2, 3, 4, 5] | |
[1,3,5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prelude> [ (y, x) | (x, y) <- [(1, 2), (2, 3), (3, 1)]] | |
[(2,1),(3,2),(1,3)] | |
Prelude> let crew = ["Kirk", "Spock", "McCoy"] | |
Prelude> [(a, b) | a <- crew, b <- crew] | |
[("Kirk","Kirk"),("Kirk","Spock"),("Kirk","McCoy"), ("Spock","Kirk"),("Spock","Spock"),("Spock","McCoy"), ("McCoy","Kirk"),("McCoy","Spock"),("McCoy","McCoy")] | |
Prelude> [(a, b) | a <- crew, b <- crew, a /= b] | |
[("Kirk","Spock"),("Kirk","McCoy"),("Spock","Kirk"), ("Spock","McCoy"),("McCoy","Kirk"),("McCoy","Spock")] | |
Prelude> [(a, b) | a <- crew, b <- crew, a < b] | |
[("Kirk","Spock"),("Kirk","McCoy"),("McCoy","Spock")] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prelude> [1..2] | |
[1,2] | |
Prelude> [1..4] | |
[1,2,3,4] | |
Prelude> [10..4] | |
[] | |
Prelude> [10, 8 .. 4] | |
[10,8,6,4] | |
Prelude> [10, 9.5 .. 4] | |
[10.0,9.5,9.0,8.5,8.0,7.5,7.0,6.5,6.0,5.5,5.0,4.5,4.0] |