Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.

Similar presentations


Presentation on theme: "Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax."— Presentation transcript:

1 Chapter 15 Other Functional Languages

2 Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax – many programmers find the syntax intimidating Other functional languages have a somewhat more familiar syntax – Miranda – Haskell – ML

3 Copyright © 2007 Addison-Wesley. All rights reserved. 1–3 ML A static-scoped functional language with syntax that is closer to Pascal than to LISP Name is short for metalanguage Devised by Robert Milner in 1973 Conceived for use in an automated theorem prover Dialects Standard ML (SML) CAML, objective CAML F#

4 Copyright © 2007 Addison-Wesley. All rights reserved. 1–4 ML Uses type declarations also does type inferencing to determine the types of undeclared variables It is strongly and statically typed with no type coercions Functions are first-class objects Parametric polymorphism Includes garbage collection exception handling lists and list operations pattern matching

5 Copyright © 2007 Addison-Wesley. All rights reserved. 1–5 ML Specifics The val statement binds a name to a value (similar to define in Scheme) Function declaration form: fun name (parameters) = body; Example function fun cube (x : int) = x * x * x;

6 Copyright © 2007 Addison-Wesley. All rights reserved. Factorial in ML Version 1 fun f (0 : int) : int = 1 | f (n : int) : int = n * f (n- 1) Version 2: types are inferred fun fac 0 = 1 | fac n = n * fac (n-1)

7 Copyright © 2007 Addison-Wesley. All rights reserved. A more robust Factorial fun fact n = let fun fac 0 = 1 | fac n = n * fac (n - 1) in if (n < 0) then raise Fail "negative argument" else fac n end

8 Copyright © 2007 Addison-Wesley. All rights reserved. Miranda Designed by David Turner in 1985 First commercial functional language – purely functional – strong, static typing – lazy evaluation Program is a set of equations Uses indentation to show nested structure

9 Copyright © 2007 Addison-Wesley. All rights reserved. Haskell Named after mathematician Haskell Curry Defined in 1990 as an open-source successor to Miranda Most Important Features – Uses lazy evaluation (evaluate no subexpression until the value is needed) – Has list comprehensions, which allow it to deal with infinite lists

10 Copyright © 2007 Addison-Wesley. All rights reserved. 1–10 Haskell vs ML Similar to ML syntax static scoped strongly typed type inferencing Different from ML purely functional (e.g., no variables, no assignment statements, and no side effects of any kind)

11 Copyright © 2007 Addison-Wesley. All rights reserved. 1–11 Function Definitions with Different Parameter Forms Fibonacci Numbers fib 0 = 1 fib 1 = 1 fib (n + 2) = fib (n + 1) + fib n

12 Copyright © 2007 Addison-Wesley. All rights reserved. 1–12 Guards Factorial fact n | n == 0 = 1 | n > 0 = n * fact (n - 1) The special word otherwise can appear as a guard

13 Copyright © 2007 Addison-Wesley. All rights reserved. 1–13 Lists List notation: Put elements in brackets e.g., directions = [“north”, “south”, “east”, “west”] Length: # e.g., #directions is 4 Arithmetic series with the.. Operator e.g., [2, 4..10] is [2, 4, 6, 8, 10] Catenation is with ++ e.g., [1, 3] ++ [5, 7] results in [1, 3, 5, 7] cons, car, cdr via the colon operator (as in Prolog) e.g., 1:[3, 5, 7] results in [1, 3, 5, 7]

14 Copyright © 2007 Addison-Wesley. All rights reserved. 1–14 Factorial Revisited product [] = 1 product (a:x) = a * product x fact n = product [1..n]

15 Copyright © 2007 Addison-Wesley. All rights reserved. 1–15 List Comprehension Set notation List of the squares of the first 20 positive integers: [n * n | n ← [1..20]] All of the factors of its given parameter: factors n = [i | i ← [1..n div 2], n mod i == 0]

16 Copyright © 2007 Addison-Wesley. All rights reserved. 1–16 Quicksort sort [] = [] sort (a:x) = sort [b | b ← x; b <= a] ++ [a] ++ sort [b | b ← x; b > a]

17 Copyright © 2007 Addison-Wesley. All rights reserved. 1–17 Lazy Evaluation Only compute those that are necessary Positive numbers positives = [0..] Determining if 16 is a square number member [] b = False member(a:x) b =(a == b)||member x b squares = [n * n | n ← [0..]] member squares 16

18 Copyright © 2007 Addison-Wesley. All rights reserved. 1–18 Member Revisited The member function could be written as: member [] b = False member(a:x) b=(a == b)||member x b However, this would only work if the parameter to squares was a perfect square; if not, it will keep generating them forever. The following version will always work: member2 (m:x) n | m < n = member2 x n | m == n = True | otherwise = False

19 Copyright © 2007 Addison-Wesley. All rights reserved. 1–19 Applications of Functional Languages APL is used for throw-away programs LISP is used for artificial intelligence – Knowledge representation – Machine learning – Natural language processing – Modeling of speech and vision Scheme is used to teach introductory programming at a significant number of universities

20 Copyright © 2007 Addison-Wesley. All rights reserved. 1–20 Comparing Functional and Imperative Languages Imperative Languages: – Efficient execution – Complex semantics – Complex syntax – Concurrency is programmer designed Functional Languages: – Simple semantics – Simple syntax – Inefficient execution – Programs can automatically be made concurrent


Download ppt "Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax."

Similar presentations


Ads by Google