CSE 341 - S. Tanimoto More-Concepts - 1 More Programming Language Concepts Currying Lazy Evaluation Polymorphism.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
More about functions Plus a few random things. 2 Tail recursion A function is said to be tail recursive if the recursive call is the very last thing it.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
1 Programming Languages and Paradigms Lisp Programming.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
CSE 115 Week 11 March , Announcements March 26 – Exam 7 March 26 – Exam 7 March 28 – Resign Deadline March 28 – Resign Deadline Lab 7 turned.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
CSE S. Tanimoto Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
CSE S. Tanimoto Introduction 1 LISP Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Genetic Programming.
CSE 341, S. Tanimoto Concepts 1- 1 Programming Language Concepts Formal Syntax Paradigms Data Types Polymorphism.
CSE S. Tanimoto Lisp Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Common lisp A functional programming language. Useful URL:
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Chapter 9: Functional Programming in a Typed Language.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
CSE (c) S. Tanimoto, 2002 AI Techniques 1 Where and When Do Symbols Refer to Values and Functions? Scope and Extent of Bindings Bindings Scope Extent.
CSE 341, S. Tanimoto Lisp LISP LISP = LISt Processing Intended for processing symbolic information Implementations from noncommercial sites: GNU.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Functional Programming
Functional Programming
Lisp S-Expressions: ATOMs
Defining Macros in Lisp
Modern Programming Languages Lecture 20 Fakhar Lodhi
FP Foundations, Scheme In Text: Chapter 14.
LISP LISP = LISt Processing
CS 36 – Chapter 11 Functional programming Features Practice
Scheme: Basic Functionality
Bindings, Scope, and Extent
CSE S. Tanimoto Introduction
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
CSE S. Tanimoto Introduction
Explicit Application of Procedures, and Explicit Evaluation
Functional Programming Concepts
Lisp: Using Functions as Data
CSE S. Tanimoto Introduction
Functional Programming Concepts
Lisp: Representation of Data
Defining Macros in Lisp
Defining Functions with DEFUN
Peter Seibel Practical Common Lisp Peter Seibel
Abstraction and Repetition
Lisp: Using Functions as Data
Bindings, Scope, and Extent
LISP: Basic Functionality
CSE S. Tanimoto Lambda Calculus
Modern Programming Languages Lecture 18 Fakhar Lodhi
Functional Programming Concepts
LISP: Basic Functionality
Bindings, Scope, and Extent
Common Lisp II.
LISP: Basic Functionality
Lisp.
Lisp: Representation of Data
LISP primitives on sequences
Presentation transcript:

CSE S. Tanimoto More-Concepts - 1 More Programming Language Concepts Currying Lazy Evaluation Polymorphism

CSE S. Tanimoto More-Concepts - 2 Currying in Functional Languages Q. How can two functions be combined to create a new function? A. By composition. h(x) = g(f(x)) Q. How can a function and one value be combined to create a new function? A. By currying. h(x) = f(a, x) h2(x, y) = f2(a, x, y)

CSE S. Tanimoto More-Concepts - 3 Currying in Miranda Q. How does Miranda support currying? A. Any function that takes multiple arguments can be curried. mult x y = x * y triple = mult 3 Here mult is being curried with 3 producing a new function triple. Miranda defines a function of n arguments to be equivalent to a function of one argument that returns another function, that function taking n-1 arguments.

CSE S. Tanimoto More-Concepts - 4 Lazy Evaluation Q. What is lazy evaluation? A. It's a policy of only evaluating forms whose values are needed by a consumer, such as a print request. If Lisp fully supported lazy evaluation, then (FIRST (LIST 1 (+ 2 3) (* 4 5) (/ 6 7))) would not result in any arithmetic actually being performed, since only the element 1 needs to be returned.

CSE S. Tanimoto More-Concepts - 5 Lazy Evaluation - Lisp? Q. Does Lisp support lazy evaluation? A. Only in some limited ways... The special forms IF and COND, and the macros AND and OR perform lazy evaluation of their arguments. (or (= n 2) (= n 3) (= n 5) (= n 7) (= n 11) ‘not-a-small-prime) If n is 2, then only the first comparison is performed.

CSE S. Tanimoto More-Concepts - 6 Lazy Evaluation - Efficiency? Q. Does lazy evaluation always lead to more efficient computation? A. No. Consider the Miranda code double x = x + x double 11*13 This evaluates as 11* *13 = = 186 But with applicative order evaluation we would have double 11*13 = double 143 = = 186 which performs less arithmetic. Also, lazy evaluation usually incurs extra overhead.

CSE S. Tanimoto More-Concepts - 7 Lazy Evaluation and Infinite Data Structures Q. Are there any other benefits of lazy evaluation? A. Yes, it permits creation and manipulation of "infinite" data structures. In Miranda, the list (1, 2, 3,... ) is written [1..] and can be used in expressions. hd (tl (tl (map triple [1..]))) || returns 9 If you told Miranda to compute the length of [1..], however, it would loop indefinitely.

CSE S. Tanimoto More-Concepts - 8 Polymorphism Q. What is polymorphism? A. The property of a programming language feature or entity being able to support multiple types of data. Q. What is usually meant by function polymorphism? A. The ability for one function to accept arguments whose types can vary from one call to another. In Lisp, for example, CONS is polymorphic: (cons 1 2) ; numeric arguments (cons t nil) ; symbolic arguments Note: CONS is not overloaded; it's polymorphic.

CSE S. Tanimoto More-Concepts - 9 Polymorphism Q. Is there another way for a function to exhibit polymorphism? A. Yes, it can return different types of values. For example, the Lisp function given by (defun check-for-positive (n) (if (< n 0) 'negative n) ) may return either a number or a symbol, depending on the value of n.

CSE S. Tanimoto More-Concepts - 10 Polymorphic Variables Q. What is a polymorphic variable? A. It’s a variable that can hold values of more than one type. In Lisp, symbols often serve as polymorphic variables: (setq x 5) (setq x ‘apple) (setq x ‘(peaches and pears))

CSE S. Tanimoto More-Concepts - 11 Polymorphism in Java Q. Does Java support polymorphism? A. Yes, via the use of the built-in, general class Object. public class Widget extends Object { private String name; public void setName(String n) {name = n;} } public Widget test(Widget w) { Widget w2 = (Widget) w.clone(); w2.setName(“The cloned Widget”); return w2; }