Skip to content

Instantly share code, notes, and snippets.

@alistair
Last active November 24, 2015 16:55
Show Gist options
  • Save alistair/2712ea217ac343fced44 to your computer and use it in GitHub Desktop.
Save alistair/2712ea217ac343fced44 to your computer and use it in GitHub Desktop.
Haskell notes
== equals
/= not equals
'a' : ['b', 'c'] -- prepend e.g. 1:2:3:[]
['a'] ++ ['b', 'c'] --append
[x*2 | x <- [1..10], x*2 >= 12] -- list comprehension
let rightTriangles = [ (a,b,c) | c <- [1..10], b <- [1..c], a <- [1..b], a^2 + b^2 == c^2]
if x > 100
then x
else x*2
== Common Types ==
Int
Integer
Float
Double
Bool
Char
== Basic Typeclasses ==
Eq
Ord
Read
Enum
Bounded
Num -- All numbers
Intergral -- Int and Integer
Floating -- Only Float and Double
Show
== Functions ==
fromIntegral :: (Num b, Integral a) => a -> b
// name :: constraints => param -> result
// short hand functions can be defined with multiple params
integralEnumFromTo :: Integral a => a -> a -> [a]
integralEnumFromTo n m = map fromInteger [toInteger n .. toInteger m]
== Pattern matching ==
lucky :: (Integral a) => a -> String
lucky 7 = "LUCKY NUMBER SEVEN!"
lucky x = "Sorry, you're out of luck, pal!"
length' :: (Num b) => [a] -> b
length' [] = 0
length' (_:xs) = 1 + length' xs
//captures variable all
capital :: String -> String
capital "" = "Empty string, whoops!"
capital all@(x:xs) = "The first letter of " ++ all ++ " is " ++ [x]
== Guards ==
bmiTell :: (RealFloat a) => a -> a -> String
bmiTell weight height
| weight / height ^ 2 <= 18.5 = "You're underweight, you emo, you!"
| weight / height ^ 2 <= 25.0 = "You're supposedly normal. Pffft, I bet you're ugly!"
| weight / height ^ 2 <= 30.0 = "You're fat! Lose some weight, fatty!"
| otherwise = "You're a whale, congratulations!"
bmiTell :: (RealFloat a) => a -> a -> String
bmiTell weight height
| bmi <= skinny = "You're underweight, you emo, you!"
| bmi <= normal = "You're supposedly normal. Pffft, I bet you're ugly!"
| bmi <= fat = "You're fat! Lose some weight, fatty!"
| otherwise = "You're a whale, congratulations!"
where bmi = weight / height ^ 2
skinny = 18.5
normal = 25.0
fat = 30.0
== let bindings ==
cylinder :: (RealFloat a) => a -> a -> a
cylinder r h =
let sideArea = 2 * pi * r * h
topArea = pi * r ^2
in sideArea + 2 * topArea
(let a = 100; b = 200; c = 300 in a*b*c, let foo="Hey "; bar = "there!" in foo ++ bar)
== case expressions ==
describeList :: [a] -> String
describeList xs = "The list is " ++ case xs of [] -> "empty."
[x] -> "a singleton list."
xs -> "a longer list."
== Functors / fmap
fmap (getPostTitle) (findPost 1) === getPostTitle <$> (findPost 1)
A functor is a data type that implements the Functor typeclass.
An applicative is a data type that implements the Applicative typeclass.
A monad is a data type that implements the Monad typeclass.
A Maybe implements all three, so it is a functor, an applicative, and a monad.
functors: you apply a function to a wrapped value using fmap or <$>
applicatives: you apply a wrapped function to a wrapped value using <*> or liftA
monads: you apply a function that returns a wrapped value, to a wrapped value using >>= or liftM
--type alias
type String = [Char]
--data declarations
data Bool = True | False
data Shape = Circle Float
| Rect Float Float
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment