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

Slides:



Advertisements
Similar presentations
Functional Programming Languages Session 12
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Functional Programming Languages
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Haskell Fall 2005 Marco Valtorta
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
ISBN Chapter 15 Functional Programming Languages.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
ISBN Chapter 15 Functional Programming Languages.
Dr. Muhammed Al-Mulhem ICS An Introduction to Functional Programming.
1 Lisp and Functional Languages Functional forms Referential transparency Function construction Function composition Mapping functions Designing functional.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
1 Functional Programming In Text: Chapter Chapter 2: Evolution of the Major Programming Languages Outline Functional programming (FP) basics A bit.
 The design of the imperative languages is based directly on the von Neumann architecture  Efficiency is the primary concern  Low-level specifications:
ISBN Chapter 15 Functional Programming Languages.
Functional Programming Languages Chapter 14
ISBN Chapter 15 Functional Programming Languages.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
ISBN Chapter 15 Functional Programming Languages.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Chapter Fifteen: Functional Programming Languages Lesson 12.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java, Haskell.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
Chapter 15 Functional Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 15 Topics Introduction Mathematical Functions.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1-1 An Introduction to Functional Programming Sept
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
Miranda Programming Language By Bindu H. Vinay. History of Miranda  Miranda was developed in by David Turner  It is currently being marketed.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming
Functional Programming
Functional Programming Languages
Programming Languages and Compilers (CS 421)
Functional Programming Languages
Functional Programming Languages
Chapter 15 :Functional Programming Languages
Functional Programming Languages
Functional Programming
Functional Programming
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
15.2 Mathematical Functions
Functional Programming Languages
Functional Programming Languages
Chapter 15 Functional Programming 6/1/2019.
Functional Programming Languages
Functional Programming Languages
Presentation transcript:

Chapter 15 Other Functional Languages

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

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#

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

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;

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)

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

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

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

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)

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

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

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]

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

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]

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]

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

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

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

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