120 likes | 250 Vues
This lecture explores functional programming concepts using the Miranda and Amanda languages. It covers key topics such as defining and using functions, legal names and binding rules, types and type checking, and tuples. Amanda demonstrates practical examples, highlighting the importance of comments and simple definitions. The lecture emphasizes the distinction between static and dynamic bindings, and data organization through specific types. Students will gain a foundational understanding of functional programming principles and their applications.
E N D
GC16/3011 Functional ProgrammingLecture 4Miranda (and her friend Amanda)
Contents • Miranda/Amanda • Amanda demonstration • Comments • Legal names and binding • Types and type checking • Tuples • Simple functions
Amanda • PC version of Miranda • Almost (but not quite) the same! • Get it from the Web pages • http://www.cs.ucl.ac.uk/teaching/3C11/index.html • Amanda204: • http://www.engineering.tech.nhl.nl/engineering/personeel/bruin/data/amanda204.zip
Amanda Demonstration • Lambda Calculus • (3 + 4) * (6 + 7) • But you can gives names to (sub)expressions • x = 3 + 4 • y = 6 + 7 • main = (x * y) • Also give names to functions (no lambdas!) • inc x = x + 1 • main = inc 56
Amanda Demonstration • Interpretive environment • Use it as a calculator • Simple definitions stored in a file • Main definition • Define and use functions
Comments • VERY important! • Use them from the start • Example: || a simple definition for some text: message = “hello mum” || here is a function which adds one to a number: inc x = x + 1
Legal names • BINDING: • NAME = EXPRESSION • Funcname argname = EXPRESSION • Binds funcname when defined (static) • Binds argname when applied to an argument (dynamic) • Each binding is unique, within specified scope • Scope of argname is the function body (only) • Nested scopes (see later) permit nested bindings for same name • Names MUST start with an alphabetic character • Names MUST NOT start with a capital letter! • but may contain numbers and underscores
Types • Data can be, for example: • Numbers (42): num • Characters (‘A’): char • Text (“strings”): [char] • Truth values (True, False): bool • Functions (f x = x + 1): arg_type -> result_type • Miranda/Amanda allows us to CATEGORISE data into specific types • Helps organise programs better • Helps detect errors
Type Checking • Done before the program is run • Checks that operators (e.g. +) are executed on data of the correct type • Checks that functions are applied to data of the correct type • You can ask “what type is this?” • You can specify “this is a Boolean value” etc.
Tuples • A simple data structure • (“Sheila Bloggs”, 23, “2 Turnabout Road, NW3”, 60, False) • (34, True) :: (num, bool) • (34, True) DOES NOT EQUAL (True, 34) • (“increment”, (+ 1), inc) :: ([char], num->num, num->num)
Simple Functions inc x = x + 1 || the hello function hello :: num -> [char] hello x = “good morning”, if (x<10) = “goodbye”, otherwise
Summary • Miranda/Amanda • Amanda demonstration • Comments • Legal names and binding • Types and type checking • Tuples • Simple functions