Haskell

Installed Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install Haskell on Mac

brew cask install haskell-platform

To run the interpreter use ghci

60/2
30.0 
60`div`2  =30   <-- This is infix form. Requires back ticks.
45`mod`3 = 0

Backquotes are needed for Integer operations. The value it stores the previous expression.

Negatives have to be in parentheses.

Prelude> mod 45 6 = 3 <-- The method call to mod requires no parentheses.

Prelude has a bunch of pre-loaded libraries. Including sqrt, sin, even, &&, gcd.

There are no variables. Just constant declarations which end up working as variables.

let <id> = <value>
<id> = <value>
if (even x) then "even" else "odd"

 List types with ++ to concat

Prelude> [5,6,7]++[5]
[5,6,7,5]

To add to the front of a list use the : cons operator

Prelude> 5:6:7:[7,8,0]
[5,6,7,7,8,0]

List operations: head, tail, length, reverse, take, drop, maximum, minimum, sum

Generate lists with dots.. Below this takes the first two numbers and finds their difference.

The range [5, 9 .. 20] is the numbers between 5 and 20 of the form (5+4n).

Modules and running files

To load a single file you can run

:l filename.hs

Reloading that file just requires

:r

To compile and run more than one file with modules you use…

runhaskell mainFileName.hs

To make a module you declare one file a module using…

module MyModuleName where

Modules need to have the same filename as the module name. So MyModuleName.hs would contain the module declaration line above. Other than that detail it is the same as any other .hs file. To import the file you simply add import MyModuleName to the top of the file that needs it.

import MyModuleName
rest of file...

IO with show and read

look to lab12.hs

Pure sections don’t use IO so they work the same way regardless of when they execute. Impure sections of code depend on IO operations like show and read.

show turns objects into strings

read turns strings into objects and you can declare the expected type.