Bookmark and Share

Introduction

  • Haskell is a purely functional programming language.

  • "In purely functional programming you don't tell the computer what to do as such but rather you tell it what stuff is. … You express that in the form of functions. … "

  • A Haskell function cannot have side effects other than mapping its input to an output as a mathematical function.

  • Haskell is lazy in that it does not compute anything until it have to in order to return a value.

  • Haskell is statically typed.

    • Type inference allows deducing the appropriate type automatically by inference on compatibility between the inputs and outputs.

Starting out

Ready, set, go!

  • ghci: The Glorious Glasgow Haskell Compilation System. Environment to interactively compile and run Haskell programs. Exit GHCi using :quit. GHCi documentation.

  • Not-equal operator: /=.

  • Compile-time error on incompatible types: 123 + "foobar"

  • Compile a program: ghci> l:myprogram.hs.

  • ' is allowed in function name, e.g. foo'Bar is a valid function name.

  • Function names cannot begin with a lower-case letter.

An intro to lists

  • In Haskell, a strings is a lists of characters.

  • List and tuple syntax difference: [1, 2], (1, "foobar"). A tuple can contain elements of different types, while a list cannot. This bears some similarity with Python's lists and tuples. A difference is that in Python, lists are mutable but tuples are not.

  • Concatenate operation: [1, 2] ++ [3, 4], "foo" ++ "bar", ['a', 'b'] ++ ['c', 'd'].

  • [https://en.wikipedia.org/wiki/Cons cons] operation ::

      Prelude> 'f' : "bar"
      "fbar"
      Prelude> 1: [3, 4]
      [1,3,4]
      Prelude> 
    
  • If "XXX...XXX" is a very long string, "XXX...XXX" ++ "foobar" will be slow.

  • [1, 2, 3] equals 1 : 2 : 3 : [].

  • Take parts of a list: [1, 3, 5, 7] !! 2 returns 5. Haskell lists uses zero-based numbering. take 2 [1, 3, 5, 7] gives the third element 5. drop 2 [1, 3, 5, 7] gives [1, 3, 7]. Compare this to Mathematica {1,3,5,7}<span class="createlink">3</span>, Take[{1,3,5,7}, {3}], Drop[{1,3,5,7}, {3}].

  • Test element membership in a list: elem 3 [2, 4, 6] and 2 \elem` [1, 3, 5, 7]`.

  • [[1, 2], ["foo", "bar"]] is invalid as the types of the elements of the two sub-lists are not same.

  • Lists compare in lexicographical order. E.g. [1, 3, 4] < [1, 4, 2].

  • head [1, 2, 3], tail [1, 2, 3], init [1, 2, 3], last [1, 2, 3]. Compare with Mathematica / Wolfram Langauge namings: First[{1,2,3}], Rest[{1,2,3}], Most[{1,2,3}], Last[{1,2,3}]

  • null [1, 2, 3], null []

Other side notes

  • One of the key features and advantanges of Haskell is its purity: all side-effects are encapsulated in a monad.

  • [https://en.wikipedia.org/wiki/Algebraic_data type Algebraic data type]:

    data List a = Nil | Cons a (List a)

    data Tree = Empty | Leaf Int | Node Tree Tree

Tree is a sum data type a.k.a. tagged union.

Tree is a recursive data type.

  • Pattern matching in Haskell:

      depth :: Tree -> Int
      depth Empty = 0
      depth (Leaf n) = 1
      depth (Node l r) = 1 + max (depth l) (depth r)
    

References

  • https://ghcformacosx.github.io/

  • Search a Haskell function by desired type signature, e.g. https://www.haskell.org/hoogle/?hoogle=String+-%3E+Int (And the desired function for turning an object to a String is possibly read :: Read a => String -> a).

blog comments powered by Disqus