Skip to content

Instantly share code, notes, and snippets.

@dustinlacewell
Last active September 8, 2021 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dustinlacewell/6cae02e3bba25ff42bebafce655498ea to your computer and use it in GitHub Desktop.
Save dustinlacewell/6cae02e3bba25ff42bebafce655498ea to your computer and use it in GitHub Desktop.

Haskell

Syntax Guide

Variables

Defining

a = 10
b = "foo"

Letting

let x = 5
    y = 10
in x + y

Where-ing

x + y
where x = 5
      y = 10

Function Basics

Defining

add x y = x + y

Calling

total = add 5 10 -- 15

Partial Application

inc = add 1
total = inc 5 -- 6

Anonymous Functions

(\x y -> x + y)

Infixing

total = 5 `add` 10 -- 15

Currying

incChar = chr . add 1 . ord
incChar 'a' -- 'b'

Operators

Calling

total = 2 + 2 -- 4

Prefixing

total = (+) 2 2 -- 4

Sectioning (partially apply second/rhs argument)

divideBy2 = (/ 2)
total = divideBy2 10 -- 5

Lists

Defining

words = ["foo", "bar", "faz", "boz"]

Ranges

oneToTen = [1 .. 10]
aToZ = ['a' .. 'z']

Indexing

third = words !! 2 -- "faz"

Concatenating

otherWords = words ++ ["fop", "fob"]

Prepending

oneMoreWord = "baz" : otherWords

Tips

first = head [1, 2, 3] -- 1
last  = tail [1, 2, 3] -- 3

Slices

firstTwo = take 2 [1, 2, 3] -- [1, 2]
lastTwo  = drop 1 [1, 2, 3] -- [2, 3]

Splitting

splitAt 5 [1..10] -- ([1,2,3,4,5],[6,7,8,9,10])
splitAt 10 ['a'..'z'] -- ("abcdefghij","klmnopqrstuvwxyz")

Pattern Matching

(x : _) = [1, 2, 3] -- 1
(_ : xs) = [1, 2, 3] -- [2, 3]
[a, b, c] = [1, 2, 3]

List Comprehensions

[ toUpper x | x <- ['a'..'c'] ] -- "ABC"
[ x * x | x <- [1..10], (mod x 2) == 0 ] -- [4,16,36,64,100]

Strings (are just [Char])

type String = [Char]

Defining

-- these are the same
foo = "bar" -- String
foo = ['b','a','r'] -- [Char]

Printing

-- print almost anything
print "Hello World"
print (53, "hello")

-- print String specifically
putStr "Hello World" -- no newline
putStrLn "!!!" -- newline added

List Equivalent Operations

-- these work the same as lists
-- because String is [Char]
third = "abcde" !! 2 -- 'c'
merged = "foo" ++ "bar" -- "foobar"
prefixed = '$' : "10.00" -- "$10.00"
first = head "abc" -- 'a'
last = tail "abc" -- 'c'
firstTwo = take 2 "abc" -- "ab"
lastTwo = drop 1 "abc" -- "bc"

Tuples

Defining

point = (4, 23)

Destructuring

(x, y) = point

Accessing

x = fst point
y = snd point

Product Types

Defining

data Person = Person String String Int

Constructing

joe = Person "Joe" "Shmoe" 42

Destructuring

Person first last age = joe

Deriving

data Person = Person String String Int deriving (Show, Eq)

Sum Types

Defining

data Make = Ford | Toyota | Tesla
data Airline = American | Southwest | Delta
data Vehicle = Car Make | Plane Airline

Constructing

car = Car Tesla
plane = Plane Southwest

Advanced Functions

Guards

isItTwo 2 = True
isItTwo _ = False

isItTwo 2 -- True
isItTwo 10 -- False

Pattern Matching Arguments

isPlane (Plane _) = True
isPlane _ = False

isPlane car -- False
isPlane plane -- True

Pattern Matching Cases

isDelta (Plane airline) =
  case airline of
    Delta -> True
    _ -> False

isDelta plane -- False
isDelta (Plane Delta) -- True

Recursive Functions

fib 0 = 0 -- base case
fib 1 = 1 -- base case
fib x = fib (x - 1) + fib (x - 2)

Higher-order Functions

map _ [] = [] -- base case
map f (x:xs) = f x : map f xs

filter _ [] = []
filter f (x:xs)
  | f x       = x : (filter f xs)
  | otherwise = filter f xs

Point-free Functions

-- these are the same
upperFirstFive xs = map toUpper (take 5 xs)
upperFirstFive = map toUpper . take 5 -- no declared args

Useful Operators

$ :: (a -> b) -> a -> b f $ a = f a

-- these are the same
foo (bar 10)
foo $ bar 10

& :: a -> (a -> b) -> b a & f = f a

map toUpper "abcdefg" & take 3 --- "ABC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment