CS 330 Lecture 32 – Taming Recursion
Dear students, We’ve seen that Haskell does some pretty crazy things: it infers types, it supports this crazy pattern matching stuff, it disallows side effects, and it computes things lazily. Let’s look at one last crazy thing it does: it uses recursion for everything! There are no loop control structures. Normally, when you think of […]
CS 330 Lecture 31 – Call-by-name and Call-by-need
Dear students, Last time we introduced a different way of passing parameters. Instead of eagerly evaluating them, we delayed evaluation until they were referenced inside the function. You are probably thinking, “Call-by-name. Big deal. How many new control structures am I going to need in my life?” Probably not many, but I think the bigger […]
CS 330 Lecture 30 – Call By Name
Dear students, When you call a function, what happens? In particular, what happens in this code? int m() { cout << "m" << std::endl; return 5; } int r() { cout << "r" << std::endl; return 7; } int multiply(int a, int b) { cout << "*" << std::endl; return a * b; } int […]
CS 330 Lecture 29 – Haskell IO Cont’d
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 28 – Haskell IO
Dear students, There are a few ideas we didn’t get to last time. Let’s touch upon those before diving into a discussion of Haskell I/O. First, what about the type signature for fold? Let’s reason it out. We know it will have four parts: three parameters and a return value. fold :: _ -> _ […]
CS 330 Lecture 27 – Fold
Dear students, Let’s start with a Program This! Complete two of the following functions based on your row number. All of them accept a list. Solve them using the pattern-matching style, with base cases and general cases defined separately. All of them should be two-liners. upped, which generates an uppercase version of the incoming String […]
CS 330 Lecture 26 – HOFs Elsewhere and Closures
Dear students, Let’s start with a Program This! Write the necessary Java 8 code to filter a List. Make the code as general as possible, allowing its clients to supply their own filtering criteria. Here’s my solution. To leave a hole in the filtering algorithm, we must leave a parameter for the predicate. We’ll use […]
CS 330 Lecture 25 – Filter, Map, and Foreach
Dear students, Let’s start with a Program This: Write in Haskell a function count that accepts a predicate and a list. It yields the number of elements for which the predicate is true. We’d write it like is in an imperative way: count = 0 for each element in list if predicate is true of […]
CS 330 Lecture 24 – Lambdas
Dear students, There are at least four ways of defining functions in Haskell: by defining a named function by partially applying parameters to get a new function expecting only the remaining parameters by composing a gauntlet of functions together by defining unnamed lambdas We discussed composition last time. We continue that today, but we also […]
CS 330 Lecture 23 – Ways of Making Functions: Composition
Dear students, There are at least four ways of defining functions in Haskell: by defining a named function by partially applying parameters 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. Now let’s turn to composition. You’ve almost […]