teaching machines

CS 330 Lecture 22 – Tuples, List Comprehensions, and Thinking Looplessly

March 30, 2013 by . Filed under cs330, lectures, spring 2013, Uncategorized.

Agenda

Code

test.hs

swap' (a, b) = (b, a)

initials f m l = head f:head m:head l:[]
-- initials f m l = head f:(head m:(head l:[])) 

-- sum' loi = 
  -- case loi of [] -> 0 
              -- _  -> head loi + sum' (tail loi) 
              --
sum' [] = 0
sum' (first:rest) = first + sum' rest

head' [] = error "Eek!"
head' (first:_) = first

length' [] = 0
length' (_:rest) = 1 + length' rest

product' [] = 1
product' (first:rest) = first * product' rest

-- factorial' 0 = 1 
-- factorial' n = n * factorial' (n - 1) 
factorial' n = product' [1..n]

isEmptyList [] = True
-- isEmptyList (_:_) = False 
isEmptyList _ = False

Haiku

“Can I cons with you?”
Hundred times asked, same answer:
“No, you’re not my type.”