CSC3315 (Spring 2009) 1 CSC 3315 CSC 3315 Programming Paradigms Programming Paradigms Scheme Language Scheme Language Hamid Harroud Hamid Harroud School of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn University University http://www.aui.ma/~H.Harroud/csc3315/
36
Embed
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University H.Harroud/csc3315
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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
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>)
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)
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
> (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
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
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
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.
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")
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
> ( 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
More FunctionsMore Functions Elements Access in a list:
The second element of a list (if it exists):(cadr x)
The third element : (caddr x), (cadddr x), ...
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)))
) )
> ( 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 )) )