GC16/3011 Functional Programming Lecture 4 Miranda
description
Transcript of GC16/3011 Functional Programming Lecture 4 Miranda
04/20/2304/20/23 11
GC16/3011 Functional ProgrammingLecture 4
Miranda(and her friend Amanda)(and her friend Amanda)
04/20/2304/20/23 22
Contents
• Miranda/AmandaMiranda/Amanda• Amanda demonstrationAmanda demonstration• CommentsComments• Legal names and bindingLegal names and binding• Types and type checkingTypes and type checking• TuplesTuples• Simple functionsSimple functions
04/20/2304/20/23 33
Amanda
• PC version of MirandaPC version of Miranda• Almost (but not quite) the same!Almost (but not quite) the same!• Get it from the Web pagesGet it from the Web pages
• http://www.cs.ucl.ac.uk/teaching/3C11/index.htmlhttp://www.cs.ucl.ac.uk/teaching/3C11/index.html
• Amanda204:Amanda204:• http://www.engineering.tech.nhl.nl/http://www.engineering.tech.nhl.nl/
engineering/personeel/bruin/data/engineering/personeel/bruin/data/amanda204.zipamanda204.zip
04/20/2304/20/23 44
Amanda Demonstration
• Lambda CalculusLambda Calculus• (3 + 4) * (6 + 7)(3 + 4) * (6 + 7)
• But you can gives names to But you can gives names to (sub)expressions(sub)expressions
• x = 3 + 4x = 3 + 4• y = 6 + 7y = 6 + 7• main = (x * y)main = (x * y)
• Also give names to functions (no lambdas!)Also give names to functions (no lambdas!)• inc x = x + 1inc x = x + 1• main = inc 56main = inc 56
04/20/2304/20/23 55
Amanda Demonstration
• Interpretive environmentInterpretive environment• Use it as a calculatorUse it as a calculator• Simple definitions stored in a fileSimple definitions stored in a file• Main definitionMain definition• Define and use functionsDefine and use functions
04/20/2304/20/23 66
Comments
• VERY important!VERY important!• Use them from the startUse them from the start• Example:Example:
|| a simple definition for some text:|| a simple definition for some text:message = “hello mum”message = “hello mum”
|| here is a function which adds one to a number:|| here is a function which adds one to a number:inc x = x + 1inc x = x + 1
04/20/2304/20/23 77
Legal names
• BINDING:BINDING:• NAME = EXPRESSIONNAME = EXPRESSION• Funcname argname = EXPRESSIONFuncname argname = EXPRESSION
• Binds funcname when defined (static)Binds funcname when defined (static)• Binds argname when applied to an argument (dynamic)Binds argname when applied to an argument (dynamic)
• Each binding is Each binding is uniqueunique, within specified scope, within specified scope• Scope of argname is the function body (only)Scope of argname is the function body (only)• Nested scopes (see later) permit nested bindings for same nameNested scopes (see later) permit nested bindings for same name
• Names MUST start with an alphabetic character Names MUST start with an alphabetic character • Names MUST NOT start with a capital letter!Names MUST NOT start with a capital letter!
• but may contain numbers and underscoresbut may contain numbers and underscores
04/20/2304/20/23 88
Types
• Data can be, for example:Data can be, for example:• Numbers (42): numNumbers (42): num• Characters (‘A’): charCharacters (‘A’): char• Text (“strings”): [char]Text (“strings”): [char]• Truth values (True, False): boolTruth values (True, False): bool• Functions (f x = x + 1): arg_type -> result_typeFunctions (f x = x + 1): arg_type -> result_type
• Miranda/Amanda allows us to CATEGORISE data into Miranda/Amanda allows us to CATEGORISE data into specific typesspecific types
• Helps organise programs betterHelps organise programs better• Helps detect errorsHelps detect errors
04/20/2304/20/23 99
Type Checking
• Done before the program is runDone before the program is run
• Checks that operators (e.g. +) are executed on data of the Checks that operators (e.g. +) are executed on data of the correct typecorrect type
• Checks that functions are applied to data of the correct typeChecks that functions are applied to data of the correct type
• You can ask “what type is this?”You can ask “what type is this?”
• You can specify “this is a Boolean value” etc.You can specify “this is a Boolean value” etc.
04/20/2304/20/23 1010
Tuples
• A simple data structureA simple data structure
• (“Sheila Bloggs”, 23, “2 Turnabout Road, NW3”, 60, False)(“Sheila Bloggs”, 23, “2 Turnabout Road, NW3”, 60, False)
• (34, True) :: (num, bool)(34, True) :: (num, bool)
• (34, True) DOES NOT EQUAL (True, 34)(34, True) DOES NOT EQUAL (True, 34)
• (“increment”, (+ 1), inc)(“increment”, (+ 1), inc):: ([char], num->num, num->num):: ([char], num->num, num->num)
04/20/2304/20/23 1111
Simple Functions
inc x = x + 1inc x = x + 1
|| the hello function|| the hello function
hello :: num -> [char]hello :: num -> [char]
hello x = “good morning”, if (x<10)hello x = “good morning”, if (x<10)
= “goodbye”, otherwise= “goodbye”, otherwise
04/20/2304/20/23 1212
Summary
• Miranda/AmandaMiranda/Amanda• Amanda demonstrationAmanda demonstration• CommentsComments• Legal names and bindingLegal names and binding• Types and type checkingTypes and type checking• TuplesTuples• Simple functionsSimple functions