CS 330: Lecture 30 – Haskell IO Continued
Dear students, Last time we introduced ourselves to Haskell’s world of purity and its world of side effects. We learned that any value that gets created in the world of side effects is packaged up in an IO wrapper using the return function. We can unpackage such wrappers using the <- operator. We will finish […]
CS 330: Lecture 29 – Haskell IO
Dear students, Up till this point we haven’t written any standalone programs—none that get input from the user, none that generate output. We have looked at the purely functional side of Haskell, where time does not exist. Functions always produce the same value given the same inputs. No matter how many times you call it. […]
CS 330: Lecture 28 – Filter, Map, and Fold
Dear students: Last time we talked about lambdas and higher-order functions. We wrote this count method: count :: (a -> Bool) -> [a] -> Int count _ [] = 0 count predicate (first : rest) | predicate first = 1 + count predicate rest | otherwise = count predicate rest Let’s start with a Program […]
CS 330: Lecture 27 – Lambdas and Higher-Order Functions
Dear students: Let’s start with a Program This: Using composition and point-free style, write a function that yields the square of the elements’ sum. Using sum and ^. It might help to start without composition and point-free style. Here’s my solution. squaredSum :: Num a => [a] -> a squaredSum list = sum list ^ […]
CS 330: Lecture 26 – Ways of Making Functions: Composition and Lambdas
Dear students: There are at least four ways of defining functions in Haskell: by defining a named abstraction of some algorithm by partially applying parameters to an existing function to get a new function expecting only the remaining parameters by composing a gauntlet of functions together by defining unnamed lambdas We’ve seen the first two. […]
CS 330: Lecture 25 – Ways of Making Functions: Composition
Dear students: Let’s warm back up to Haskell by writing some functions to review the ideas we’ve seen in Haskell so far. Here’s our first: Given three points in 2D space, find a fourth which forms a parallelogram. There are many possible answers to this. The trick is to find the vector between two of […]
CS 330: Lecture 24 – Local Variables, Lists, and Pattern Matching
Let’s write a function. Turn to a neighbor and discuss how you’d solve this: You are given two points. On the perimeter of what circle do both points lie? Write things down and draw pictures to help your thinking. A very natural thing to do is start off with something like this: circletween :: Double […]
CS 330: Lecture 23 – Haskell
Dear students, This class has hit upon two overall themes so far this semester. The first theme was the recognition and interpretation of a programming language. The second theme was types, which brought with it ideas about object orientation and static vs. dynamic decision-making. The third theme of the semester is functional programming, which has […]
CS 330: Lecture 22 – Templates
Dear students, The most common application of templates is to create reusable collections. But in C++ we can also pass the compiler a value to help it specialize the templated pattern. For example, suppose we want lightning fast multiplication. Let’s take a first stab, which is anything but lightning fast: int multiply(int a, int n) […]
CS 330: Lecture 21 – Parametric Polymorphism
Dear students, Don’t get me started on arrays in C. What sort of world is this where an array is fixed in size, but yet it doesn’t know what that size is? We must fix this. Let’s create a growable array: class Flexray { public: Flexray() : capacity(10), nitems(0), elements(new int[capacity]) { } ~Flexray() { […]