CS3L: Introduction to Symbolic Programming - PowerPoint PPT Presentation

About This Presentation
Title:

CS3L: Introduction to Symbolic Programming

Description:

Review of List stuff. Generalized Lists. Deep-add. list ... Makes the first arg the car of the new list. Makes the second arg the cdr of the new list ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 12
Provided by: collee2
Category:

less

Transcript and Presenter's Notes

Title: CS3L: Introduction to Symbolic Programming


1
CS3L Introduction to Symbolic Programming
Lecture 25 Trees and Generalized Lists
  • Summer 2008 Colleen Lewis
  • colleenL_at_berkeley.edu

2
Today
  • Review of List stuff
  • Generalized Lists
  • Deep-add

3
list
  • Takes any number of arguments and puts them in a
    list

4
cons
  • Takes two arguments
  • Makes the first arg the car of the new list
  • Makes the second arg the cdr of the new list
  • The second argument MUST be a list

5
append
  • Takes one or many lists and turns them into one
  • all arguments MUST be lists

6
car and cdr
  • (car (cdr (car (car L))))
  • (cadaar L)
  • (cddaadr L)
  • (cdr (cdr (car (car (cdr L)))))

7
Generalized Lists
  • ((this (is a (list)))(((with) only
    two)((elements))))
  • ((this (is a (list)))(((with) only
    two)((elements))))
  • ((this (is a (list)))(((with) only
    two)((elements))))

8
deep-add
  • (define (deep-add L)
  • (cond
  • ((null? L) 0)
  • ((number? (car L))
  • ( (car L) (deep-add (cdr L))))
  • (else
  • ( (deep-add (car L))
  • (deep-add (cdr L))))))

9
  • (deep-add '(1 (2 3) ((4) 5)))
  • ( 1 (deep-add ((2 3) ((4) 5)))
  • ( 1 ( (deep-add (2 3)) (deep-add ((4) 5)))
  • ( 1 ( ( 2 (deep-add (3))) (deep-add ((4)
    5)))
  • ( 1 ( ( 2 ( 3 (deep-add ())) (deep-add ((4)
    5)))
  • ( 1 ( ( 2 ( 3 0)) (deep-add
    ((4) 5)))

((null? L) 0) ((number? (car L)) ( (car
L) (deep-add (cdr L)))) (else ( (deep-add
(car L)) (deep-add (cdr L))))))
10
  • ( 1 ( ( 2 ( 3 0)) (deep-add
    ((4)) 5)))
  • ( 1 ( ( 2 ( 3 0)) (deep-add ((4) 5)))
  • ( 1 ( ( 2 ( 3 0))( (deep-add (4)) (deep-add
    (5))))
  • ( 1 ( ( 2 ( 3 0))( ( 4 (deep-add ()))
    (deep-add (5))))
  • ( 1 ( ( 2 ( 3 0))( ( 4 0)) (deep-add
    (5))))
  • ( 1 ( ( 2 ( 3 0))( ( 4 0) (deep-add (5))))

((null? L) 0) ((number? (car L)) ( (car
L) (deep-add (cdr L)))) (else ( (deep-add
(car L)) (deep-add (cdr L))))))
11
  • ( 1 ( ( 2 ( 3 0))( ( 4 0) (deep-add
    (5)))))
  • ( 1 ( ( 2 ( 3 0))( ( 4 0) ( 5 (deep-add
    ())))))
  • ( 1 ( ( 2 ( 3 0))( ( 4 0) ( 5 0))))
  • ( 1 ( ( 2 ( 3 0))( ( 4 0) ( 5 0))))
  • (deep-add '(1 (2 3) ((4) 5)))

((null? L) 0) ((number? (car L)) ( (car
L) (deep-add (cdr L)))) (else ( (deep-add
(car L)) (deep-add (cdr L))))))
Write a Comment
User Comments (0)
About PowerShow.com