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

Agenda

  • what ?s
  • logo
  • tuples
  • list comprehensions
  • initials
  • head’
  • pattern matching
  • case vs. function definition patterns
  • sum’
  • product’
  • factorial’
  • reverse’

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.”

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *