Skip to content

Instantly share code, notes, and snippets.

@disolovyov
Created October 25, 2012 06:43
Show Gist options
  • Select an option

  • Save disolovyov/3950899 to your computer and use it in GitHub Desktop.

Select an option

Save disolovyov/3950899 to your computer and use it in GitHub Desktop.
Resources for FP class @ 8-bit Panda

Functional Programming

Below is the material for lectures and sessions we've had so far, crammed into one file. When only concrete parts of a book chapter are required for understading, it is marked as such in parenthesis. Which is not to say it is forbidden to read whole chapters and learn new things outside of class.

Lecture: Essential Ideas

  • Functional programming
  • First-class functions (essence)
  • Higher-order functions (essence)
  • Currying (essence)

Resources

Session: Haskell as a Calculator

  • Function application
  • Prefix and infix notation
  • Disambiguating unary operations
  • Logical and comparative operators
  • Basic data types and literals
  • Operator precedence
  • Defining functions and operators

Resources

Russian Freshmen, A HINT!

Not related to Haskell specifically, but to programming in general.

Brush up your English. Don't gather all of your knowledge from a Russian resource like the Russian Wikipedia or “Хабрахабр”. A good chunk of that material are incorrectly-translated English articles. Fear being misinformed, read the original.

Lecture: Types

  • Types
  • Type systems
  • Type classes
  • Parametric polymorphism
  • Lists and operations on lists
  • Ranges
  • Tuples

Resources

Hoogle

Try thinking about what you could do with the types you already know. Think of some functions and define their signatures. Search for functions with such signatures in Hoogle and try them in GHCi.

For example, I want to get an item from a list by its index. For an item of any type a I need the list [a] itself and the integer index Int. The signature [a] -> Int -> a, when searched, yields the (!!) operator.

Implicit Type Conversion

We've talked about some languages that implicitly change types of things when an unobvious operation happens. Examples include JavaScript and PHP. Try two answer both questions:

  • How can this turn out to be a hindrance?
  • On the other hand, when would this be convenient?

Session: Using Lists and HOFs

  • Function application
  • Higher-order functions
  • Coditional expressions
  • Guards

Resources

At this point, we've diverged from both Learn You a Haskell and Real World Haskell books that have served as our primary resources thus far. You can, however, continue reading both these books at your own pace, if you're lacking information on the topics covered.

Functions we used during the session (in alphabetical order): ($), (*), (<=), (==), elem, filter, map, mod, not, null, odd, otherwise, reverse, show, zip, zipWith. They all can be searched for by name on Hoogle.

The important ones are map and filter, of course. These are higher-order functions, and you should make sure you understand their purpose.

Bizz Buzz

There's a drinking game called Bizz Buzz, the rules for which are explained on Wikipedia. It is also used as a simple programming task.

You can teach the computer to play a perfect Bizz Buzz game by making a function that takes a number and returns a list of that much consecutive Bizz Buzz rounds:

bizzbuzz :: Num a => a -> [String]

Bonus points for using guards in your solution.

Lecture: Recursion and Pattern Matching

  • Lambda expressions
  • Name binding
  • Pattern matching
  • Folding
  • Recursion

Resources

Folding

Here is the corrected fold implementation we did, which is actually a right fold (or foldr):

fold' :: (a -> b -> b) -> b -> [a] -> b
fold' f init [] = init
fold' f init (x:xs) = f x (fold' f init xs)

There's a bit of needless repetition here. How would you refactor this code to be shorter and more readable?

GHC.List

You can take a peek at the source code of the recursive functions you've seen and used. Most of them can be found in the GHC.List module (already present in the Prelude).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment