Comparative Programming Languages Functional programming with Lisp/Scheme.

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.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
1 Programming Languages and Paradigms Lisp Programming.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
Functional Programming Languages
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
The Scheme Programming Language History and Significance Dmitry Nesvizhsky CIS24 Professor Danny Kopec.
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.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
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.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
Lisp by Namtap Tapchareon Lisp Background  Lisp was developed by John McCarthy in  Lisp is derives from List Processing Language. 
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
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.
Common lisp A functional programming language. Useful URL:
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
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.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
CS 331 Lisp/Scheme 1 Lisp/Scheme Ruth Rutter Functional and Logic Programming Team P-Phunck.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming Part 1. Organization of Programming Languages-Cheng Big Picture u What we’ve learned so far: Imperative Programming Languages 
ISBN Chapter 15 Functional Programming Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming Languages
Functional Programming Languages
Functional Programming
History of Computing – Lisp
Modern Programming Languages Lecture 20 Fakhar Lodhi
Functional Programming
Functional Programming Languages
Fundamentals of Functional Programming Languages
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
CS 36 – Chapter 11 Functional programming Features Practice
LISP A brief overview.
15.2 Mathematical Functions
Chapter 15 Functional Programming 6/1/2019.
Lisp.
Presentation transcript:

Comparative Programming Languages Functional programming with Lisp/Scheme

CS 363 Spring 2005 GMU2 Fundamentals of Functional Programming Languages The objective of the design of a functional programming language (FPL) is to mimic mathematical functions to the greatest extent possible The basic process of computation is fundamentally different in a FPL than in an imperative language –In an imperative language, operations are done and the results are stored in variables for later use –Management of variables is a constant concern and source of complexity for imperative programming In an FPL, variables are not necessary, as is the case in mathematics

CS 363 Spring 2005 GMU3 Lisp Lisp – based on lambda calculus (Church) –Uniform representation of programs and data using single general data structure (list) –Interpreter based (written in Lisp) –Automatic memory management –Evolved over the years –Dialects: COMMON LISP, Scheme

CS 363 Spring 2005 GMU4 Scheme Scheme is a functional programming language and a dialect of Lisp. It was developed by Guy L. Steele and Gerald Jay Sussman at MIT Use Programming Languages ->Dr. Scheme here in the Comp Sys lab

CS 363 Spring 2005 GMU5 Common Lisp Use: clisp for Common Lisp here in the systems lab

CS 363 Spring 2005 GMU6 Scheme (dr scheme, guile) (define (gcd u v) (if ( = v 0) u (gcd v (remainder u v)) ) (define (reverse l) (if (null? l) l (append (reverse (cdr l))(list (car l))) )

CS 363 Spring 2005 GMU7 Scheme (dr scheme, guile) Using guile (gnu scheme): (load "slides.scm") (gcd ) --> 4 (reverse '( )) --> ( )

8 Common Lisp (clisp) (defun mygcd (u v) (if (= v 0) u (mygcd v (rem u v)) ) (defun myreverse (l) (if (null l) l (append (myreverse (cdr l))(list (car l))) ) ;; (load "slides.lsp"), (mygcd ) --> 4 ;; (myreverse '( )) --> ( )

9 Scheme (define (gcd u v) (if ( = v 0) u (gcd v (remainder u v)) ) Once defined in the interpreter:  (gcd 25 10)  5

10 Scheme/Lisp expression → atom | list atom → number | string | identifier | character | boolean list → ‘(‘ expression-sequence ‘)’ expression-sequence → expression | expression-sequence expression

11 Scheme Expression vs. C In Scheme: (+ 3 (* 4 5 )) (and (= a b)(not (= a 0))) (gcd 10 35) In C: * 5 (a = = b) && (a != 0) gcd(10,35)

12 Evaluation Rules for Scheme Expressions 1.Constant atoms (numbers, strings) evaluate to themselves 2.Identifiers are looked up in the current environment and replaced by the value found there (using dynamically maintained symbol table) 3.A list is evaluated by recursively evaluating each element in the list as an expression; the first expression must evaluate to a function. The function is applied to the evaluated values of the rest of the list.

13 Scheme Evaluation To evaluate (* (+ 2 3)(+ 4 5)) 1.* is the function – must evaluate the two expressions (+ 2 3) and (+ 4 5) 2.To evaluate (+ 2 3) 1.+ is the function – must evaluation the two expressions 2 and evaluates to the integer evaluates to the integer = 5 3.To evaluate (+ 4 5) follow similar steps 4.* 5 9 = 45 *

14 Scheme Conditionals If statement: (if ( = v 0) u (gcd v (remainder u v)) ) (if (= a 0) 0 (/ 1 a)) Cond statement: (cond (( = a 0) 0) ((= a 1) 1) (else (/ 1 a)) )

15 Example of COND (Scheme) (DEFINE (compare x y) (COND ((> x y) (DISPLAY “x is greater than y”)) ((< x y) (DISPLAY “y is greater than x”)) (ELSE (DISPLAY “x and y are equal”)) )

16 Example of COND (Lisp) (defun compare (x y) (COND ((> x y)(format t “x is greater than y”)) ((< x y)(format t “y is greater than x”)) (t (format t “x and y are equal”)) )

17 Predicate Functions (Scheme) EQ? takes two symbolic parameters; it returns #T if both parameters are atoms and the two are the same e.g., (EQ? 'A 'A) yields #T (EQ? 'A '(A B)) yields () –Note that if EQ? is called with list parameters, the result is not reliable –EQ? does not work for numeric atoms (use = )

18 Predicate Functions (Lisp) EQ takes two symbolic parameters; it returns #T if both parameters are atoms and the two are the same e.g., (eq 'A 'A) yields #T (eq 'A '(A B)) yields nil

19 Predicate Functions (Scheme) LIST? takes one parameter; it returns #T if the parameter is a list; otherwise () 3. NULL? takes one parameter; it returns #T if the parameter is the empty list; otherwise () Note that NULL? returns #T if the parameter is () 4. Numeric Predicate Functions =, <>, >, =, <=, EVEN?, ODD?, ZERO?, NEGATIVE?

20 Predicate Functions (Lisp) LISTP takes one parameter; it returns #T if the parameter is a list; otherwise nil 3. NULL takes one parameter; it returns #T if the parameter is the empty list; otherwise nil Note that NULL returns #T if the parameter is () 4. Numeric Predicate Functions =, <>, >, =, <=, EVENP, ODDP, ZEROP