Lecture 03 Getting Started

Joseph Haugh

University of New Mexico

Installing GHC

  • First install GHCup https://www.haskell.org/ghcup/
  • While installing make sure you install stack, cabal and HLS (Haskell Language Server) if you want IDE like support in your editor
  • That’s it!

Using GHC

  • Now start up ghci (interactive interpreter) by typing “ghci” in your terminal
  • Once within ghci you can evaluate Haskell expressions!
  • Let’s try it!

Standard Prelude

  • Haskell comes with many builtin functions which you are always welcome to use
  • The prelude can be found here: prelude
  • Provides many useful list functions (some of which we will redefine)

Exploring The Prelude: Numeric Functions

  • Addition:

    ghci> 1 + 2
    3
  • Exponentiation:

    ghci> 3 ^ 2
    9
  • Integer Division (note the backticks, shift ~):

    ghci> 5 ‘div‘ 2
    2

Exploring The Prelude: List Functions

  • Get the first element of a list:

    ghci> head [1, 2, 3]
    1
  • Get everything but the first element of a list:

    ghci> tail [1, 2, 3]
    [2, 3]
  • Get the nth element of a list:

    ghci> [1, 2, 3] !! 2
    3

Exploring The Prelude: List Functions Cont.

  • Check if a list contains an element:

    ghci> elem 3 [1, 2, 4] 
    False
  • Calculate the length of a list:

    ghci> length [1, 2, 3]
    3
  • Reverse a list:

    ghci> reverse [1, 2, 3]
    [3, 2, 1]

Exploring The Prelude: List Functions Cont.

  • Drop the first n elements from a list:

    ghci> drop 2 [1, 2, 3]
    [3]
  • Appending two lists:

    ghci> [1, 2] ++ [3, 4]
    [1, 2, 3, 4]
  • Sum a list:

    ghci> sum [1, 2, 3]
    6

Function Application Java

Given the following Java Function:

public static int foo(int x, int y) {
    return x + (y * 2);
}

You apply it to a set of arguments as follows:

foo(2, 3)

Thus, function application in Java is done with parentheses.

Function Application Haskell

Instead of parentheses Haskell uses a space to denote function application.

For example here is the same function written in Haskell:

foo x y = x + (y * 2)

And here is that function being applied:

foo 2 3

Function Application Haskell Precedent

Function application has a higher priority than any other operator.

This means it will always happen first, for example what does the following expression evaluate to?

foo 2 3 * 3

Creating A Haskell File

  • Haskell files use the .hs suffix
  • General workflow:
    • Make edits in the Haskell file
    • Save
    • Open terminal with ghci and reload Haskell file
    • Fix errors if necessary
    • Test code
    • Repeat

Loading A Haskell File

  • The easiest way to load in a Haskell file to ghci is to run the following in a terminal: “ghci foo.hs”
  • Then after you make changes you can reload by typing “:reload” or “:r” inside ghci
  • While inside ghci you have access to the prelude as well as functions defined within your file
  • See this link for more details about ghci: ghci user guide

Naming Requirements and Layout

  • Functions and arguments must begin with a lower-case letter
  • By convention, names that represent a list end with an ’s’
  • Sequences of definitions must all be in the same column

Exercise: Feet to Meters

Define a Haskell function, feet2Meters, which converts a given number of feet to meters. The conversion rate is 1 foot to 0.3048 meters.

For example a function to convert feet to inches could be defined as follows:

feet2Inches ft = ft * 12