Haskell


Notes for learning Haskell. See also [[Learn Haskell by Building a Blog Generator]]? and [[Thinking Functionally with Haskell]]?.

Project Ideas

  • Write a Brainfuck interpreter in Haskell
  • Write a tool like LambdaCalc that interprets lambda calculus expressions. It would take a source file, one with a file containing substitutions and a main source file, and would output the reduced lambda calculus expression. Would be fun for learning more about lambda calculus and Haskell at the same time.

Exercism

and and any pattern

Need to check if every character is uppercase and there is at least one uppercase letter?

import Data.Char (isUpper)

all isUpper input && any isUpper input

Check for null at the top

Because of lazy evaluation, checking for cases like the input being empty at the top of a function definition means that you don’t have to worry about issues later on down the line, like last not working.

Left and Right

By convention, in functions where there can be a success or a failure:

  • Left is used for failure
  • Right is used for success

This is the other way around from Go!

Traverse

There’s a lot of stuff on the internet about “monads” and “applicatives” that I don’t understand yet related to traverse. In its use I’ve found so far, it lets you map over a list and return a new list if it’s successful on everything or return something else if it fails on any element.

Map.FromListWith

Creates a map from a list of (key, value) pairs and with list items with the same key being combined by a function a -> a -> a.

When working with splitting up a string

First of all, Haskell doesn’t have a function to split input up by a delimiter other than space (words). Second of all, it’s often a good idea to think “how could I change the input so that it’s easier to work with?”. This happened with my implementation of an abbreviation function which was super long-winded, and then I looked at the community solutions and then all modified the input text to make it much easier to work with.




Related posts