# 0 PROGRAMMING IN HASKELL Chapter 2 - First Steps.

Post on 20-Dec-2015

215 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

• Slide 1
• 0 PROGRAMMING IN HASKELL Chapter 2 - First Steps
• Slide 2
• 1 The Hugs System zHugs is an implementation of Haskell 98, and is the most widely used Haskell system; zThe interactive nature of Hugs makes it well suited for teaching and prototyping purposes; zHugs is available on the web from: www.haskell.org/hugs
• Slide 3
• 2 Starting Hugs % hugs __ __ __ __ ____ ___ _________________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2005 ||---|| ___|| World Wide Web: http://haskell.org/hugs || || Report bugs to: hugs-bugs@haskell.org || || _________________________________________ > On a Unix system, Hugs can be started from the % prompt by simply typing hugs:
• Slide 4
• 3 The Hugs > prompt means that the Hugs system is ready to evaluate an expression. For example: > 2+3*4 14 > (2+3)*4 20 > sqrt (3^2 + 4^2) 5.0
• Slide 5
• 4 The Standard Prelude The library file Prelude.hs provides a large number of standard functions. In addition to the familiar numeric functions such as + and *, the library also provides many useful functions on lists. zSelect the first element of a list: > head [1,2,3,4,5] 1
• Slide 6
• 5 zRemove the first element from a list: > tail [1,2,3,4,5] [2,3,4,5] zSelect the nth element of a list: > [1,2,3,4,5] !! 2 3 zSelect the first n elements of a list: > take 3 [1,2,3,4,5] [1,2,3]
• Slide 7
• 6 zRemove the first n elements from a list: > drop 3 [1,2,3,4,5] [4,5] zCalculate the length of a list: > length [1,2,3,4,5] 5 zCalculate the sum of a list of numbers: > sum [1,2,3,4,5] 15
• Slide 8
• 7 zCalculate the product of a list of numbers: > product [1,2,3,4,5] 120 zAppend two lists: > [1,2,3] ++ [4,5] [1,2,3,4,5] zReverse a list: > reverse [1,2,3,4,5] [5,4,3,2,1]
• Slide 9
• 8 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.
• Slide 10
• 9 In Haskell, function application is denoted using space, and multiplication is denoted using *. f a b + c*d As previously, but in Haskell syntax.
• Slide 11
• 10 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).
• Slide 12
• 11 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
• Slide 13
• 12 Haskell Scripts zAs well as the functions in the standard prelude, you can also define your own functions; zNew functions are defined within a script, a text file comprising a sequence of definitions; zBy convention, Haskell scripts usually have a.hs suffix on their filename. This is not mandatory, but is useful for identification purposes.
• Slide 14
• 13 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 Hugs. Start an editor, type in the following two function definitions, and save the script as test.hs:
• Slide 15
• 14 % hugs test.hs Leaving the editor open, in another window start up Hugs with the new script: > quadruple 10 40 > take (double 2) [1,2,3,4,5,6] [1,2,3,4] Now both Prelude.hs and test.hs are loaded, and functions from both scripts can be used:
• Slide 16
• 15 factorial n = product [1..n] average ns = sum ns `div` length ns Leaving Hugs open, return to the editor, add the following two definitions, and resave: zdiv is enclosed in back quotes, not forward; zx `f` y is just syntactic sugar for f x y. Note:
• Slide 17 factorial 10 3628800 > average [1,2,3,4,5] 3 Hugs does not automatically detect that the script has been changed"> factorial 10 3628800 > average [1,2,3,4,5] 3 Hugs does not automatically detect that the script has been changed, so a reload command must be executed before the new definitions can be used:"> factorial 10 3628800 > average [1,2,3,4,5] 3 Hugs does not automatically detect that the script has been changed" title="16 > :reload Reading file "test.hs" > factorial 10 3628800 > average [1,2,3,4,5] 3 Hugs does not automatically detect that the script has been changed">
• 16 > :reload Reading file "test.hs" > factorial 10 3628800 > average [1,2,3,4,5] 3 Hugs does not automatically detect that the script has been changed, so a reload command must be executed before the new definitions can be used:
• Slide 18
• 17 Naming Requirements zFunction and argument names must begin with a lower-case letter. For example: myFunfun1arg_2x zBy convention, list arguments usually have an s suffix on their name. For example: xsnsnss
• Slide 19
• 18 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
• Slide 20
• 19 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
• Slide 21
• 20 Useful Hugs Commands CommandMeaning :load nameload script name :reloadreload current script :edit nameedit script name :editedit current script :type exprshow type of expr :?show all commands :quitquit Hugs
• Slide 22
• 21 Exercises N = a div length xs where a = 10 xs = [1,2,3,4,5] Try out slides 2-8 and 14-17 using Hugs. Fix the syntax errors in the program below, and test your solution using Hugs. (1) (2)
• Slide 23
• 22 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)