Dear students,

Today we solve more problems in Haskell together. Along the way we will discover some of the peculiarities of functional programming, like the supremacy of linked lists.

First, an exercise to complete with a neighbor:

- ParallelogramWrite a function
`pcomplete`

that completes a parallelogram. Suppose you are given three points in 2D space. Compute a fourth that when combined with the others yields a paralellogram. Recall this common structure for defining functions:f p1 p2 = some-expr where l1 = some-expr l2 = some-expr

Pattern matching is encouraged.

And here are the problems we’ll solve:

- Let’s write a parallelogram completer. Suppose you are given three points. Find a fourth that joins up with the three to yield a parallelogram.
parallelogram (x1, y1) (x2, y2) (x3, y3) = (x, y) where x = (x2 + x3 - x1) y = (y2 + y3 - y1)

- Let’s write a function to generate initials for someone with three names. Not everyone has three names, so this function probably will disenfranchise them. If you are designing a website, consider providing just one textbox for the name. We could write it like this:
initials first middle last = f : m : l : [] where f = head first m = head middle l = head last

But suppose we wanted to force the letters to lowercase.`Data.Char.toLower`

to the rescue:initials first middle last = f : m : l : [] where f = toLower (head first) m = toLower (head middle) l = toLower (head last)

We used the parentheses to force associativity. Parentheses are not our only option in Haskell. Anytime we have code likef (expr p1 p2 p3)

we can rewrite it asf $ expr p1 p2 p3

Consider this expression, which is a silly way of extracting out the digit in the 10s place:head (tail (reverse (show 9876)))

That’s more parentheses that we might like to write. We can use the`$`

to simplify:head $ tail $ reverse $ show 9876

In the case of`initials`

, we could writeinitials first middle last = f : m : l : [] where f = toLower $ head first m = toLower $ head middle l = toLower $ head last

Still, this is a lot of repetition. We’re apply`toLower`

in the same way to each letter. If we need to apply the same operation to every element of a collection, what can we do? We have two options. One is called`map`

and we will discuss in great detail later. The other way you read about for today: a list comprehension. Let’s bundle the letters up in a list, and then generate a new list from that:initials first middle last = [toLower c | c <- fml] where f = toLower $ head first m = toLower $ head middle l = toLower $ head last fml = f : m : l : []

- What do you think? Are there more integer numbers than there are even numbers? John Green wrote this in
*The Fault in Our Stars*:There are infinite numbers between 0 and 1. There’s .1 and .12 and .112 and an infinite collection of others. Of course, there is a bigger infinite set of numbers between 0 and 2, or between 0 and a million. Some infinities are bigger than other infinities. A writer we used to like taught us that. There are days, many of them, when I resent the size of my unbounded set. I want more numbers than I’m likely to get, and God, I want more numbers for Augustus Waters than he got. But, Gus, my love, I cannot tell you how thankful I am for our little infinity. I wouldn’t trade it for the world. You gave me a forever within the numbered days, and I’m grateful.

Early 1900s mathematician Georg Cantor said that infinity indeed had levels, but two infinite sets were at the same level if there was a one-to-one mapping between them. Is there a relationship between the integers and the even numbers? If we can write a list comprehension to turn one into the other, than they live in the same level. And we can:evens upto = [2 * n | n <- [0..upto]]

We stick with the positives. Speaking of infinities, we can actually produce infinite things in Haskell:evens = [2 * n | n <- [0..]]

This only works because Haskell doesn’t evaluate most expressions until the value is absolutely needed. It is*lazy*. We must careful to never need everything. We can use`take`

to specify how many evens we need:take $ evens 100

See you next time!

Sincerely,

- December 2017
- November 2017
- October 2017
- September 2017
- August 2017
- July 2017
- May 2017
- April 2017
- March 2017
- February 2017
- January 2017
- December 2016
- November 2016
- October 2016
- September 2016
- August 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- June 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- December 2014
- November 2014
- October 2014
- September 2014
- August 2014
- July 2014
- June 2014
- May 2014
- April 2014
- March 2014
- February 2014
- January 2014
- December 2013
- November 2013
- October 2013
- September 2013
- August 2013
- July 2013
- June 2013
- May 2013
- April 2013
- March 2013
- February 2013
- January 2013
- December 2012
- November 2012
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- July 2011
- January 2011

- 3d
- algorithms
- aphasia
- art
- blackbox
- blender
- buster
- code
- courses
- cs1
- cs145
- cs148
- cs245
- cs318
- cs330
- cs352
- cs396
- cs436
- cs455
- cs491 mobile
- eclipse
- exams
- experiments
- failures
- fall 2011
- fall 2012
- fall 2013
- fall 2014
- fall 2015
- fall 2016
- fall 2017
- fish vision
- gallery
- gamedev for learning
- gamedev_cse
- gamedev2
- gamedev3
- games
- git
- graphics
- honors 304.503
- honors gamedev
- howto
- keystrokes
- labs
- lectures
- madeup
- meta
- mobile
- mutt
- nature
- nbsn
- outreach
- pop computer science
- postmortems
- programming languages
- public
- ratorvaders
- reader animated
- reading
- research
- responses
- semesters
- software
- specchecker
- specifications
- spring 2012
- spring 2013
- spring 2014
- spring 2015
- spring 2016
- spring 2017
- talks
- teaching
- travel
- Uncategorized
- unity
- vim
- writing
- wrong number
- www

## Leave a Reply