CSI2520  Un arbre binaire peut être représentée avec des listes imbriquées a / \ b c / \ d e (a b (c d e)) ou (a (b () ()) (c (d () ()) (e () ())) ou.

Slides:



Advertisements
Similar presentations
Joshua Eckroth The Plan 1.Review some functions. 2.Write more functions. 3.Consider the nature of recursion. 4.Look at the.
Advertisements

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
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.
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)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
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)
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.
מבוא מורחב 1 Lecture #7. מבוא מורחב 2 The rational number abstraction Wishful thinking: (make-rat ) Creates a rational number (numer ) Returns the numerator.
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.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
( (lambda (z) (define x (lambda (x) (lambda (y z) (y x)))) ( ( (x (lambda () z)) (lambda (z) z) 3 ) ) ) 2)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
CS3L: Introduction to Symbolic Programming Summer 2008Colleen Lewis Lecture 25: Trees and Generalized Lists.
Programming Languages I LISP
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
PPL Pairs, lists and data abstraction. Data Abstraction? An interface: separate implementation from usage Think of the Map interface in Java: we know.
Spring 2008Programming Development Techniques 1 Topic 6 Hierarchical Data and the Closure Property Section September 2008.
1 (Functional (Programming (in (Scheme)))) Jianguo Lu.
Plt /8/ Inductive Sets of Data Programming Language Essentials 2nd edition Chapter 1.2 Recursively Specified Programs.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Introduction to Scheme. Lisp and Scheme Lisp: List processor language –Full recursion –Conditional expression –Extensibility –Interactive Scheme: one.
1 You’re Invited! Course VI Freshman Open House! Friday, April 7, :30-5:00 PM FREE Course VI T-Shirts (while supplies last) and Department.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Spring 2004Programming Development Techniques 1 Topic 11 Sets and their Representation April 2004.
Functional Programming: Lisp MacLennan Chapter 10.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 8. Outline 1.The special form quote 2.Data abstraction: Trie 3.Alternative list: Triplets 4.Accumulate-n.
CS61A Lecture Colleen Lewis. Clicker poll Do you feel comfortable posting questions to piazza? A)Yes with my classmates and instructors.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
Principles Of Programming Languages Lecture 2 Today Design-By-Contract Data Structures: Lists and Pairs Iteration vs. Recursion If we have time: live.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Additional Scheme examples
CS 550 Programming Languages Jeremy Johnson
Representing Sets (2.3.3) Huffman Encoding Trees (2.3.4)
Introduction to Scheme
Racket CSC270 Pepper major portions credited to
Chapter 15 – Functional Programming Languages
CS 270 Math Foundations of CS Jeremy Johnson
COP4020 Programming Languages
Abstract Syntax Prabhaker Mateti 1.
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Lecture 18.
Lecture #8 מבוא מורחב.
CS 36 – Chapter 11 Functional programming Features Practice
The Metacircular Evaluator (Continued)
Chapter 1 Review: BNF Grammar for lists:
Lecture #9 מבוא מורחב.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
topics mutable data structures
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
6.001 SICP Data abstractions
Lecture #7 מבוא מורחב.
List and list operations (continue).
Functional Programming: Lisp
Lecture # , , , , מבוא מורחב.
To understand recursion, one must first understand recursion.
More Scheme CS 331.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

