CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
CSE 341 Lecture 16 More Scheme: lists; helpers; let/let*; higher-order functions; lambdas slides created by Marty Stepp
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.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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).
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.
Common Lisp Derek Debruin Mark Larue Vinh Doan. Problem Domains There was a need in the early 1960s to define a programming language whose computational.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Programming Languages I LISP
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
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.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
Functional Programming in Scheme
ISBN Chapter 15 Functional Programming Languages.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 8.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
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.
Comparative Programming Languages Functional programming with Lisp/Scheme.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
Functional Programming
Functional Programming Languages
Functional Programming
History of Computing – Lisp
Section 15.4, 15.6 plus other materials
CS 326 Programming Languages, Concepts and Implementation
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
The Metacircular Evaluator
Functional Programming Languages
The Metacircular Evaluator
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
Defining Functions with DEFUN
Abstraction and Repetition
COP4020 Programming Languages
Programming Languages
More Scheme CS 331.
Lists in Lisp and Scheme
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University

Functional Programming & Lisp Designed by John McCarthy at MIT (1956 – 1959) for AI applications Derived from -calculus theory (allows functions to be values in an expression) Various dialects: Lisp 1.5 (1960), Scheme (1975), Common Lisp (1985)…[LISP = LISt Processor] Rich Language: functional, symbolic. Uniform Syntax and Semantics

In Scheme: Symbolic calculus. Basic objets : atoms (words), Atoms groups: lists (sentences). Atoms + Lists = Symbolic Expressions ( S-expr ) Scheme manipulates S-exprs: A scheme program is a S-expr  Programs and Data have the same representation. Functional Programming & Scheme

Domains: non numerical applications, especially: AI (expert systems, natural languages,...) Automatic reasoning (proof of theorems, proof of programs,...) Formal calculus Games Functional Programming & Scheme

Functional Programming: basic entity = function control structure = recursion A function is a first class that can be created, assigned to variables, passed as a parameter or returned as a value. Scheme is implemented as an interactif loop (read-eval-print loop). Functional Programming & Scheme

Basic Expressions Numbers: integers / floats. A variable is a name associated to a data, for example: (define pi ) ; pi is a global variable A variable has an implicit type, depending on its value. It can have a value of another type: (set! pi ) (set! pi 'alpha) (set! pi (cons pi '(rho))) A variable can be local to a block: (let ((var1 E1)(var2 E2)...) )

Composed Expressions The general format of a list is: (E 1 E E n ) where E i is a S-expression. A list can be processed as a data: '((William Shakespeare) (The Tempest)) or as a function call with variables passed by value: (append x y)

Defining a Function Two ways: > (define (carre x) (* x x)) or: >(define carre (lambda (x) (* x x))) >(carre 2) 4 Functions may not have names! >((lambda (x) (* x x)) 3) 9

