Skip to content

Instantly share code, notes, and snippets.

View yangsu's full-sized avatar

Yang Su yangsu

  • San Francisco, CA
  • X @ys65
View GitHub Profile
@yangsu
yangsu / haskell-tree-ex.hs
Created April 23, 2013 19:18
Haskell Tree Example
*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
@yangsu
yangsu / haskell-recursive-type.hs
Created April 23, 2013 19:18
Haskell Recursive Type
module Main where
data Tree a = Children [Tree a] | Leaf a deriving (Show)
depth (Leaf _) = 1
depth (Children c) = 1 + maximum (map depth c)
@yangsu
yangsu / haskell-user-defined-type.hs
Created April 23, 2013 19:08
Haskell User Defined Types
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
@yangsu
yangsu / haskell-bool.hs
Last active December 16, 2015 14:09
Haskell Boolean
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
@yangsu
yangsu / haskell-lazy-fib-usage.hs
Last active December 16, 2015 13:49
Haskell Lazy Fib Usage
*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
@yangsu
yangsu / haskell-lazy-fib.hs
Created April 23, 2013 15:15
Haskell Fibonacci with Lazy Evaluation
module Main where
lazyFib x y = x:(lazyFib y (x + y))
fib = lazyFib 1 1
fibNth x = head (drop (x - 1) (take (x) fib))
@yangsu
yangsu / haskell-currying.hs
Created April 23, 2013 15:09
Haskell Partially Applied Functions and Currying
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
@yangsu
yangsu / haskell-higherorder-functions.hs
Created April 23, 2013 15:04
Haskell Higher Order Functions
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]
@yangsu
yangsu / haskell-for-comprehensions.hs
Last active December 16, 2015 13:48
Haskell For Comprehensions
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")]
@yangsu
yangsu / haskell-ranges.hs
Created April 23, 2013 14:25
Haskell Ranges
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]