CS 3304 Comparative Languages

Slides:



Advertisements
Similar presentations
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
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.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional Programming Languages
The Scheme Programming Language History and Significance Dmitry Nesvizhsky CIS24 Professor Danny Kopec.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
Dr. Muhammed Al-Mulhem ICS An Introduction to Functional Programming.
Chapter 10 :: Functional Languages
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Functional Programming in Scheme
1 Functional Programming In Text: Chapter Chapter 2: Evolution of the Major Programming Languages Outline Functional programming (FP) basics A bit.
ISBN Chapter 15 Functional Programming Languages.
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.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
1-1 An Introduction to Functional Programming Sept
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Functional Programming
CS314 – Section 5 Recitation 10
Functional Programming
Functional Programming Languages
Functional Programming Languages
Functional Programming
History of Computing – Lisp
Chapter 11 :: Functional Languages
CS 326 Programming Languages, Concepts and Implementation
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Functional Programming
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Closures and Streams cs784(Prasad) L11Clos
Functional Programming Languages
Introduction to Functional Programming in Racket
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
CS 36 – Chapter 11 Functional programming Features Practice
Functional Programming Languages
Introduction to Functional Programming in Racket
COP4020 Programming Languages
Functional Programming and Haskell
15.2 Mathematical Functions
Chapter 15 Functional Programming 6/1/2019.
Functional Programming Languages
Functional Programming and Haskell
Presentation transcript:

CS 3304 Comparative Languages Lecture 18: Functional Languages 22 March 2012

Imperative Languages The imperative and functional models grew out of work undertaken Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s: Different formalizations of the notion of an algorithm, or effective procedure, based on automata, symbolic manipulation, recursive function definitions, and combinatorics. The design of the imperative languages is based directly on the von Neumann architecture: Efficiency is the primary concern, rather than the suitability of the language for software development. Turing’s model of computing was the Turing machine a sort of pushdown automaton using an unbounded storage “tape”: The Turing machine computes in an imperative way, by changing the values in cells of its tape – like variables just as a high level imperative program computes by changing the values of variables.

Church’s Model These results led Church to conjecture that any intuitively appealing model of computing would be equally powerful as well: this conjecture is known as Church’s thesis. Church’s model of computing is called the lambda calculus: Based on the notion of parameterized expressions with each parameter introduced by an occurrence of the letter λ — hence the notation’s name. Lambda calculus was the inspiration for functional programming. One uses it to compute by substituting parameters into expressions, just as one computes in a high level functional program by passing arguments to functions.

Functional Languages The design of the functional languages is based on mathematical functions: A solid theoretical basis that is also closer to the user, but relatively unconcerned with the architecture of the machines on which programs will run. Functional languages such as Lisp, Scheme, FP, ML, Miranda, and Haskell are an attempt to realize Church's lambda calculus in practical form as a programming language The key idea: do everything by composing functions: No mutable state. No side effects.

Functional Programming Concepts Necessary features, many of which are missing in some imperative languages: 1st class and high-order functions. Serious polymorphism. Powerful list facilities. Structured function returns. Fully general aggregates. Garbage collection. So how do you get anything done in a functional language? Recursion (especially tail recursion) takes the place of iteration. In general, you can get the effect of a series of assignments x := 0 ... x := expr1 ... x := expr2 ... from f3(f2(f1(0))), where each f expects the value of x as an argument, f1 returns expr1, and f2 returns expr2.

Recursion Recursion even does a nifty job of replacing looping: x := 0; i := 1; j := 100; while i < j do x := x + i*j; i := i + 1; j := j - 1 end while return x becomes f(0,1,100), where: f(x, i, j) == if i < j then f(x+i*j, i+1, j-1) else x Thinking about recursion as a direct, mechanical replacement for iteration, however, is the wrong way to look at things: One has to get used to thinking in a recursive style.