> (define (f x) (* (+ x 1)(- x 1))) > (define (fact n) ( if (> n 0) ( * n (fact (- n 1))) 1 ) >(fact 40) Defining a Function

Quote quote avoids the evaluation of an argument (expression or atom): (quote pi) or 'pi If pi est defined as: (define pi ) (write 'pi) displays pi symbol (write pi) displays (* 2.0 pi) returns (* 2.0 'pi) invalid parameter

Primitive Functions cons, car, cdr are primitives (functions) that allow the construction and access to lists. lists are defined recusively: empty list: (), non-empty list: (cons   ) where  is a list. The head and the tail of a list: (car (cons   )) returns  (cdr (cons   )) returns  (car '()) and (cdr '()): invalid parameter

Primitive Functions Predicates: functions that return #t or #f. (symbol? x) #t if x is a symbol, (number? x) #t if x is a number, (eq? x y) #t if x and y are equal symbols.

More Functions Can be defined using primitive functions: (equal? x y) if x and y are identical objects (not necesary atoms) (null? x) if x is () – empty list. (append x y) concatenate x and y. (if (f...) (g...) "hello")

More Functions: Example > ( define ( NumberLists? x ) ( if ( not ( list? x ) ) #f ( if ( null? x ) #t ( if ( not ( number? ( car x ) ) ) #f ( NumberLists? ( cdr x ) ) ) ) ) ) > ( NumberLists? '( )) #t

> ( define ( numberList? x ) ( cond ( ( not ( list? x ) ) #f ) ( ( null? x ) #t ) ( ( not ( number? ( car x ) ) ) #f ) ( else ( numberList? ( cdr x ) ) ) ) > ( numberList? ' ( ) ) #t > ( numberList? ' ( bad 4 ) ) #f More Functions: Example

More Functions Elements Access in a list: (caar x)  (car (car x)) (cdadr x)  (cdr (car (cdr x)))) For example, the following evaluation is made in 4 steps: (caadar '((p ((q r) s) u) (v))) (caadr '(p ((q r) s) u)) (caar '(((q r) s) u)) (car '((q r) s)) '(q r) The second element of a list (if it exists): (cadr x) The third element : (caddr x), (cadddr x),...

Example 2 (define (same_neighbours? l) (cond ((null? l) #f) ((null? (cdr l)) #f) ((equal? (car l)(cadr l)) #t) (else (same_neighbours? (cdr l))) )

> ( define ( eqExpr? x y ) ( cond ( ( symbol? x ) ( eq? x y ) ) ( ( number? x ) ( eq? x y ) ) ; x is a list: ( ( null? x ) ( null? y ) ) ; x is non-empty list: ( ( null? y ) #f ) ( ( eqExpr? ( car x ) ( car y ) ) ( eqExpr? ( cdr x ) ( cdr y ) ) ) ( else #f ) ) Example 3

>(define (repeatedElems L) (if (list? L) (doRepeatedElems L) ‘list-error) ) (define (doRepeatedElems L) (cond ((null? L) '()) ((member (car L) (cdr L)) (doRepeatedElems (cdr L))) (else (cons (car L) (doRepeatedElems (cdr L)))) ) Example 4

>(define reverse (lambda (x) (if (null? x) ’() (append (reverse (cdr x)) (list (car x)) ) )) ) > (reverse '(a b c)) (c b a) More Examples

CSI2520, Hiver 2007 empty? (define (empty? stack) (null? stack) ) pop (define (pop stack) (if (empty? stack) () (cdr stack) ) push (define (push e stack) (cons e stack) ) top (define (top stack) (if (empty? stack) () (car stack) ) Pile en Scheme

(define (min x) (cond ((not (numberlist? x)) (list 'not-number-list x)) ((null? x) x) (else (min-aux (car x) (cdr x))) ) ) (define (min-aux elt x) (cond ((null? x) elt) ((> elt (car x) (min-aux (car x)(cdr x))) (else (min-aux elt (cdr x))) ) Minimum of a List

(define (minL-aux Elt Lst) (if (null? Lst) Elt (let ((v1 (car Lst)) (v2 (cdr Lst))) (if (> Elt v1) (minl-aux v1 v2) (minl-aux Elt v2) ) Minimum of a List: local variables

(define append (lambda (x y) (cond ((null? x) y) (else (cons (car x) (append (cdr x) y))) ) ) ) > (append '(a b c) '(d e)) (a b c d e) > (string-append “abc” “123”) “abc123” Concatenation of two lists

(define member? (lambda (x y) (cond ((null? y) #f) ((equal? x (car y)) #t) (else (member? x (cdr y))) ) ) ) > (member? 'aa '(bb ccc aa eeee rr ttttt)) #t > (member? 'aa '(bb ccc (aa) eeee rr ttttt)) #f > (member 'aa '(bb ccc aa eeee rr ttttt)) (aa eeee rr ttttt) Member Function

Some functions can have other functions as parameters. > (define (combine f1 f2 x) (f1 (f2 x)) ) > (combine (lambda (x) (+ 1 x)) (lambda (x) (* 2 x)) 6) 13 > (combine (lambda (x) (* 2 x)) (lambda (x) (+ 1 x)) 6) 14 Équivalent to: > ((lambda (x) (+ 1 x)) ((lambda (x) (* 2 x)) 6)) > ((lambda (x) (* 2 x)) ((lambda (x) (+ 1 x)) 6)) High-level functions: Functions as Parameters

(define f (lambda (x) (lambda (y) (lambda (z) (+ x y z) ) ) ) ) > (((f 1) 2) 3) 6 (define g ((f 1) 2)) >(g 3) 6 High-level functions: Functions as Parameters

(define (map F Lst) (if (null? Lst) Lst (cons (F (car Lst)) (map F (cdr Lst))) ) map: applies a function to each element of a list: (E 1 E 2... E n )  ((f E 1 ) (f E 2 )... (f E n )) For example (map (lambda(x) (+ x 1)) '(1 2 3)) returns: (2 3 4) High-level Functions: map

( map car '((a b) (c d) (e f)) ) ( map cadr '((a b) (c d) (e f)) ) ( map (lambda (x) (cons 3 (list x))) '(a b c d) ) High-level Functions: map

Consider a function F with two parameters, and F 0 a constant. We want to have the following transformation (F E 1 (F E 2 (F... (F E n F 0 ) )...) Which can be represented using an infix notation: (E 1 E E n )  E 1 F E 2 F F E n F F 0 High-level functions: Reducers

Example: (E 1 E E n )  E 1 + E E n + 0 (E 1 E E n )  E 1 * E 2 * * E n * 1 (define (reduce F F0 L) (if (null? L) F0 (F (car L) (reduce F F0 (cdr L))) )) High-level functions: Reducers

> (reduce + 0 '( )) > (reduce * 1 '( )) > (reduce (lambda (x y) (+ x y 1)) 8 '(1 2 3)) > (reduce cons '() '( )) > (reduce append '() '((1) (2) (3))) High-level functions: Reducers

Lists Manipulation car cdr cons append list length caar, cadr, cdar, cddr, caaaar, cddddr,... To define Functions define lambda Pre-defined Functions

Logic not and or #t #f Control if cond else let Arithmetic and comparison + < - > * <= / >= max = min Pre-defined Functions

Predicates symbol? number? integer? list? null? eq? equal? procedure? member? I/O Functions read write display print Pre-defined Functions

CSI2520, Hiver 2007 Other functions quote ---> ‘a, ‘(1 2) set! ---> (define a 1) (set! a (+ a 1)) load ---> (load “scm1.txt”) map eval ---> (eval ‘(+ 1 2)) apply ---> (apply + ‘(1 2)) Pre-defined Functions