getting started with hugs on linux - university of …...in haskell, java, c, english, html no...
TRANSCRIPT
Getting started with Hugs on LinuxCS190 Functional Programming Techniques
Dr Hans Georg Schaathun
University of Surrey
Autumn 2008Week 1
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 1 / 43
Outline
1 Learning Objectives
2 File tree and command line
3 Software
4 The editor
5 Haskell
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 2 / 43
Learning Objectives
Outline
1 Learning Objectives
2 File tree and command line
3 Software
4 The editor
5 Haskell
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 3 / 43
Learning Objectives
Learning ObjectivesFrom the module description
At the end of the module, the students willbe able to work with standard command-line interfaces.write programs employing the fundamental features of Haskell.be able to make and use reusable functions as a fundamentalbuilding block of software development.understand recursion and polymorphism and be able to use themin simple algorithm design.contrast functional and imperative languages and understand theadvantages and disadvantages of each.
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 4 / 43
Learning Objectives
The lab exercises
Weekly lab sessions – one exercise set per weekLab Sheet 1 for next week (preparatory exercises).
PortfoliosContaining Haskell functions from the weekly labsMust be passed
Portfolio 1 (Lab Sheet 2 for Weeks 2-4).Public test script (TBC)
Portfolio 2 (Lab Sheet 3 for Weeks 5-8).Secret test script (TBC)
Exact details will be announced later.Including assessment criteriaSee http://www.computing.surrey.ac.uk/teaching/current/cs190/
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 5 / 43
Learning Objectives
The exam
The mark is decided purely by the exam... though you will fail if you fail on a portfolio
2h written, unseen examYou need the practical understanding from the labs
The exercise material is central for the examIf you master the exercises, a catastrophe is unlikely
... but the exam will have something extraExtra exercises will help you
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 6 / 43
Learning Objectives
This session
This sessions will give you the basic background tostart using the faculty linux systemstart using the hugs Haskell interpreter
During this week you have totry the software in the linux labsmake sure that you can use the knowledge from the lecture
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 7 / 43
Learning Objectives
References
A basic book: Programming in Haskell by Graham HuttonA comprehensive book: Haskell: The Craft of FunctionalProgramming by Simon Thompson
You’ll need one of the bookshttp://www.haskell.org
You can download a copy of hugs
Reference on Unix/Linux (e.g. Unix in a Nutshell [O’Reilly])
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 8 / 43
File tree and command line
Outline
1 Learning Objectives
2 File tree and command line
3 Software
4 The editor
5 Haskell
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 9 / 43
File tree and command line
The command line (shell)
Open a terminal windowApplications→ System→ Terminal... emulates an old-fashioned text terminal (tty)
An interpreter (or shell) runs in the terminalreads and interprets commands
Different interpreters are available: bash, tcsh, ksh, zsh
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 10 / 43
File tree and command line
The file tree
research/ teaching/Notes/ labs/
Mail/ bin/ Papers/ work/Mail/ cs190/ cs168/
css1hs/ css1ht/eps351/
bin/ lib/cscssst/ epsguest/
filer2/ filer6/ bin/ man/ local/
home/ tmp/ usr/
/
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 11 / 43
File tree and command line
Navigating in the tree
The files are organised in a treeEach node is either
a directory, containing files and/or other directoriesa file, containing data for some piece of software
Current Working Directoryyour location in the tree
Useful commands:pwd : print current directorycd : change directoryls : list directory contents
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 12 / 43
File tree and command line
Naming a node
A file/directory is identified by its pathName each directory traversed to reach the file
separated by slashes (/)A relative path starts from the current directory
e.g. cs190/test.hs.. identifies the parent directory
Absolute paths starts at the root (/)/home/filer6/epsguest/eps351/cs190/test.hs
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 13 / 43
File tree and command line
Path: /home/filer6/epsguest/eps351/cs190/
research/ teaching/Notes/ labs/
Mail/ bin/ Papers/ work/Mail/ cs190/ cs168/
css1hs/ css1ht/eps351/
bin/ lib/cscssst/ epsguest/
filer2/ filer6/ bin/ man/ local/
home/ tmp/ usr/
/
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 14 / 43
File tree and command line
Modifying the tree
mkdir : make new directoryrmdir : remove (empty) directoryrm : remove filecp : copy filemv : move file
For more information you can use the man pagesman cpman man
It takes some time to learn to read the man pages though
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 15 / 43
File tree and command line
File information
% ls -ltotal 12drwxr-sr-x 2 eps351 epsguest 4096 Aug 6 13:25 labs/drwxr-sr-x 2 eps351 epsguest 4096 Aug 6 13:25 Notes/-rw------- 1 eps351 epsguest 0 Aug 6 13:26 test2.hs-rw-r--r-- 1 eps351 epsguest 8 Aug 6 13:25 test.hs
File type: d for directory, - for regular fileNine characters for mode (access permissions)Number of links to the fileFile owner (eps351) and group (epsguest)File size (bytes)Time of last modification
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 16 / 43
File tree and command line
File mode (access permissions)
drwxr-sr-x 2 eps351 epsguest 4096 Aug 6 13:25Notes/Three user classes:
user/owner (char 2-4)group (char 5-7)others (char 8-10)
Three access rights:r for readw for writex (or s) for execute (programs) or open (directories)
chmod : change modechmod g+w test.hs gives (+) the group (g) the write access (w)
u/g/o/a [+/-/=] [r/w/x]
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 17 / 43
Software
Outline
1 Learning Objectives
2 File tree and command line
3 Software
4 The editor
5 Haskell
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 18 / 43
Software
Running other software
Any program can be launched from the command lineSome create their own window:
firefox &emacs &
Some run in the terminal and shell:man manvim myfile.hsfind . -name myexercise.hs -print
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 19 / 43
Software
Background and Foreground
What does the ampersand mean?Starting a program in the foreground: emacsStarting a program in the background: emacs &Only have one foreground process
and then, it cannot do anything elseWhen a program runs in the background,
the shell continues doing other things (i.e. new prompt)
Programs running in the terminal window needs the foreground
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 20 / 43
Software
Background and Foreground
What does the ampersand mean?Starting a program in the foreground: emacsStarting a program in the background: emacs &Only have one foreground process
and then, it cannot do anything elseWhen a program runs in the background,
the shell continues doing other things (i.e. new prompt)
Programs running in the terminal window needs the foreground
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 20 / 43
Software
Background and Foreground
What does the ampersand mean?Starting a program in the foreground: emacsStarting a program in the background: emacs &Only have one foreground process
and then, it cannot do anything elseWhen a program runs in the background,
the shell continues doing other things (i.e. new prompt)
Programs running in the terminal window needs the foreground
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 20 / 43
Software
Moving to the background
Special keys in the terminal onlyInterrupt: Ctrl-C (terminates the program)Suspend: Ctrl-Z (program stops, but can rewake)
Rewaking a process:Background: bgForeground: fgList jobs: jobsbg %jobno.
If you forget the ampersand: Ctrl-Z + bg
If you suspend a terminal program: fg
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 21 / 43
Software
Processes and kill
List processes:Current terminal: psAll: ps xgNote PID (process ID) to the left
Kill a process:Terminate: kill PID or kill %jobno.Kill: kill -KILL (and PID or job no.)
Dynamic process list: topshows resource expenditure
If a process malfunctions, you can kill it.
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 22 / 43
The editor
Outline
1 Learning Objectives
2 File tree and command line
3 Software
4 The editor
5 Haskell
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 23 / 43
The editor
The editor
The editor is our most important toolEdit plain text files
in Haskell, Java, C, English, HTMLno special formatting (i.e. not a word-processor)
Use your favourite editoremacs, vim, pico, etc.
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 24 / 43
The editor
emacs
Straight forward for most usersUni-modal editorRuns in a separate window (with menus etc.)
It is a powerful toolAdvanced editingMacrosPlugins for email, news, etc
Advanced features are hard to learn and few use them
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 25 / 43
The editor
vim
Bimodal editorAlien for many usersMany commands at few key strokes
Command modeall keys are used for commands (movement, replace, etc)
Insert modeTyped text is added to the file
Runs on the command line: very fast
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 26 / 43
The editor
Basics in vim/vi
Start it: vim filename.hs
Command mode→ Insert mode: i, I, a, A, o, OInsert mode→ Command mode: EscColon commands are followed by return:
Quit: :q (:q! forced)Save and quit: :wqSearch and replace: :%s/old/new/g
Delete: x, X (character), dd (line)Replace: r (character), R (several character (insert mode))See a full tutorial on the web page.
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 27 / 43
The editor
Starting the editor on a file
A file can be given on the command linevi myfile.hs starts vi on the given file
Editors running in the terminal (vi, pico)are almost always started this way
You can also run emacs myfile.hsbut many prefer loading the files from the menus
because emacs is heavy, subsequent files should be loaded fromthe menu
quitting and restarting is impractical
vi users often prefer quit and restart
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 28 / 43
Haskell
Outline
1 Learning Objectives
2 File tree and command line
3 Software
4 The editor
5 Haskell
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 29 / 43
Haskell
The interpreter
Start hugs on the command linedifferent prompt
Reads and interprets commands and expressions:load module.hs56+3*9-7"Hello World!""Hello" ++ ", " ++ "George"
You can use it straight away as a calculator... but you’d rather want to make your own module with advancedfeatures
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 30 / 43
Haskell
Expressions
An expression can beA single number: 42 or 12.4An operator with arguments: 12 + 4 or 54.2/3A function call: sqrt 12 (
√12)
When you type an expresion,it is evaluated, i.e. functions and operators are applied to produce aresultthe result is displayed if possible
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 31 / 43
Haskell
What is a function?
x1 function y1
View the function as a machineA value x1 comes inA value y1 is returned
Using a function, it is a black boxYou don’t know what happens insideYou see only the output y1.
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 32 / 43
Haskell
What is a function?
x1 function y1
View the function as a machineA value x1 comes inA value y1 is returned
Using a function, it is a black boxYou don’t know what happens insideYou see only the output y1.
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 32 / 43
Haskell
For example?
2 sqrt (√ ) 1.4142135623731
Main> sqrt 21.4142135623731Main>
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 33 / 43
Haskell
Functional programming
Functions are the fundamental building block in functionalprogrammingContrary to imperative programming (e.g. Java)
... where data objects are the building blocksDuring the module we will see how
functions return functionsfunctions are arguments to functions
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 34 / 43
Haskell
Haskell ScriptsDefining functions and constants
Haskell scripts: e.g. demo.hsEdit scripts using an editor (vim, emacs)Load the script in hugs using :load
The script contains definitionsnot expressions
Note that you cannot make definitions on the command lineonly expressions
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 35 / 43
Haskell
Haskell ScriptsDefining functions and constants
Haskell scripts: e.g. demo.hsEdit scripts using an editor (vim, emacs)Load the script in hugs using :load
The script contains definitionsnot expressions
Note that you cannot make definitions on the command lineonly expressions
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 35 / 43
Haskell
Definitions and declarations
Declaration: myName :: String
Definition: myName = "George"This is not similar to Java
= denotes equality – it is a universal truthin Java = denotes an assignment
and myName could then be reassigned
a definition is done once and for all
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 36 / 43
Haskell
Definitions and declarations
Declaration: myName :: String
Definition: myName = "George"This is not similar to Java
= denotes equality – it is a universal truthin Java = denotes an assignment
and myName could then be reassigned
a definition is done once and for all
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 36 / 43
Haskell
Function definitions
treble :: Int -> Int
treble x = 3*x
Hugs> :load 01intro/demo.hsMain> myName"George"Main> treble 515Main> treble 4.3ERROR - Cannot infer instance
*** Instance : Fractional Int
*** Expression : treble 4.3
Main>
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 37 / 43
Haskell
Lists
myList :: [Int]
myList = [12, 15, 24, 14, 12, 6]
Standard functions and operators exist for lists
Main> myList!!224Main> reverse myList[6,12,14,24,15,12]Main> drop 4 myList[12,6]Main> drop (2*2) (reverse myList)[15,12]Main>
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 38 / 43
Haskell
Naming requirementsHaskell is case sensitive
Haskell is case sensitivemyList and mylist are different variables
Case gives information about the identifierall variables start with lower-case : myListall type identifiers start with upper-case : Int
If you forget this, you will get confusing error messages...
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 39 / 43
Haskell
The layout rule
How does the interpreter know where a definition ends?In Java this is marked by a semicolon (;)Haskell uses layout, i.e. indentation
fun x y =a long
and winding expressionin x
+ yfun2 x y= another function in x and y
lines with extra indentation continues the definitionany text in the same column or to the left marks a new definition
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 40 / 43
Haskell
Good practice
Use indentation consistentlyTry to make it easy to read
max x y| x > y = x| otherwise = y
Align the guards (|) and the equal signs (=)Layout is mainly a question of personal style
... but please think of the reader
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 41 / 43
Haskell
Comments
Good practice: comment your codeComment starts with - and ends at the end of line
- the compiler ignore comments like this
Comments are for human readersexplain your codemake it easy to understand
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 42 / 43
Haskell
Summary
Quick overview of basic features and tools neededYou will have to test it all on the computer
lab sessionsand in your own time
This week’s material is basicyou have to master it
Dr Hans Georg Schaathun Getting started with Hugs on Linux Autumn 2008Week 1 43 / 43