1 / 12

159.331 Programming Languages & Algorithms

159.331 Programming Languages & Algorithms. Lecture 22 - Functional Programming Languages - Part 4 More Example Languages. Example Languages - ML & SML. Meta-Language Robin Milner et al. 1990 Static Scoped (like Scheme) ML is strongly typed however

Télécharger la présentation

159.331 Programming Languages & Algorithms

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. 159.331 Programming Languages & Algorithms Lecture 22 - Functional Programming Languages - Part 4 More Example Languages Prog Lang & Alg

  2. Example Languages - ML & SML • Meta-Language • Robin Milner et al. 1990 • Static Scoped (like Scheme) • ML is strongly typed however • Ha s type inferencing so type declaratiosn not always (often) used • No type coercions - types must match properly Prog Lang & Alg

  3. ML uses infix so it “looks a lot more imperative” than Lisp and Scheme fun fact(n: int) : int = if n = 0 then 1 else n * fact( n - 1); • ML has exception handling and module facility for implementing ADTs • ML uses square brackets for lists - like Miranda • hd and tl instead of CAR and CDR • CONS is :: instead of a single : as in Miranda • Various features absorbed from Hope combined to form Standard ML (SML) • We can see a lot of Miranda’s and Haskell’s origins in ML and SML. Prog Lang & Alg

  4. Example Languages - Haskell • Similar to ML syntax • Haskell is statically scoped and strongly typed and has same type inferencing method as ML • but Haskell uses lazy evaluation (non-strict semantics) • and Haskell has list comprehensions • (Useful book is Haskell - the Craft of Functional Programming, Simon Thompson, Pub Addison Wesley, ISBN 0-201-34275-8) Prog Lang & Alg

  5. Haskell - factorial function fact :: Int -> Int fact 0 = 1 fact n = n * fact (n-1) • Put the above in file fact.hs and run the command HUGS fact.hs • Set of two pattern matching equations • (No reserved word to introduce the function definition ie fun in ML ) • We can invoke fact 3 => 6 (type fact 3 at the HUGS prompt) Prog Lang & Alg

  6. Fibonacci Numbers in Haskell fib 0 = 1 fib 1 = 1 fib ( n + 2 ) = fib ( n + 1 ) + fib n • The type checker will figure out the missing first line fib :: Int -> Int • map fib [0..10] => [1,1,2,3,5,8,13,21,34,55,89] • map fib [0..] is a bad idea because…? • Guards | can specify which definition to use, eg: fact n | n == 0 = 1 | n > 0 = n * fact( n -1) • So that fact 0 is OK but fact -1 gives an error Prog Lang & Alg

  7. We can have an “otherwise” sub n | n < 10 = 0 | n > 100 = 2 | otherwise = 1 • hd, tl, : etc as in Miranda member [ ] b = False member ( a:x) b = ( a == b ) || member x b • And member [1..10] 6 => True • member [1..] 0 will take a while… Prog Lang & Alg

  8. Functional programs consist of functions that map input values from one domain onto output values of another (co)domain, much as in mathematics. Pure functional languages lack imperative features such as assignment and repetitive statements. Also unlike most imperative languages, functions in functional languages cannot have side-effects. Pure functional languages are referentially transparent, which means that the result of a function application does not depend on when the function is called but only on the arguments of the call. Referential transparency can make programs easier to read, transform, parallelize and prove correct. Summary Prog Lang & Alg

  9. Problems with the functional paradigm are expressing I/O and obtaining an efficient implementation. Efficient updating of data structures poses an important implementation problem. • The most important concepts in functional languages are recursive functions, lists, polymorphism, higher-order functions, currying and equations. • Recursion can be used to express repetition. • Lists are the main data structures in functional languages. Many operations on lists are pre-defined or built-in. • The management of memory for data structures is done automatically. Programmers are not aware of the existence of machine addresses (there are no pointers). Deallocation of memory that is no longer used is also done automatically, using garbage collection. Prog Lang & Alg

  10. Modern functional languages support polymorphic functions, which can take arguments of different types. Many (functional) languages use strong typing, but the types are often inferred by a type checker and need not be given by the programmer. • A higher-order function takes a function as argument. Higher-order functions contribute significantly to the flexibility of functional languages. • Applicative order reduction first evaluates the arguments of a function and then applies the function. Normal order reduction applies the function to the unevaluated arguments, and only evaluates the arguments when needed. Prog Lang & Alg

  11. Normal order reduction is the basis for lazy evaluation. Lazy evaluation makes it possible to deal with infinite data structures. It is conceptually more elegant than eager evaluation, but it is also harder to implement efficiently. • Modern functional languages allow functions to be defined as a set of equations. They use pattern matching to select the appropriate equation. • Impure functional languages such as Lisp support some properties of functional languages, but are not referentially transparent. Prog Lang & Alg

  12. Further Reading • See Bal & Grune Chapter 4 • See Sebesta Chapter 15 • See also Hudak’s ACM article on Conception, Evolution and Application of Functional Programming Languages • Next - Lambda Calculus (not assessed) • And then Logic Programming Languages… Prog Lang & Alg

More Related