Higher-Order Functions Even more important than recursion is the notion of higher- order functions. Take a function as argument, or return a function as a result. Great for building things. Why higher-order functions are not more common in imperative programming languages? Depends on the ability to create new functions on the fly: we need a function constructor – a significant departure from the syntax and semantics of traditional imperative languages. The ability to specify functions as return values or to store them in variables requires one of the following: Eliminate function nesting. Give local variables unlimited extent.

Lisp The first functional language. Lisp also has (these are not necessary present in other functional languages): Homo-iconography: Homogeneity of programs and data – a program in Lisp is itself a list, and can be manipulated with the same mechanisms used to manipulate data. Self-definition: the operational semantics of Lisp can be defined elegantly in terms of an interpreter written in Lisp. Read-evaluate-print: interaction with the user. Variants of Lisp: Pure (original) Lisp. Interlisp, MacLisp, Emacs Lisp. Common Lisp. Scheme.

Other Functional Languages Pure Lisp is purely functional; all other Lisps have imperative features. All early Lisps dynamically scoped:. Not clear whether this was deliberate or if it happened by accident. Scheme and Common Lisp statically scoped: Common Lisp provides dynamic scope as an option for explicitly- declared special functions. Common Lisp now THE standard Lisp: Very big; complicated (The Ada of functional programming). Scheme is a particularly elegant Lisp. Other functional languages: ML, Miranda, Haskell, FP. Haskell is the leading language for research in functional programming.

Scheme We will use Scheme as a representative functional language. Homework 7 will be about Scheme programming. You need to install the programming languages Scheme on your computer: We will use Racket. Location: http://racket-lang.org/ Download and install software in time for the next lecture. As mentioned, Scheme is a particularly elegant Lisp: Interpreter runs a read-eval-print loop. Things typed into the interpreter are evaluated (recursively) once. Anything in parentheses is a function call (unless quoted). Parentheses are NOT just grouping, as they are in Algol-family languages: Adding a level of parentheses changes meaning.

Basics Boolean values #t and #f. Numbers. Lambda expressions. Quoting: (+ 3 4) ⇒ 7 (quote (+ 3 4)) ⇒ (+ 3 4) '(+ 3 4) ⇒ (+ 3 4) Creating a function using lambda expression: (lambda (x) (* x x)) ⇒ function Mechanisms for creating new scopes: (let ((square (lambda (x) (* x x))) (plus +)) (sqrt (plus (square a) (square b))))

Lists Scheme provides a wealth of functions to manipulate lists (recall Section 7.8). The three most important are: car: returns the head of a list. (car '(2 3 4)) ⇒ 7 cdr: returns the rest of the list. (cdr '(2 3 4)) ⇒ (3 4) cons: joins a head to the rest of the list. (cons 2 ’(3 4)) ⇒ (2 3 4) Empty list:() Null predicate null? determines if its argument is the empty list. Improper list: (cons 2 3)) ⇒ (2 . 3)

Control FLow Conditional expressions: (if (< 2 3) 4 5) ⇒ 4 (cond ((< 3 2) 1) ((< 4 3) 2) (else 3)) ⇒ 3 Imperative stuff: Assignments. Sequencing (begin). Iteration. I/O (read, display).

Standard Functions (Partial List) arithmetic boolean operators equivalence list operators symbol? number? complex? real? rational? Integer?

Example: Problem We'll invoke the program by calling a function called 'simulate', passing it a DFA description and an input string: The automaton description is a list of three items: Start state. The transition function. The set of final states. The transition function is a list of pairs: The first element of each pair is a pair, whose first element is a state and whose second element in an input symbol. If the current state and next input symbol match the first element of a pair, then the finite automaton enters the state given by the second element of the pair.

Example: Code

Example: Use

Summary A functional program computes principally through substitution of parameters into functions. The underlying model for functional programming is the lambda calculus. Lisp is the first functional language. We will use Scheme, a dialect of Lisp. Install Scheme (Racket) on your computer.