SlideShare a Scribd company logo
1
PROGRAMMING IN HASKELL
Chapter 2 - First Steps
2
Glasgow Haskell Compiler
GHC is the leading implementation of Haskell,
and comprises a compiler and interpreter;
The interactive nature of the interpreter makes it
well suited for teaching and prototyping;
GHC is freely available from:
www.haskell.org/platform
3
Starting GHC
% ghci
GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude>
The GHC interpreter can be started from the Unix
command prompt % by simply typing ghci:
4
The GHCi prompt > means that the interpreter is
ready to evaluate an expression.
For example:
> 2+3*4
14
> (2+3)*4
20
> sqrt (3^2 + 4^2)
5.0
5
The Standard Prelude
Haskell comes with a large number of standard
library functions. In addition to the familiar
numeric functions such as + and *, the library
also provides many useful functions on lists.
Select the first element of a list:
> head [1,2,3,4,5]
1
6
Remove the first element from a list:
> tail [1,2,3,4,5]
[2,3,4,5]
Select the nth element of a list:
> [1,2,3,4,5] !! 2
3
Select the first n elements of a list:
> take 3 [1,2,3,4,5]
[1,2,3]
7
Remove the first n elements from a list:
> drop 3 [1,2,3,4,5]
[4,5]
Calculate the length of a list:
> length [1,2,3,4,5]
5
Calculate the sum of a list of numbers:
> sum [1,2,3,4,5]
15
8
Calculate the product of a list of numbers:
> product [1,2,3,4,5]
120
Append two lists:
> [1,2,3] ++ [4,5]
[1,2,3,4,5]
Reverse a list:
> reverse [1,2,3,4,5]
[5,4,3,2,1]
9
Function Application
In mathematics, function application is denoted
using parentheses, and multiplication is often
denoted using juxtaposition or space.
f(a,b) + c d
Apply the function f to a and b, and add
the result to the product of c and d.
10
In Haskell, function application is denoted using
space, and multiplication is denoted using *.
f a b + c*d
As previously, but in Haskell syntax.
11
Moreover, function application is assumed to have
higher priority than all other operators.
f a + b
Means (f a) + b, rather than f (a + b).
12
Examples
Mathematics Haskell
f(x)
f(x,y)
f(g(x))
f(x,g(y))
f(x)g(y)
f x
f x y
f (g x)
f x (g y)
f x * g y
13
Haskell Scripts
As well as the functions in the standard library,
you can also define your own functions;
New functions are defined within a script, a text
file comprising a sequence of definitions;
By convention, Haskell scripts usually have a
.hs suffix on their filename. This is not
mandatory, but is useful for identification
purposes.
14
My First Script
double x = x + x
quadruple x = double (double x)
When developing a Haskell script, it is useful to
keep two windows open, one running an editor for
the script, and the other running GHCi.
Start an editor, type in the following two function
definitions, and save the script as test.hs:
15
% ghci test.hs
Leaving the editor open, in another window start up
GHCi with the new script:
> quadruple 10
40
> take (double 2) [1,2,3,4,5,6]
[1,2,3,4]
Now both the standard library and the file test.hs
are loaded, and functions from both can be used:
16
factorial n = product [1..n]
average ns = sum ns `div` length ns
Leaving GHCi open, return to the editor, add the
following two definitions, and resave:
div is enclosed in back quotes, not forward;
x `f` y is just syntactic sugar for f x y.
Note:
17
> :reload
Reading file "test.hs"
> factorial 10
3628800
> average [1,2,3,4,5]
3
GHCi does not automatically detect that the script
has been changed, so a reload command must be
executed before the new definitions can be used:
18
Naming Requirements
Function and argument names must begin with
a lower-case letter. For example:
myFun fun1 arg_2 x’
By convention, list arguments usually have an s
suffix on their name. For example:
xs ns nss
19
The Layout Rule
In a sequence of definitions, each definition must
begin in precisely the same column:
a = 10
b = 20
c = 30
a = 10
b = 20
c = 30
a = 10
b = 20
c = 30
20
means
The layout rule avoids the need for explicit syntax
to indicate the grouping of definitions.
a = b + c
where
b = 1
c = 2
d = a * 2
a = b + c
where
{b = 1;
c = 2}
d = a * 2
implicit grouping explicit grouping
21
Useful GHCi Commands
Command Meaning
:load name load script name
:reload reload current script
:edit name edit script name
:edit edit current script
:type expr show type of expr
:? show all commands
:quit quit GHCi
22
Exercises
N = a ’div’ length xs
where
a = 10
xs = [1,2,3,4,5]
Try out slides 2-8 and 14-17 using GHCi.
Fix the syntax errors in the program below,
and test your solution using GHCi.
(1)
(2)
23
Show how the library function last that selects
the last element of a list can be defined using
the functions introduced in this lecture.
(3)
Similarly, show how the library function init
that removes the last element from a list can
be defined in two different ways.
(5)
Can you think of another possible definition?(4)

