1 / 12

GC16/3011 Functional Programming Lecture 4 Miranda

GC16/3011 Functional Programming Lecture 4 Miranda. (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!

varen
Download Presentation

GC16/3011 Functional Programming Lecture 4 Miranda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. GC16/3011 Functional ProgrammingLecture 4Miranda (and her friend Amanda)

  2. Contents • Miranda/Amanda • Amanda demonstration • Comments • Legal names and binding • Types and type checking • Tuples • Simple functions

  3. 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

  4. 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

  5. Amanda Demonstration • Interpretive environment • Use it as a calculator • Simple definitions stored in a file • Main definition • Define and use functions

  6. 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

  7. 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

  8. 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

  9. 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.

  10. 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)

  11. Simple Functions inc x = x + 1 || the hello function hello :: num -> [char] hello x = “good morning”, if (x<10) = “goodbye”, otherwise

  12. Summary • Miranda/Amanda • Amanda demonstration • Comments • Legal names and binding • Types and type checking • Tuples • Simple functions

More Related