0% found this document useful (0 votes)
38 views

CSC 398 Functional Programming: Instructor: Dr. Hao Wu Lecture 2 Haskell and Functional Programming

This document discusses Haskell and functional programming. It covers: - Features of Haskell like its advanced type system and suitability for writing languages - Resources for learning Haskell including websites and tutorials - Instructions for installing Haskell on Mac and Windows, including using ghcup and chocolatey - An example of a simple "Hello World" Haskell program and how to run/compile it.

Uploaded by

Cleo Brown
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

CSC 398 Functional Programming: Instructor: Dr. Hao Wu Lecture 2 Haskell and Functional Programming

This document discusses Haskell and functional programming. It covers: - Features of Haskell like its advanced type system and suitability for writing languages - Resources for learning Haskell including websites and tutorials - Instructions for installing Haskell on Mac and Windows, including using ghcup and chocolatey - An example of a simple "Hello World" Haskell program and how to run/compile it.

Uploaded by

Cleo Brown
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

CSC 398 Functional

Programming
Instructor: Dr. Hao W

Lecture 2 Haskell and Functional Programming


u

Features of Haskell

• It’s an advanced higher order functional language

• Has a very modern, concise syntax

• Has automatic type inference with parametric polymorphism

• Used a lot in research, and extremely influential in the


development of other languages

• Extremely well suited for writing languages.

2 Hao Wu, CSC 398 Functional Programming Week 1


.

Resources

• Haskell Website
https://www.haskell.org/

• Tutorials and Resource


https://www.haskell.org/documentation/

• Downloads and Installation Guid


https://www.haskell.org/downloads/

3 Hao Wu, CSC 398 Functional Programming Week 1


:

Installation

• What do you need


- The Glasgow Haskell Compiler, a state-of-the-art, open source
compiler and interactive environment for Haskel
• How to get it
- MacOS:
• https://www.haskell.org/ghcup/
- Windows
• https://www.haskell.org/downloads/#windows

4 Hao Wu, CSC 398 Functional Programming Week 1


Installation on Mac
Open your Terminal and enter the following command (You need to follow the instructions
to install all necessary packages ):
cschxw071168:~ wuh2$ curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh

Welcome to Haskell!

This script will download and install the following binaries:


* ghcup - The Haskell toolchain installer
(for managing GHC/cabal versions)
* ghc - The Glasgow Haskell Compiler
* cabal - The Cabal build tool

ghcup installs only into the following directory,


which can be removed anytime:
/Users/wuh2/.ghcup

Press ENTER to proceed or ctrl-c to abort.


Note that this script can be re-run at any given time.

% Total % Received % Xferd Average Speed Time Time Time Current


Dload Upload Total Spent Left Speed
100 9133k 100 9133k 0 0 3164k 0 0:00:02 0:00:02 --:--:-- 3163k
[ Info ] Upgrading GHCup...
[ Warn ] No GHCup update available

System requirements
Note: On OS X, in the course of running ghcup you will be given a dialog box to install the command line tools. Accept and the requirements will be
installed for you. You will then need to run the command again.

Press ENTER to proceed or ctrl-c to abort.


Installation may take a while.

[ Info ] downloading: https://downloads.haskell.org/~ghc/8.10.4/ghc-8.10.4-x86_64-apple-darwin.tar.xz


% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 184M 100 184M 0 0 3032k 0 0:01:02 0:01:02 --:--:-- 3069k
[ Info ] verifying digest of: ghc-8.10.4-x86_64-apple-darwin.tar.xz
[ Info ] Unpacking: ghc-8.10.4-x86_64-apple-darwin.tar.xz to /var/folders/d8/yld2b1qs7mn_dh9df0hqgxq5n3tdm9/T/ghcup-RYtltl

5 Hao Wu, CSC 398 Functional Programming Week 1


Installation on Mac

• You may need to run


• source .bashrc

6 Hao Wu, CSC 398 Functional Programming Week 1


:

Installation on Windows

• Configure Chocolatey on your machine:


• https://chocolatey.org/install
• Open your PowerShell with administrative privilege
• Check your Get-ExecutionPolicy

PS C:\WINDOWS\system32> Get-ExecutionPolicy
AllSigned

• If the return value is Restricted, then run


• “Set-ExecutionPolicy AllSigned”

7 HaoWu,
Hao Wu, CSC
CSC 398
398 Functional
FunctionalProgramming
Programming Week 11
Week
Installation on Windows

• Run the following command:


