474NS)PROGRAMMING YOU FORGOT TO PUT PARENTHESSS ACTER MAIN. Lecture 20-More Scheme Chris Allsman MAE PARENS. &
Lecture 20 - More Scheme Chris Allsman
Lis(t)P(rocessing) s7% APR CALIFORNIA☑ CAR CDR LEY ENGINEERING 3
Lis(t) P(rocessing)
Scheme List Definition A Scheme list is either: nil Composed of a first element(car)and the rest(cdr)of the Scheme list Value of car is the 2 3 number 1 List terminated with nil car cdr car cdr car cdr cdr contains a pointer to a linked list
Scheme List Definition A Scheme list is either: ● nil ● Composed of a first element (car) and the rest (cdr) of the Scheme list 1 2 3 car cdr car cdr car cdr Value of car is the number 1 cdr contains a pointer to a linked list List terminated with nil
Creating Scheme Lists Demo 2 3 car cdr car cdr car cdr (cons 1 (cons1(cons2 ) (cons 1 (cons 2 (cons 3 nil))
Creating Scheme Lists 1 2 3 car cdr car cdr car cdr (cons 1 __________________________________) (cons 1 (cons 2 _________________________)) (cons 1 (cons 2 (cons 3 nil)) Demo
Scheme Lists vs.Linked Lists Scheme Lists: Linked Lists: (cons a b) Link(a,b) (car Ist) Ist.first (cdr Ist) Ist.rest nil Link.empty (1(23) >
Scheme Lists vs. Linked Lists Scheme Lists: (cons a b) (car lst) (cdr lst) nil (1 (2 3)) Linked Lists: Link(a, b) lst.first lst.rest Link.empty >
Checking Equivalence Demo (equal?e1 e2)checks if el and e2 evaluate to equivalent values ●Like=in Python ●(null?expr)is like(equal?expr ni1) (eq?e1 e2)checks if el and e2 evaluate to identical values ●Like is in Python Only matters for lists (e1 e2)only works for numbers
Checking Equivalence (equal? e1 e2) checks if e1 and e2 evaluate to equivalent values ● Like == in Python ● (null? expr) is like (equal? expr nil) (eq? e1 e2) checks if e1 and e2 evaluate to identical values ● Like is in Python ● Only matters for lists (= e1 e2) only works for numbers Demo
List Constructor Demo If we know each element we want to put in a list,we can use the list constructor The list constructor takes in any number of elements and puts each element as a single element in a list scm> (1ist123) scm>(list 0 (list 1 2 3)) (123) (0(123) 123 123N
List Constructor If we know each element we want to put in a list, we can use the list constructor The list constructor takes in any number of elements and puts each element as a single element in a list scm> (list 1 2 3) (1 2 3) scm> (list 0 (list 1 2 3)) (0 (1 2 3)) 1 2 3 1 2 3 0 Demo
Quoting Demo The quote special form takes in a single argument and returns an unevaluated version of the argument Quoting a symbol gives a symbol back,that symbol can mean something to scheme! Quoting the representation of a list gives a list scm>‘(abc) scm>‘(0(123)) (a b c) (8(123)) 123N
Quoting The quote special form takes in a single argument and returns an unevaluated version of the argument Quoting a symbol gives a symbol back, that symbol can mean something to scheme! Quoting the representation of a list gives a list scm> ‘(a b c) (a b c) scm> ‘(0 (1 2 3)) (0 (1 2 3)) a b c 1 2 3 0 Demo
Example-Add To End Given a scheme list,Ist,and an element,x,add x to the end of Ist (define (add-to-end lst x) scm> (add-to-end nil 1) (1) scm> (add-to-end (1 2)3) (123) scm> (add-to-end‘(123)‘(4)) (123(4))
Example - Add To End Given a scheme list, lst, and an element, x, add x to the end of lst (define (add-to-end lst x) ) scm> (add-to-end nil 1) (1) scm> (add-to-end ‘(1 2) 3) (1 2 3) scm> (add-to-end ‘(1 2 3) ‘(4)) (1 2 3 (4))
Using List Constructors
Using List Constructors