teaching machines

CS 330 Lecture 23 – Functions in Haskell

April 1, 2016 by . Filed under cs330, lectures, spring 2016.

Agenda

TODO

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)