• Set-ExecutionPolicy Bypass -Scope Process -Force;
[System.Net.ServicePointManager]::SecurityProtocol =
[System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object
System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

PS C:\WINDOWS\system32> Set-ExecutionPolicy Bypass -Scope Process -Force;


[System.Net.ServicePointManager]::SecurityProtocol =
[System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object
System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
Forcing web requests to allow TLS v1.2 (Required for requests to Chocolatey.org)
Getting latest version of the Chocolatey package for download.
Not using proxy.
Getting Chocolatey from https://community.chocolatey.org/api/v2/package/chocolatey/0.10.15.
Downloading https://community.chocolatey.org/api/v2/package/chocolatey/0.10.15 to
C:\Users\phili\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip
Not using proxy.

8 HaoWu,
Hao Wu, CSC
CSC 398
398 Functional
FunctionalProgramming
Programming Week 11
Week
Installation on Windows

• Run the following command:


• choco install haskell-dev followed by refreshenv

PS C:\WINDOWS\system32> choco install haskell-dev


Chocolatey v0.10.15
Installing the following packages:
haskell-dev
By installing you accept licenses for the packages.
Progress: Downloading ghc 9.0.1... 100%
Progress: Downloading cabal 3.4.0.0... 100%
Progress: Downloading chocolatey-core.extension 1.3.5.1... 100%
Progress: Downloading haskell-dev 0.0.1... 100%
Progress: Downloading msys2 20210419.0.0... 100%

9 HaoWu,
Hao Wu, CSC
CSC 398
398 Functional
FunctionalProgramming
Programming Week 11
Week
Your First Haskell Code

• Create a file called hello.hs with the following contents


main = putStrLn "Helle, world!"

• Compile your program to a native executable like this



cschxw071168:Scripts wuh2$ ghc --make hello
[1 of 1] Compiling Main ( hello.hs, hello.o )
Linking hello ...
cschxw071168:Scripts wuh2$ ./hello
Helle, world!

10 Hao Wu, CSC 398 Functional Programming Week 1


Your First Haskell Code

• Or run it in the GHCI interpreter like this



cschxw071168:Scripts wuh2$ ghci hello.hs
GHCi, version 8.10.4: https://www.haskell.org/ghc/ :? for help
[1 of 1] Compiling Main ( hello.hs, interpreted )
Ok, one module loaded.
*Main> main
Helle, world!
*Main>

11 Hao Wu, CSC 398 Functional Programming Week 1


GHCi

• GHCi is GHC’s interactive environment that includes an


interactive debugge
- interactively evaluate Haskell expression
- interpret Haskell program
- load GHC-compiled modules

12 Hao Wu, CSC 398 Functional Programming Week 1


r

GHCi: Basic operations

• Open your Terminal (MacOS) or cmd/PowerShell (Windows


• Your can start GHCi session by entering the following
command: “ghci”
cschxw071168:Scripts wuh2$ ghci
GHCi, version 8.10.4: https://www.haskell.org/ghc/ :? for help
Prelude>

• Quit
cschxw071168:Scripts wuh2$ ghci
GHCi, version 8.10.4: https://www.haskell.org/ghc/ :? for help
Prelude> :quit
• Leaving GHCi.

13 Hao Wu, CSC 398 Functional Programming Week 1


:

GHCi: Basic operations

• Haskell expressions can be typed at the prompt


Prelude> 1+2
3
Prelude> let x = 42 in x / 9
4.666666666666667
Prelude>

14 Hao Wu, CSC 398 Functional Programming Week 1

Haskell Editor/IDE

• You can use VSCode as the editor and IDE for Haskel
• You can find a complete tutorial from the website
https://medium.com/@dogwith1eye/setting-up-haskell-in-vs-code-on-
macos-d2cc1ce9f60a
• Here are some plugins you need
-

15 Hao Wu, CSC 398 Functional Programming Week 1


Functional Programming Basics

• Objected Oriented Programming Paradigm


- Focus on the object

• Functional Programming Paradigm


- Focus on the relationships between values

16 Hao Wu, CSC 398 Functional Programming Week 1


s

Function

• A function is something which we can picture as a box with


some inputs and an outpu
5
15
inputs 10 + output

• The function gives an output value which depends upon the


input value(s
- Output —> results
- Inputs —> arguments and parameter
• The process of giving particular inputs to a function is called
function application.

17 Hao Wu, CSC 398 Functional Programming Week 1


)

Types

• A type is a collection of value


• Each function will accept inputs from particular types, and give
a result of a particular typ
integer
integer
integer +

• In modelling a problem situation (Problem Domain


- types represent the things in the domai
- functions represent what can be done to transform or manipulate the
objects

18 Hao Wu, CSC 398 Functional Programming Week 1


e

Expressions and evaluation


• In functional programming, we evaluate expressions to give
value
• In those expressions, we use functions which model our
particular proble

expression value
(7-3)*2 4*2 8

evaluation

expression value

ipV horse ipV

evaluation

19 Hao Wu, CSC 398 Functional Programming Week 1


fl
fl
s

Expressions and evaluation


• A functional Program is made up of a series of definitions of
functions and other value
expression
ipH ( ipV horse )

evaluation
ipH ( ipV )


ipH

value

20 Hao Wu, CSC 398 Functional Programming Week 1


fl
fl
fl
fl
fl
s

Definitions

• A functional program consists of a number definition


• A Haskell definition associates a name (identifier) with a
value of a particular typ

• General Syntax
name :: type
name = expression

• Example

size :: Integer
size = 12 + 13

21 Hao Wu, CSC 398 Functional Programming Week 1


:

Definitions

• names for functions and other values begin with a small lette

• type names begin with a capital letter

22 Hao Wu, CSC 398 Functional Programming Week 1

Function definitions

• We can also define functions


• Example: to square an integer

square :: Integer -> Integer


square n = n * n

Integer Integer
n square n*n

• The first line declares the type of the thing being defined.
- “->” signifies this is a functio
• The second line gives the definition of the functio
- when square is applied to an unknown or variable n, then the result
is n*n
23 Hao Wu, CSC 398 Functional Programming Week 1
n

Function Definitions

• In general, a simple function definition will take the form

name :: t1 -> t2 -> … -> tk -> t

the name
of the the types
function of the the types
being formal of result
defined parameters

24 Hao Wu, CSC 398 Functional Programming Week 1

You might also like