![Page 1: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/1.jpg)
CSC3315 (Spring 2009) 1
CSC 3315CSC 3315Programming Programming ParadigmsParadigmsScheme LanguageScheme Language
Hamid HarroudHamid HarroudSchool of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn
UniversityUniversityhttp://www.aui.ma/~H.Harroud/csc3315/
![Page 2: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/2.jpg)
Functional Programming & Functional Programming & LispLisp
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
![Page 3: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/3.jpg)
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 & Functional Programming & SchemeScheme
![Page 4: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/4.jpg)
Domains: non numerical applications, especially: AI (expert systems, natural languages,...) Automatic reasoning (proof of theorems,
proof of programs,...) Formal calculus Games
Functional Programming & Functional Programming & SchemeScheme
![Page 5: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/5.jpg)
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 & Functional Programming & SchemeScheme
![Page 6: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/6.jpg)
Basic ExpressionsBasic Expressions Numbers: integers / floats. A variable is a name associated to a data, for example:
(define pi 3.14159) ; 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 3.141592)
(set! pi 'alpha)
(set! pi (cons pi '(rho))) A variable can be local to a block: (let ((var1 E1)(var2 E2)...) <expr>)
![Page 7: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/7.jpg)
Composed ExpressionsComposed Expressions
The general format of a list is:(E1 E2 ...... En) where Ei 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)
![Page 8: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/8.jpg)
Defining a FunctionDefining 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
![Page 9: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/9.jpg)
> (define (f x) (* (+ x 1)(- x 1))) > (define (fact n) ( if (> n 0)
( * n (fact (- n 1))) 1 ) )
>(fact 40)
815915283247897734345611269596115894272000000000
Defining a FunctionDefining a Function
![Page 10: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/10.jpg)
QuoteQuote
quote avoids the evaluation of an argument (expression or atom):
(quote pi) or 'pi If pi est defined as: (define pi 3.141592)
(write 'pi) displays pi symbol (write pi) displays 3.141592 (* 2.0 pi) returns 6.283184 (* 2.0 'pi) invalid parameter
![Page 11: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/11.jpg)
Primitive FunctionsPrimitive 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
![Page 12: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/12.jpg)
Primitive FunctionsPrimitive 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.
![Page 13: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/13.jpg)
More FunctionsMore 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")
![Page 14: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/14.jpg)
More Functions: ExampleMore Functions: Example
> ( define ( NumberLists? x ) ( if ( not ( list? x ) ) #f ( if ( null? x ) #t ( if ( not ( number? ( car x ) ) ) #f ( NumberLists? ( cdr x )) ) ) ) )
> ( NumberLists? '( 1 2 3 4 ))#t
![Page 15: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/15.jpg)
> ( define ( numberList? x ) ( cond ( ( not ( list? x ) ) #f ) ( ( null? x ) #t ) ( ( not ( number? ( car x ) ) ) #f ) ( else ( numberList? ( cdr x ) ) )) )> ( numberList? ' ( 1 2 3 4 ) )#t> ( numberList? ' ( 1 2 3 bad 4 ) )#f
More Functions: ExampleMore Functions: Example
![Page 16: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/16.jpg)
More FunctionsMore 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), ...
![Page 17: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/17.jpg)
Example 2Example 2(define (same_neighbours? l)
(cond
((null? l) #f)
((null? (cdr l)) #f)
((equal? (car l)(cadr l)) #t)
(else
(same_neighbours? (cdr l)))
) )
![Page 18: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/18.jpg)
> ( 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 3Example 3
![Page 19: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/19.jpg)
>(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 4Example 4
![Page 20: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/20.jpg)
>(define reverse(lambda (x) (if (null? x) ’()
(append (reverse (cdr x)) (list (car x))) )) )
> (reverse '(a b c))(c b a)
More ExamplesMore Examples
![Page 21: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/21.jpg)
CSI2520, Hiver 2007
(define (empty?empty? stack)
(null? stack)
)
(define (poppop stack)
(if (empty? stack)
()
(cdr stack)
) )
(define (pushpush e stack)
(cons e stack)
)
(define (toptop stack)
(if (empty? stack)
()
(car stack)
) )
Pile en SchemePile en Scheme
![Page 22: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/22.jpg)
(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 ListMinimum of a List
![Page 23: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/23.jpg)
(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 Minimum of a List: local variablesvariables
![Page 24: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/24.jpg)
(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 listsConcatenation of two lists
![Page 25: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/25.jpg)
(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))> (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 Member Function
![Page 26: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/26.jpg)
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: High-level functions: Functions as ParametersFunctions as Parameters
![Page 27: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/27.jpg)
(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: High-level functions: Functions as ParametersFunctions as Parameters
![Page 28: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/28.jpg)
(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:(E1 E2 ... En) ((f E1) (f E2) ... (f En))
For example(map (lambda(x) (+ x 1)) '(1 2 3))returns: (2 3 4)
High-level Functions: mapHigh-level Functions: map
![Page 29: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/29.jpg)
( 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: mapHigh-level Functions: map
![Page 30: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/30.jpg)
Consider a function F with two parameters, and F0 a constant.
We want to have the following transformation
(F E1 (F E2 (F ... (F En F0) ) ...)
Which can be represented using an infix notation:
(E1 E2 ...... En) E1 F E2 F ...... F En F F0
High-level functions: High-level functions: ReducersReducers
![Page 31: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/31.jpg)
Example:
(E1 E2 ...... En) E1 + E2 + ...... + En + 0
(E1 E2 ...... En) E1 * E2 * ...... * En * 1
(define (reduce F F0 L)
(if (null? L)
F0
(F (car L)
(reduce F F0 (cdr L)))
))
High-level functions: High-level functions: ReducersReducers
![Page 32: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/32.jpg)
> (reduce + 0 '(1 2 3 4))
> (reduce * 1 '(1 2 3 4))
> (reduce (lambda (x y) (+ x y 1)) 8 '(1 2 3))
> (reduce cons '() '(1 2 3 4))
> (reduce append '() '((1) (2) (3)))
High-level functions: High-level functions: ReducersReducers
![Page 33: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/33.jpg)
Lists Manipulation
carcdrconsappendlistlengthcaar, cadr, cdar, cddr,caaaar, cddddr, ...
To define Functions
definelambda
Pre-defined FunctionsPre-defined Functions
![Page 34: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/34.jpg)
Logic
not and or #t #f
Control
ifcondelselet
Arithmetic and comparison + < - > * <= / >= max = min
Pre-defined FunctionsPre-defined Functions
![Page 35: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/35.jpg)
Predicatessymbol? number? integer? list? null? eq? equal? procedure?member?
I/O Functions readwrite display print
Pre-defined FunctionsPre-defined Functions
![Page 36: CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315](https://reader035.vdocuments.mx/reader035/viewer/2022070409/56649e9e5503460f94b9f21b/html5/thumbnails/36.jpg)
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 FunctionsPre-defined Functions