More Related Content

PDF
multi-line record grep
PDF
Phil Bartie QGIS PLPython
PPTX
Understand more about C
PDF
Ruby : Block, Proc and, lambda
PDF
All I know about rsc.io/c2go
PPTX
PDF
Diving into HHVM Extensions (Brno PHP Conference 2015)
PPTX
C Programming Homework Help
multi-line record grep
Phil Bartie QGIS PLPython
Understand more about C
Ruby : Block, Proc and, lambda
All I know about rsc.io/c2go
Diving into HHVM Extensions (Brno PHP Conference 2015)
C Programming Homework Help

What's hot (20)

PPTX
C Assignment Help
PPTX
Programming Homework Help
ODP
LCDS - State Presentation
DOCX
Php questions and answers
DOCX
UNIT 4-HEADER FILES IN C
PPT
Function
PDF
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
PDF
Large Scale JavaScript with TypeScript
PDF
Maintainable go
PDF
Swift the implicit parts
PPTX
Lambda Expressions in C++
PPT
PDF
Data Structure - 2nd Study
PDF
Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...
PPTX
Basic C++ 11/14 for Python Programmers
PPTX
C language header files
PDF
C++11
PDF
C++ ARRAY WITH EXAMPLES
ODP
Devel::hdb debugger talk
C Assignment Help
Programming Homework Help
LCDS - State Presentation
Php questions and answers
UNIT 4-HEADER FILES IN C
Function
ECSE 221 - Introduction to Computer Engineering - Tutorial 1 - Muhammad Ehtas...
Large Scale JavaScript with TypeScript
Maintainable go
Swift the implicit parts
Lambda Expressions in C++
Data Structure - 2nd Study
Scalability comparison: Traditional fork-join-based parallelism vs. Goroutine...
Basic C++ 11/14 for Python Programmers
C language header files
C++11
C++ ARRAY WITH EXAMPLES
Devel::hdb debugger talk
Ad

Similar to Chapter2 Haskell (20)

