CS 330 Lecture 23 – Functions in Haskell
Agenda
- what ?s
- what we mean by functional
- functions are first-class
- immutability
- expressions everywhere
- functions in Haskell
- coping with looplessness
- guards to case statements to pattern matching
TODO
- Watch Erik Meijer discuss list comprehensions in both Haskell and Microsoft’s LINQ in the video below. On a 1/4 sheet, generate 3 interesting lists using Haskell’s list comprehensions.
Note
Today we dig deeper into functional programming using the Haskell language. We’ll write a handful of functions together, learn to deal with looplessness, and see some syntactic sugar called pattern matching that tends to make functions short and elegant.
Code
functions.hs
import Data.Char
initialize :: String -> String -> String -> String
-- initialize first middle last = head first : (head middle : (head last : []))
initialize first middle last = head first : head middle : head last : []
initialize' first middle last = [f, m, l]
where
f = head first
m = head middle
l = head last
capitalize :: String -> String
capitalize s =
if s == "" then
""
else
toUpper (head s) : capitalize (tail s)