Lists in Lisp and Scheme

Similar documents
Lists in Lisp and Scheme

Functional Programming. Pure Functional Programming

CSc 520. Principles of Programming Languages 7: Scheme List Processing

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

A Brief Introduction to Scheme (II)

Func%onal Programming in Scheme and Lisp

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Func%onal Programming in Scheme and Lisp

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Programming with Pairs and Lists. Box-and-pointer diagrams for cons trees

CSC 533: Programming Languages. Spring 2015

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Pairs and Lists in Racket. Box-and-pointer diagrams for cons trees

Lisp. Versions of LISP

Introduction to Scheme

Project 2: Scheme Interpreter

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

User-defined Functions. Conditional Expressions in Scheme

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

An Explicit-Continuation Metacircular Evaluator

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Functional Languages. Hwansoo Han

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CSCC24 Functional Programming Scheme Part 2

Building a system for symbolic differentiation

A brief tour of history

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Intro. Scheme Basics. scm> 5 5. scm>

Building a system for symbolic differentiation

CS 360 Programming Languages Interpreters

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Functional Programming. Pure Functional Languages

Scheme: Strings Scheme: I/O

Functional Programming. Pure Functional Languages

University of Massachusetts Lowell

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

Vectors in Scheme. Data Structures in Scheme

Programming Languages

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

FP Foundations, Scheme

Simplifying Logical Formulas

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CS 61A, Fall, 2002, Midterm #2, L. Rowe. 1. (10 points, 1 point each part) Consider the following five box-and-arrow diagrams.

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

LECTURE 16. Functional Programming

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

Lexical vs. Dynamic Scope

Lisp Basic Example Test Questions

Organization of Programming Languages CS3200/5200N. Lecture 11

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Scheme Quick Reference

CS61A Midterm 2 Review (v1.1)

CS 314 Principles of Programming Languages. Lecture 16

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13


Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

4/19/2018. Chapter 11 :: Functional Languages

CSCI337 Organisation of Programming Languages LISP

Sample Final Exam Questions

Lecture Notes on Lisp A Brief Introduction

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

CS61A Notes Week 9: Muta4on (solu4ons) Revenge of the Box- and- pointers

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Lecture #24: Programming Languages and Programs

CS 314 Principles of Programming Languages

Scheme Quick Reference

Discussion 12 The MCE (solutions)

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CSc 520 Principles of Programming Languages

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

CS 275 Name Final Exam Solutions December 16, 2016

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Functional programming with Common Lisp

CS 314 Principles of Programming Languages

Sample midterm 1 #1. Problem 1 (What will Scheme print?).

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Essentials of Programming Languages Language

Essentials of Programming Languages Language

INTRODUCTION TO SCHEME

6.001: Structure and Interpretation of Computer Programs

COP4020 Programming Assignment 1 - Spring 2011

CSE413 Midterm. Question Max Points Total 100

CMSC 331 Final Exam Section 0201 December 18, 2000

ALISP interpreter in Awk

CS 61A Discussion 8: Scheme. March 23, 2017

Artificial Intelligence Programming

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Transcription:

Lists in Lisp and Scheme a

Lists in Lisp and Scheme Lists are Lisp s fundamental data structures, but there are others Arrays, characters, strings, etc. Common Lisp has moved on from being merely a LISt Processor However, to understand Lisp and Scheme you must understand lists common func?ons on them how to build other useful data structures with them

Lisp Lists Lists in Lisp and its descendants are very simple linked lists Represented as a linear chain of nodes Each node has a (pointer to) a value (car of list) and a pointer to the next node (cdr of list) Last node s cdr pointer is to null Lists are immutable in Scheme Typical access pagern is to traverse the list from its head processing each node

In the beginning was the cons (or pair) What cons really does is combines two objects into a two- part object called a cons in Lisp and a pair in Scheme Conceptually, a cons is a pair of pointers - - the first is the car, and the second is the cdr Conses provide a convenient representa?on for pairs of any type The two halves of a cons can point to any kind of object, including conses This is the mechanism for building lists (pair? (1 2) ) => #t a null

Pairs Lists in Lisp and Scheme are defined as pairs Any non empty list can be considered as a pair of the first element and the rest of the list We use one half of a cons cell to point to the 1st element of the list, and the other to point to the rest of the list (either another cons or null) a