PDF
Real World Haskell: Lecture 1
PPT
chapter2.ppt
PDF
Functional programming using haskell notes iitk
PDF
DEFUN 2008 - Real World Haskell
PPTX
Functional programming seminar (haskell)
PDF
ZIP
Haskell Jumpstart
PPTX
Introduction to Haskell: 2011-04-13
PDF
High-Performance Haskell
PPT
haskell5.ppt is a marketing document lol
KEY
An Introduction to Functional Programming using Haskell
PDF
Programming in Haskell 2nd Edition Graham Hutton
PDF
01. haskell introduction
PDF
[FLOLAC'14][scm] Functional Programming Using Haskell
PPTX
GHCi: More Awesome Than You Thought
PDF
05. haskell streaming io
PDF
Introduction to Functional Languages
PPT
03-fortran.ppt
PPTX
Solving Haskell Assignment: Engaging Challenges and Solutions for University ...
PDF
Haskell - Being lazy with class
Real World Haskell: Lecture 1
chapter2.ppt
Functional programming using haskell notes iitk
DEFUN 2008 - Real World Haskell
Functional programming seminar (haskell)
Haskell Jumpstart
Introduction to Haskell: 2011-04-13
High-Performance Haskell
haskell5.ppt is a marketing document lol
An Introduction to Functional Programming using Haskell
Programming in Haskell 2nd Edition Graham Hutton
01. haskell introduction
[FLOLAC'14][scm] Functional Programming Using Haskell
GHCi: More Awesome Than You Thought
05. haskell streaming io
Introduction to Functional Languages
03-fortran.ppt
Solving Haskell Assignment: Engaging Challenges and Solutions for University ...
Haskell - Being lazy with class
Ad

Recently uploaded (20)

PPT
Mechanical Engineering MATERIALS Selection
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
additive manufacturing of ss316l using mig welding
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
Safety Seminar civil to be ensured for safe working.
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Mechanical Engineering MATERIALS Selection
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
additive manufacturing of ss316l using mig welding
CYBER-CRIMES AND SECURITY A guide to understanding
BIO-INSPIRED HORMONAL MODULATION AND ADAPTIVE ORCHESTRATION IN S-AI-GPT
OOP with Java - Java Introduction (Basics)
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
Safety Seminar civil to be ensured for safe working.
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Operating System & Kernel Study Guide-1 - converted.pdf
Geodesy 1.pptx...............................................
UNIT-1 - COAL BASED THERMAL POWER PLANTS
III.4.1.2_The_Space_Environment.p pdffdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...

Chapter2 Haskell

  • 2. 2 Glasgow Haskell Compiler GHC is the leading implementation of Haskell, and comprises a compiler and interpreter; The interactive nature of the interpreter makes it well suited for teaching and prototyping; GHC is freely available from: www.haskell.org/platform
  • 3. 3 Starting GHC % ghci GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. Prelude> The GHC interpreter can be started from the Unix command prompt % by simply typing ghci:
  • 4. 4 The GHCi prompt > means that the interpreter is ready to evaluate an expression. For example: > 2+3*4 14 > (2+3)*4 20 > sqrt (3^2 + 4^2) 5.0
  • 5. 5 The Standard Prelude Haskell comes with a large number of standard library functions. In addition to the familiar numeric functions such as + and *, the library also provides many useful functions on lists. Select the first element of a list: > head [1,2,3,4,5] 1
  • 6. 6 Remove the first element from a list: > tail [1,2,3,4,5] [2,3,4,5] Select the nth element of a list: > [1,2,3,4,5] !! 2 3 Select the first n elements of a list: > take 3 [1,2,3,4,5] [1,2,3]
  • 7. 7 Remove the first n elements from a list: > drop 3 [1,2,3,4,5] [4,5] Calculate the length of a list: > length [1,2,3,4,5] 5 Calculate the sum of a list of numbers: > sum [1,2,3,4,5] 15
  • 8. 8 Calculate the product of a list of numbers: > product [1,2,3,4,5] 120 Append two lists: > [1,2,3] ++ [4,5] [1,2,3,4,5] Reverse a list: > reverse [1,2,3,4,5] [5,4,3,2,1]
  • 9. 9 Function Application In mathematics, function application is denoted using parentheses, and multiplication is often denoted using juxtaposition or space. f(a,b) + c d Apply the function f to a and b, and add the result to the product of c and d.
  • 10. 10 In Haskell, function application is denoted using space, and multiplication is denoted using *. f a b + c*d As previously, but in Haskell syntax.
  • 11. 11 Moreover, function application is assumed to have higher priority than all other operators. f a + b Means (f a) + b, rather than f (a + b).
  • 13. 13 Haskell Scripts As well as the functions in the standard library, you can also define your own functions; New functions are defined within a script, a text file comprising a sequence of definitions; By convention, Haskell scripts usually have a .hs suffix on their filename. This is not mandatory, but is useful for identification purposes.
  • 14. 14 My First Script double x = x + x quadruple x = double (double x) When developing a Haskell script, it is useful to keep two windows open, one running an editor for the script, and the other running GHCi. Start an editor, type in the following two function definitions, and save the script as test.hs:
  • 15. 15 % ghci test.hs Leaving the editor open, in another window start up GHCi with the new script: > quadruple 10 40 > take (double 2) [1,2,3,4,5,6] [1,2,3,4] Now both the standard library and the file test.hs are loaded, and functions from both can be used:
  • 16. 16 factorial n = product [1..n] average ns = sum ns `div` length ns Leaving GHCi open, return to the editor, add the following two definitions, and resave: div is enclosed in back quotes, not forward; x `f` y is just syntactic sugar for f x y. Note:
  • 17. 17 > :reload Reading file "test.hs" > factorial 10 3628800 > average [1,2,3,4,5] 3 GHCi does not automatically detect that the script has been changed, so a reload command must be executed before the new definitions can be used:
  • 18. 18 Naming Requirements Function and argument names must begin with a lower-case letter. For example: myFun fun1 arg_2 x’ By convention, list arguments usually have an s suffix on their name. For example: xs ns nss
  • 19. 19 The Layout Rule In a sequence of definitions, each definition must begin in precisely the same column: a = 10 b = 20 c = 30 a = 10 b = 20 c = 30 a = 10 b = 20 c = 30
  • 20. 20 means The layout rule avoids the need for explicit syntax to indicate the grouping of definitions. a = b + c where b = 1 c = 2 d = a * 2 a = b + c where {b = 1; c = 2} d = a * 2 implicit grouping explicit grouping
  • 21. 21 Useful GHCi Commands Command Meaning :load name load script name :reload reload current script :edit name edit script name :edit edit current script :type expr show type of expr :? show all commands :quit quit GHCi
  • 22. 22 Exercises N = a ’div’ length xs where a = 10 xs = [1,2,3,4,5] Try out slides 2-8 and 14-17 using GHCi. Fix the syntax errors in the program below, and test your solution using GHCi. (1) (2)
  • 23. 23 Show how the library function last that selects the last element of a list can be defined using the functions introduced in this lecture. (3) Similarly, show how the library function init that removes the last element from a list can be defined in two different ways. (5) Can you think of another possible definition?(4)