CSI2520  Un arbre binaire peut être représentée avec des listes imbriquées a / \ b c / \ d e (a b (c d e)) ou (a (b () ()) (c (d () ()) (e () ())) ou (a b.(c d.e))

CSI2520 (define arbre? (lambda (t) (cond ((not (list? t)) #f) ((null? t) #t) ((not (= (length t) 3)) #f) ((not (arbre? (cadr t))) #f) ((not (arbre? (caddr t))) #f) (else #t) ) ) ) (arbre? '(73 (31 (5 () ()) ()) (101 (83 () (97 () ())) ()))) (arbre? '(73 (31 (5 () ()) ()) (101 (83 () (97 () () ())) ()))) (arbre? '(73 (31 (5 () ()) ()) (101 (83 () (97 ())) ())))

CSI2520 (define inordre (lambda (t) (define do-inordre (lambda (t) (if (null? t) '() (append (inordre (cadr t)) (cons (car t) (do-inordre (caddr t)))) ) ) ) (if (not (arbre? t)) (list ‘erreur-arbre-invalide t) (do-inordre t) ) ) ) (inordre '(73 (31 (5 () ()) ()) (101 (83 () (97 () ())) ())))

CSI2520 (define cherche (lambda (x t) (define do-cherche (lambda (x t) (cond ((null? t) #f) ((equal? x (car t)) #t) ((precedes? x (car t)) (do-cherche x (cadr t))) ((precedes? (car t) x) (do-cherche x (caddr t))) (else #f) ) ) ) (if (not (arbre? t)) (list 'erreur-arbre-invalide t) (do-cherche x t) ) ) ) (define precedes? (lambda (x y) (< x y))) (cherche 83 '(73 (31 (5 () ()) ()) (101 (83 () (97 () ())) ()))) (cherche 84 '(73 (31 (5 () ()) ()) (101 (83 () (97 () ())) ())))

CSI2520 (define (arbre-insere arbre valeur) (cond ((null? arbre) (list valeur '() '())) ((< valeur(car arbre)) (list (car arbre) (arbre-insere (cadr arbre) valeur) (caddr arbre))) (else (list (car arbre) (cadr arbre) (arbre-insere (caddr arbre) valeur)))))

CSI2520 (define detruire (lambda (x t) (if (not (arbre? t)) (list 'erreur-arbre-invalide t) (do-detruire x t) ) ) )

CSI2520 (define do-detruire (lambda (x t) (cond ((null? t) '()) ((and (equal? x (car t)) (null? (cadr t))) (caddr t)) ((and (equal? x (car t)) (null? (caddr t))) (cadr t)) ((equal? x (car t)) (let ((r (removemax (cadr t)))) (list (cdr r) (car r) (caddr t)) )) ((precedes? x (car t)) (list (car t) (do-detruire x (cadr t)) (caddr t))) ((precedes? (car t) x) (list (car t) (cadr t) (do-detruire x (caddr t)))) (else t) ) ) )

CSI2520 (define removemax (lambda (t) (cond ((null? (caddr t)) (cons (cadr t) (car t))) (else (let ((r (removemax (caddr t)))) (cons (list (car t) (cadr t) (car r)) (cdr r)) )) ) ) )

CSI2520 (define (nsymbols tree) (if (list? tree) (+ (nsymbols (car tree)) (nsymbols (cdr tree))) (if (symbol? tree) 1 0) ) ) > (nsymbols '(+ a (* b c))) 5 - Représentation sans les ( )

CSI2520 (define (nsymbols tree) (nsymbolsb tree 0)) (define (nsymbolsb tree n) (if (list? tree) (nsymbolsb (cdr tree) (nsymbolsb (car tree) n)) (+ n (if (symbol? tree) 1 0)) ) )

CSI2520 (define start ‘((1 2 3) (4 5 6) (7 8 9) (1 4 7) (2 5 8) (3 6 9) (1 5 9) (3 5 7))) X joue: ( (X 2 3) (4 5 6) (7 8 9) (X 4 7) (2 5 8) (3 6 9) (X 5 9) (3 5 7)) O joue: ((X 2 3) (4 5 6) (7 O 9) (X 4 7) (2 5 O) (3 6 9) (X 5 9) (3 5 7)) X O

CSI2520 (define subst (lambda (new old l) (cond ((null? l) (quote ())) ((atom? (car l)) (cond ((eq? (car l) old) (cons new (subst new old (cdr l)))) (else (cons (car l) (subst new old (cdr l)))))) (else (cons (subst new old (car l)) (subst new old (cdr l))))))) La substitution:

CSI2520 (define (all-equal? list) (cond ((null? list) '()) ((null? (cdr list)) (car list)) ((equal? (car list) (cadr list)) (all-equal? (cdr list))) (else #f))) Egalité de tous les éléments d’une liste?

CSI2520 (define (play board player position) (subst player position board)) (define (winner board) (map all-equal? board)) > (winner '((X 2 3) (4 X X) (7 O O) (X X X) (2 X O) (3 X O) (X X O) (3 X 7))) (#f #f #f X #f #f #f #f)

CSI2520 (define (number-of-member x list) (cond ((null? list) 0) ((equal? x (car list)) (+ 1 (number-of-member x (cdr list)))) (else (number-of-member x (cdr list))))

CSI2520 X XX OO (map (lambda (list) (number-of-member ‘X list)) ‘((X 2 3) (4 X X) (7 O O) (X 4 7) (2 X O) (3 X O) (X X O) (3 X 7)))) ( )

CSI2520 (define (dohanoi n to from using) (if (> n 0) (begin (dohanoi (- n 1) using from to) (display "move ") (display from) (display " --> ") (display to) (newline) (dohanoi (- n 1) to using from) ) ) (define (hanoi n) (dohanoi n 3 1 2) )

 Avec une liste d’adjacence (define Graph '((1 (2 4)) (2 ()) (3 (2)) (4 (1 5)) (5 (1 2)) (define valeur car) (define enfants cadr) (define premier-enfant caadr)

(define (noeud x graphe) (cond ((null? graphe) '()) ((equal? x (valeur (car graphe))) (car graphe)) (else (noeud x (cdr graphe)) ) ))

 En profondeur  Parcours d’une liste de noeuds étant donné une liste de noeuds déjà visités (define (parcours x graphe) (reverse (parcours-liste-noeuds (list x) graphe '()))) (define (parcours-liste-noeuds liste graphe visites) (cond ((null? liste) visites) ((member (car liste) visites) ; deja visite? (parcours-liste-noeuds (cdr liste) graphe visites)) (else (parcours-liste-noeuds ( append (enfants (noeud (car liste) graphe)) (cdr liste)) graphe (cons (car liste) visites))))