Haskell Intro Lab
Haskell is a functional language, which means that the fundamental computational paradigm consists
of functions that provide mappings from inputs to outputs:
Functional programs consist of many smaller functions that are chained or composed. Iteration is
accomplished using recursion. To effectively use Haskell, you must shift away from thinking about how
the machine should solve your problem (as you had to with procedural programming) and focus on
defining the solution to the problem in a mathematically rigorous way.
In Haskell, variable bindings are permanent and all types are statically bound at compile time. Haskell
programs are stateless, and I/O is accomplished using special wrappers to encapsulate the side effects.
Functions are evaluated using lazy (or non-strict) evaluation.
In this lab, we will use ghci, which is an interactive interpreter for Haskell (similar to irb for Ruby).
Because of the lack of traditional procedural I/O in Haskell, we will concentrate on writing functions
rather than programs.
Few example programs in Haskell to get you started:
1. Start ghci and get the prompt Prelude>. When you need to, you can exit ghci by entering the
end-of-file keystroke Control-D at the prompt.
2. Try evaluating various Haskell expressions, such as:
map (\x -> x / 2) [2.25, 3.5, 4.5, 7.2, 6.5]
map (\x -> 2 / x) [2.25, 3.5, 4.5, 7.2, 6.5]
map (/ 2) [2.25, 3.5, 4.5, 7.2, 6.5]
map (2 /) [2.25, 3.5, 4.5, 7.2, 6.5]
filter (\x -> x > 5) [2.25, 3.5, 4.5, 7.2, 6.5]
filter (> 5) [2.25, 3.5, 4.5, 7.2, 6.5]
filter (> "Fun") ["C", "Fortran", "Haskell", "ML"]
reverse [1,2,3,4,5]
reverse "Haskell"
show 17
show [1,2,3,4]
reverse (show [1,2,3,4])
show (reverse [1,2,3,4])
[22..47]
take 15 [22..47]
drop 15 [22..47]
1 : [2,3,4]
['h','e','l','l','o']
'h' : "ello"
"Hello" ++ " " ++ "World"
[1,2,3,4] ++ [40,50,60,70]
[2,3,4] ++ [1]
import Data.List
partition (> 5) [2.25, 3.5, 4.5, 7.2, 6.5]
partition (> "Fun") ["C", "Fortran", "Haskell", "ML"]
sort [80, 38, 23, 40, 70, 24, 8, 92, 78, 80, 42, 19, 95, 87, 4]
sort "haskell = HASKELL"
Make sure you understand the difference between these lines:
reverse "ABC"
:type reverse "ABC"
reverse
:type reverse
Programs:
1. Hello World
A simple program to print "Hello, World!" to the console.
main :: IO ()
main = putStrLn "Hello, World!"
2. Factorial Function
A recursive function to calculate the factorial of a number.
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n - 1)
main :: IO ()
main = print (factorial 5) -- Output: 120
3. Fibonacci Sequence
A function to generate the nth Fibonacci number.
fibonacci :: Integer -> Integer
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n = fibonacci (n - 1) + fibonacci (n - 2)
main :: IO ()
main = print (fibonacci 10) -- Output: 55
4. List Comprehension
A program to generate a list of squares of the first 10 natural numbers.
squares :: [Integer]
squares = [x * x | x <- [1..10]]
main :: IO ()
main = print squares -- Output: [1,4,9,16,25,36,49,64,81,100]
5. QuickSort Algorithm
An implementation of the QuickSort algorithm.
quickSort :: (Ord a) => [a] -> [a]
quickSort [] = []
quickSort (x:xs) = quickSort [y | y <- xs, y <= x] ++ [x] ++ quickSort [y | y <- xs, y > x]
main :: IO ()
main = print (quickSort [3,1,4,1,5,9,2,6,5,3,5]) -- Output: [1,1,2,3,3,4,5,5,5,6,9]
6. Write a list comprehension that produces all the numbers between 1 and 100 that are multiples
of 2, 3, and 5.
7. Write a list comprehension to generate all the pairs of numbers between 1 and 20 that add up to
20.