4/3/20081CSE 3302 Programming LanguagesFunctional Programming Language: Chengkai LiFall 2007HaskellLecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 20081Reading and Implementation• Yet Another Haskell Tutorial http://www.cs.utah.edu/~hal/htut• WinHugs:h//hkll /H/ /d ldihhttp://cvs.haskell.org/Hugs/pages/downloading.htmDownload file WinHugs‐Sep2006.exe (14 MB): • Installation:– Try to install outside of “Program Files”.– May fail if you install inside.Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 20082Topics• Basics• Types and classes• Defining functions• List comprehensionsp• Recursive functions• Higher‐order functionsLecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 20083Notes• You need to use script (module) files to define functions, and use interactive environment to evaluate expressions (functions)•Haskellis not freeformat It has certain layout rules•Haskell is not free‐format. It has certain layout rules.• General rules to follow when writing script files:– Indent the same amount for definitions at the same level– Don’t use tabLecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 20084BasicsBasicsLecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 20085The Standard PreludeWhen Hugs is started it first loads the library file Prelude.hs, and then repeatedly prompts the user for an expression to be evaluated.For example: 23*4> 2+3*414> (2+3)*420Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200864/3/20082In Haskell, function application is denoted using spacef aFunction Applicationf(a)Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 20087> length [1,2,3,4]4The standard prelude also provides many useful functions that operate on lists. For example:> product [1,2,3,4]24> take 3 [1,2,3,4,5][1,2,3]Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 20088Moreover, function application is assumed to have higher priority than all other operators.f a + bMeans f(a) + b, rather than f (a + b).Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 20089ExamplesMathematicsHaskellf(x)f( )f xf f(x,y)f(g(x))f(x,g(y))f x yf (g x)f x (g y)Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200810OperatorsOperators are special binary functions. They are used in infix form:3 + 42 < 3Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200811When enclosed in ( ), they are just like other functions(+) 3 4(<) 2 3 ScriptWhen 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: (file name, without .hs, must match module name)module Testwheredouble x = x + xquadruple x = double (double x)Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 2008124/3/20083:load TestLeaving the editor open, load the script:File Test.hs must be in the right path. Use “File >> Options” to change the pathNow both Prelude hs and Test hs are loaded and functions from both scripts> quadruple 1040> take (double 2) [1..6][1,2,3,4]Now both Prelude.hs and Test.hs are loaded, and functions from both scripts can be used:Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200813> :reloadAfter a script is changed, it is automatically reloaded when you save the file. You can also use a reload command:Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200814Types and ClassesTypes and ClassesLecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200815Types in HaskellFalse :: Boolnot :: Bool → BoolWe use the notation e :: T to mean that evaluating the expression e will produce a value of type T.not False :: BoolFalse && True :: BoolLecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200816You can use :type to get the type of an expression> :type False> :type not> :type ‘a’a Every expression must have a valid type, which is calculated prior to evaluating the expression by type inference;a Haskell programs are type safe, because type errors can never occur during evaluation;Note:Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200817try> not ‘a’Type Inference: an ExampleTest> not 'a'ERROR - Type error in application*** Expression : not 'a'*** Term : 'a'*** Type : Char*** Does not match : Booltyping ruleLecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200818f :: A -> B e :: Af e :: Bnot :: Bool -> Bool ‘3’ :: Charnot ‘3’ :: ?4/3/20084Type Inferencemodule Testwheredouble x = x + xTest> :type doubleThe type is inferred automatically.Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200819ypYou can declare the type explicitly. Error in the declaration would be caught. Thus a good debugging tool.Try the following:module Testwheredouble :: Char -> Chardouble x = x + xBasic TypesHaskell has a number of basic types, including:Bool- Logical valuesCharStringInt- Single characters- Strings of characters- integersLecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages, UT‐Arlington ©Chengkai Li, 200820List Types[False,True,False] :: [Bool][’a’,’b’,’c’,’d’] :: [Char]A list is sequence of values of the same type:[[’a’],[’b’,’c’]] :: [[Char]]In general:[T] is the type of lists with elementsof type T.Lecture 18 – Functional Programming, Spring 2008CSE3302 Programming Languages,
View Full Document