programming language.
(Haskell Basics by Brent Yorgey)
⇓
⇓
designed by a committee of CS academics
in the late 80’s
to create a standard lazy
functional programming language
Haskell package build system
$ ghc Setup
$ ./Setup configure
$ ./Setup build
$ ./Setup install
like Python’s pip
source repository of open source Haskell packages
User package manager for Hackage packages
eg to install pandoc
$ cabal install pandoc
resolves and builds dependencies
::
= “has type”
True :: Bool
1 :: Int -- machine integers
2^2^2^2^2 -1 :: Integer -- arbitrary precision
'a' :: Char
"Hello!" :: String
1.0 :: Double
() :: ()
add :: Integer -> Integer -> Integer
add m n = m + n
add 3 2 ⟹ 5 :: Integer
-- partial application
add 5 :: Integer -> Integer
prefix vs infix
2 + 4 ⟹ 6
-- prefix
(+) 2 4 ⟹ 6
div 4 2 ⟹ 2
-- infix
4 `div` 2 ⟹ 2
[a]
is a list of elements of type a
The type variable a
is an example of polymorphism.
-- empty list
[] :: [a]
[True, False, True] :: [Bool]
[1..5] :: [Int]
["Haskell", "is", "good"] :: [String]
Note ['a', 1, True]
is not a valid Haskell list!
(++) :: [a] -> [a] -> [a]
[1, 2] ++ [3, 4] ⟹ [1, 2, 3, 4]
head :: [a] -> a
tail :: [a] -> [a]
length :: [a] -> Integer
head [1, 2, 3] ⟹ 1
tail [1, 2, 3] ⟹ [2, 3]
length [1, 2, 3] ⟹ 3
length $ show $ 2^2^2^2^2 -1 ⟹ 19729
a : as
1 : [2, 3] ⟹ [1, 2, 3]
map :: (a -> b) -> [a] -> [b]
map (add 3) [1, 2, 3] ⟹ [4, 5, 6]
Lazy evaluation allows infinite lists:
naturals = [0..]
Fibonacci series
fib :: Int -> Int
-- naive definition
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
let x = 2 in
x*x + 3*x + 1
sumsquare x y = square x + square y
where
square u = u * u
squares = [ x * x | x <- [0..] ]
-- "nested 'for' loop"
pairs = [(x,y) | x <- [1..10], y <- [1..10]]
Python’s comprehensions come from Haskell
primes = filterPrime [2..]
where filterPrime (p:xs) =
p : filterPrime [x | x <- xs, x `mod` p /= 0]
(from http://haskell.org)
(a, b)
is a pair(a, b ,c)
is a triple("Life", 42) :: (String, Int)
(False, 'a', 1.1) :: (Bool, Char, Float)
take :: Int -> [a] -> [a]
take 0 _ = []
take _ [] = []
take n (x:xs) = x : take (n-1) xs
data Arch = X86 | X86_64 | ARMv7
case arch of
X86 -> "/usr/lib"
X86_64 -> "/usr/lib64"
otherwise -> "/usr/lib"
vs
type annotations are generally optional
data Maybe a = Just a | Nothing
data Bool = False | True
-- record types
data Point = Pt Float Float
pt = Pt 1.0 2.0
-- recursive datatype
data Tree a = Leaf a | Branch (Tree a) (Tree a)
putStrLn :: String -> IO ()
getLine :: IO String
do
let greet = "Hello"
name <- getLine
putStrLn $ greet ++ " Your name is " ++ name
$ cat > test.hs
main = putStrLn "hello"
$ ghc test.hs
$ ./test
hello
$
in GHC runtime
forkIO :: IO () -> IO ThreadId
https://hackage.haskell.org/package/async
(page1, page2) <- concurrently (getURL url1) (getURL url2)
concurrency control mechanism for shared memory,
analogous to database translations
#!/usr/bin/env runhaskell
-- #!/bin/bash
import Turtle --
--
main = do --
dir <- pwd -- DIR=$(pwd)
time <- datefile dir -- TIME=$(date -r $DIR)
print time -- echo $TIME
markup conversion tool
eg markdown to html, etc
These slides created with pandoc: http://github.com/juhp/presentations
X tiling Window Manager
Stable consistent version subsets of Hackage packages
Build system library to replace Make
accounting
manage files across systems with git
without checking them in
gtk stack bindings
Dependently-typed programming languages
Static website generator
Extensible customizable editor (like Emacs)
Scotty
Warp webserver can
“provide performance on a par with nginx”
and “three times faster than Node”.
Haskell Reddit
beginners
and haskell-cafe
irc: #haskell-beginners and #haskell
Contact: petersen@fedoraproject.org (juhp)