Box and pointer nota:on (a) Common notation: use diagonal line in cdr part of a cons cell for a pointer to null a a A one element list (a) null (a b c) a b c A list of three elements (a b c)

Z What sort of list is this? a d Z is a list with three elements: (i) the atom a, (ii) a list of two elements, b & c and (iii) the atom d. b c > (define Z (list a (list b c) d)) > Z (a (b c) d) > (car (cdr z))??

Pair? The func?on pair? returns true if its argument is a cons cell The equivalent func?on in CL is consp So list? could be defined: (define (list? x) (or (null? x) (pair? x))) Since everything that is not a pair is an atom, the predicate atom could be defined: (define (atom? x) (not (pair? x)))

Equality Each?me you call cons, Scheme allocates a new cons cell from memory with room for two pointers If we call cons twice with the same args, we get two values that look the same, but are dis?nct objects Ø (define L1 (cons 'a null)) Ø L1 (A) Ø (define L2 (cons 'a null))) Ø L2 Ø (A) Ø (eq? L1 L2) Ø #f Ø (equal? L1 L2) Ø #t Ø (and (eq? (car L1)(car L2)) (eq? (cdr L1)(cdr L2))) Ø #t

Equal? Do two lists have the same elements? Scheme provides a predicate equal? that is like Java s equal method Eq? returns true iff its arguments are the same object, and Equal?, more or less, returns true if its arguments would print the same. > (equal? L1 L2) #t Note: (eq? x y) implies (equal? x y)

