# Haskell Basics --- CS 135 // 2021-02-04 ## Administrivia - Announcements go here? - Poll question: ## Questionnaire - Will the final exam cover content from the midterm presentations? - What made you want to be a CS professor? - What sparked your interest in DNA nanotechnology? ## Haskell Dev Environment - Haskell is easiest to use on a "nix" environment + i.e., Linux or MacOS - You all have access to a "CS Desktop" environment already configured to use Haskell + You can access instructions on how to access it under the Resources page on the course website ## Haskell Dev Environment - If you'd like to set up a Haskell environment on your personal machine, you will need to: 1. Install the [Haskell Platform](https://www.haskell.org/platform/) 2. Install [Visual Studio Code](https://code.visualstudio.com/) 3. Install the [Haskell extension](https://marketplace.visualstudio.com/items?itemName=haskell.haskell) for VSC - Note that this is an especially painful process for Windows users ## Haskell Dev Environment - Initially, I recommend using the CS Desktop environment since it is pre-configured for you - There is also a `runTests` command built-in to check your lab solutions before you submit on codePost ## CS Desktop VDI - Here are [instructions](../resources/accessing-the-cs-desktop-vdi) for accessing the CS Desktop - Most reliable to access while on-campus, but there are also instructions for accessing it via off-campus Note: Demo the CS Desktop, VSC, and Haskell # Imperative VS Functional ## Imperative Programming - Languages like Python, Java, and C are **imperative** - Imperative programs consist of a sequence of **statements** that mutate the global state of a program over time ```python win = GraphWin() shape = Rectangle(Point(0, 0), Point(1, 1)) shape.setFill("red") shape.draw(win) ``` ## Imperative Programming - Imperative programs use a lot of "side effects" - A **side effect** is when a function performs an action other than returning a value + `shape.setFill("red")` + `s = input("Enter a value: ")` + `print("Hello, world!")` + `img.save("cat.png")` + `x = x + 1` ## Imperative Programming - **Question:** Are side effects a bad thing? Note: Errors, race conditions, parallelism ## Functional Programming - Functional programs typically avoid side effects entirely and compute by evaluating **expressions** - A function is **pure** if it has no side effects ```py # Even though this is Python, this function is "pure" def factorial(n): return 1 if n == 0 else n * factorial(n-1) ``` - Pure functions are easy to reason about, since they always work the same way - They are also **safe** to run in parallel since they only depend on their arguments and nothing else ## Functional Programming - Pure functional programming languages include: + Haskell, Agda, PureScript, and many more + These languages **enforce** avoiding side effects - Impure functional programming languages include: + C++, C#, Java, Python, and many more + These languages support the functional programming style, but do not have any safety guarantees # Haskell ## Features of Haskell - Functions are "first class" and can be passed as arguments to other functions ```haskell filter even [0, 1, 2, 3, 4, 5] ``` - Anonymous functions (lambda expressions) ```haskell filter (\x -> x `mod` 3 == 0) [1, 2, 3, 4, 5, 6] ``` - Partial application (currying) ```haskell filter (<4) [1, 2, 3, 4, 5, 6] ``` ## Features of Haskell - Pattern matching ```py # Python def sum(numbers): if numbers == []: return 0 else: return numbers[0] + sum(numbers) ``` ```haskell -- Haskell sum :: Num a => [a] -> [a] sum [] = 0 sum (x:xs) = x + sum xs ``` - List comprehensions (similar to Python) ```haskell pairs = [(x,y) | x <- [1,2,3] , y <- ["abc", "def"] ] ``` ## Features of Haskell - Haskell is **lazy**, which means that it only evaluates expressions when it absolutely needs to + This helps with performance + Also allows us to define infinite data structures ```haskell evens = filter even [0..] ``` ```haskell primes = filterPrime [2..] where filterPrime (p:xs) = p : filterPrime [x | x <- xs, x `mod` p /= 0] ``` ```haskell fibonacci = fib 0 1 where fib prev next = next : fib next (prev + next) ``` ## A Simple Haskell Program ```haskell module Gold where -- The golden ratio phi :: Double phi = (sqrt 5 + 1) / 2 polynomial :: Double -> Double polynomial x = x^2 - x - 1 f x = polynomial (polynomial x) main = do print (polynomial phi) print (f phi) ``` Note: Top-level expressions should always have types, talk briefly about the main function and monads ## GHCi - GHCi is an interpreter for Haskell - Great for debugging your code - Type `:quit` to quit GHCi - Type `:type exp` to check the type of `exp` - Type `:reload` to reload your modules - Type `:info exp` to get information on `exp` # Self Checks ## Lab Time - You will be sent into a breakout session with your partner for this week - Start by checking if you both have access to the CS Desktop VDI, following [Accessing the CS Desktop VDI](/teaching/2021s/cs135/resources/accessing-the-cs-desktop-vdi) - Then move start working on the `Lab1.hs` exercises - Nothing to turn in today! + We will continue this lab on Tuesday