Equal? (define (myequal? x y) ; this is ~ how equal? could be defined (cond ((number? x) (= x y)) ((not (pair? x)) (eq? x y)) ((not (pair? y)) #f) ((myequal? (car x) (car y)) (myequal? (cdr x) (cdr y))) (else #f)))

Use trace to see how it works > (require racket/trace) > (trace myequal?) > (myequal? '(a b c) '(a b c)) >(myequal? (a b c) (a b c)) > (myequal? a a) < #t >(myequal? (b c) (b c)) > (myequal? b b) < #t >(myequal? (c) (c)) > (myequal? c c) < #t >(myequal? () ()) <#t #t Trace is a debugging package showing what args a userdefined function is called with and what it returns The require function loads the package if needed

Does Lisp have pointers? A secret to understanding Lisp is to realize that variables have values in the same way that lists have elements As pairs have pointers to their elements, variables have pointers to their values Scheme maintains a data structure represen?ng the mapping of variables to their current values.

Variables point to their values > (define x (a b)) > x environment VAR VALUE (a b) > (define y x) y (a b) x y a b

Variables point to their values > (define x (a b)) > x (a b) > (define y x) y (a b) > (set! y (1 2) > y (1 2) environment VAR x y VALUE a 1 2 b

Does Scheme have pointers? The loca?on in memory associated with the variable x does not contain the list itself, but a pointer to it. When we assign the same value to y, Scheme copies the pointer, not the list. Therefore, what would be the value of Ø (eq? x y) #t or #f?

Length is a simple func:on on Lists The built- in func?on length takes a list and returns the number of its top- level elements Here s how we could implement it (define (length L) (if (null? L) 0 (+ 1 (length (cdr L)))) As typical in dynamically typed languages (e.g., Python), we do minimal type checking The underlying interpreter does it for us Get run-?me error if we apply length to a non- list

Building Lists list- copy takes a list and returns a copy of it The new list has the same elements, but contained in new pairs > (set! x (a b c)) (a b c) > (set! y (list- copy x)) (a b c) Spend a few minutes to draw a box diagram of x and y to show where the pointers point

Copy- list List- copy is a Lisp built- in (as copy- list) that could be defined in Scheme as: (define (list- copy s) (if (pair? s) (cons (list- copy (car s)) s)) (list- copy (cdr s))) Given a non- atomic s- expression, it makes and returns a complete copy (e.g., not just the top- level spine)

Append append returns the concatena?on of any number of lists Append copies its arguments except the last If not, it would have to modify the lists Such side effects are undesirable in func?onal languages >(append (a b) (c d)) (a b c d) > (append ((a)(b)) (((c)))) ((a) (b) ((c))) > (append (a b) (c d) (e)) (a b c d e) >(append (a b) ()) (a b) >(append (a b)) (a b) >(append) ()

Append The two argument version of append could be defined like this (define (append2 s1 s2) (if (null? s1) s2 (cons (car s1) (append2 (cdr s1) s2)))) No?ce how it ends up copying the top level list structure of its first argument

Visualizing Append > (load "append2.ss") > (define L1 '(1 2)) > (define L2 '(a b)) > (define L3 (append2 L1 L2)) > L3 (1 2 a b) > L1 (1 2) > L2 (a b) > (require racket/trace) > (trace append2) > (append2 L1 L2) >(append2 (1 2) (a b)) > (append2 (2) (a b)) > >(append2 () (a b)) < <(a b) < (2 a b) <(1 2 a b) (1 2 a b) Append does not modify its arguments. It makes copies of all of the lists save the last.

Visualizing Append > (load "append2.ss") > (define L1 '(1 2)) > (define L2 '(a b)) > (define L3 (append2 L1 L2)) > L3 (1 2 a b) > L1 (1 2) > L2 (a b) > (eq? (cdr (cdr L3) L2) #f environment VAR L2 L1 L3 VALUE 1 2 Append2 copies the top level of its first list argument, L1 a b

List access func:ons To find the element at a given posi?on in a list use the func?on list- ref (nth in CL) > (list- ref (a b c) 0) a To find the nth cdr, use list- tail (nthcdr in CL) > (list- tail (a b c) 2) (c) Both func?ons are zero indexed

List- ref and list- tail > (define L '(a b c d)) > (list- ref L 2) c > (list- ref L 0) a > (list- ref L - 1) list- ref: expects type <non- nega?ve exact integer> as 2nd arg, given: - 1; other arguments were: (a b c d) > (list- ref L 4) list- ref: index 4 too large for list: (a b c d) > (list- tail L 0) (a b c d) > (list- tail L 2) (c d) > (list- tail L 4) () > (list- tail L 5) list- tail: index 5 too large for list: (a b c d)

Defining Scheme s list- ref & list- tail (define (mylist- ref l n) (cond ((< n 0) (error...)) ((not (pair? l)) (error...)) ((= n 0) (car l)) (else (mylist- ref (cdr l) (- n 1))))) (define (mylist- tail l n) (cond ((< n 0) (error...)) ((not (pair? l)) (error...)) ((= n 0) (cdr l)) (else (mylist- ref (cdr l) (- n 1)))))

Accessing lists Scheme s last returns the last element in a list > (define (last l) (if (null? (cdr l)) (car l) (last (cdr l)))) > (last (a b c)) c Note: in CL, last returns last cons cell (aka pair) We also have: first, second, third, and CxR, where x is a string of up to four as or ds. E.g., cadr, caddr, cddr, cdadr,

Member Member returns true, but instead of simply returning t, its returns the part of the list beginning with the object it was looking for. > (member b (a b c)) (b c) member compares objects using equal? There are versions that use eq? and eqv? And that take an arbitrary funcgon

Defining member (define (member X L) (cond ((null? L) #f) ((equal? X (car L)) L) (else (member X (cdr L)))))

Memf If we want to find an element sa?sfying an arbitrary predicate we use the func?on memf: > (memf odd? (2 3 4)) (3 4) Which could be defined like: (define (memf f l) (cond ((null? l) #f) ((f (car l)) l) (else (memf f (cdr l)))))

DoQed pairs and lists Lists built by calling list are known as proper lists; they always end with a pointer to null A proper list is either the empty list, or a pair whose cdr is a proper list Pairs aren t just for building lists, if you need a structure with two fields, you can use a pair Use car to get the 1st field and cdr for the 2nd Ø (define the_pair (cons a b)) (a. b) Ø Pair isn t a proper list, so it s displayed in dot notagon In dot nota?on the car and cdr of each pair are shown separated by a period

DoQed pairs and lists A pair that isn t a proper list is called a doged pair Remember that a doged pair isn t really a list at all, It s a just a two part data structure Doted pairs and lists that end with a doged pair are not used very onen If you produce one for 331 code, you ve probably made an error a (a. b) b

Conclusion Simple linked lists were the only data structure in early Lisp versions From them you can build most other data structures though efficiency may be low Its s?ll the most used data structure in Lisp and Scheme Simple, elegant, less is more Recursion is the natural way to process lists