Symbolic Computation and Common Lisp

Similar documents
Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

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

Common LISP Tutorial 1 (Basic)

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

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

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Project 2: Scheme Interpreter

CSCI-GA Scripting Languages

Documentation for LISP in BASIC

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

Recursion & Iteration

Allegro CL Certification Program

Common Lisp. Blake McBride

Functional programming with Common Lisp

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

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

MIDTERM EXAMINATION - CS130 - Spring 2005

Chapter 1. Fundamentals of Higher Order Programming

Functional Programming. Big Picture. Design of Programming Languages

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

Type Checking and Type Inference

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

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Organization of Programming Languages CS3200/5200N. Lecture 11

Debugging in LISP. trace causes a trace to be printed for a function when it is called

Lecture Notes on Lisp A Brief Introduction

Pierce Ch. 3, 8, 11, 15. Type Systems

SOFTWARE ARCHITECTURE 6. LISP

1.3. Conditional expressions To express case distinctions like

The Typed Racket Guide

Functional Programming. Pure Functional Programming

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

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial

Robot Programming with Lisp

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

Principles of Programming Languages 2017W, Functional Programming

Lecture #24: Programming Languages and Programs

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Typed Racket: Racket with Static Types

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

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

Introduction to Programming Using Java (98-388)

LECTURE 16. Functional Programming

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

Scheme as implemented by Racket

CSCI337 Organisation of Programming Languages LISP

FP Foundations, Scheme

CS 360 Programming Languages Interpreters

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

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

User-defined Functions. Conditional Expressions in Scheme

A Brief Introduction to Scheme (II)

Lecture #2 Kenneth W. Flynn RPI CS

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

A LISP Interpreter in ML

A Brief Introduction to Common Lisp

CS558 Programming Languages

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

Imperative languages

Robot Programming with Lisp

Func%onal Programming in Scheme and Lisp

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

INTRODUCTION TO SCHEME

Functional Programming. Pure Functional Languages

Functional Languages. Hwansoo Han

Programming Languages

Func%onal Programming in Scheme and Lisp

Introduction to lambda calculus Part 3

Functional Programming. Pure Functional Languages

Artificial Intelligence Programming

Pace University. Fundamental Concepts of CS121 1

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

CMSC 330: Organization of Programming Languages

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Control in Sequential Languages

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Functional Programming Languages (FPL)

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Types and Type Inference

John McCarthy IBM 704

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

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

Recap: Functions as first-class values

Functional Programming

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson

CPS 506 Comparative Programming Languages. Programming Language Paradigm

G Programming Languages - Fall 2012

INF4820. Common Lisp: Closures and Macros

CS 231 Data Structures and Algorithms, Fall 2016

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

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

NOTE: Answer ANY FOUR of the following 6 sections:

A little bit of Lisp

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Transcription:

Symbolic Computation and Common Lisp Dr. Neil T. Dantam CSCI-56, Colorado School of Mines Fall 28 Dantam (Mines CSCI-56) Lisp Fall 28 / 92

Why? Symbolic Computing: Much of this course deals with processing (rewriting) symbolic expressions Lisp has good support for symbol processing Functional Programming: Many algorithms in this course are more easily expressed in functional/recursive style Lisp has good support for functional programming. Understanding the abstractions in Lisp will make you a better programmer. Learn Lisp. Even if you don t actually use it, it will make you a better programmer. Alan Kay inventor of Smalltalk and OOP Dantam (Mines CSCI-56) Lisp Fall 28 2 / 92

Outline S-Expressions Rewrite Systems Implementing Expressions List Manipulation Lisp Common Lisp by Example Implementation Details Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI-56) Lisp Fall 28 3 / 92

S-Expressions Outline S-Expressions Rewrite Systems Implementing Expressions List Manipulation Lisp Common Lisp by Example Implementation Details Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI-56) Lisp Fall 28 4 / 92

S-Expressions Rewrite Systems Rewrite Systems Expressions Reductions Arithmetic: a x + a x 2 + a 3 x 3 3x + = Propositional Logic: etc. (p p 2 ) p 3 (p p 2 ) = p 3 Distributive Properties: x (y + z) xy + xz α (β γ) (α β) (α γ) De Morgan s Laws: (α β) ( α β) (α β) ( α β) etc. Progressively apply reductions until reaching desired expression. Dantam (Mines CSCI-56) Lisp Fall 28 5 / 92

S-Expressions Rewrite Systems Example: Algebra Given: 3x + = Find: x Solution: Initial 3x + = 3x + = Simplify 3x = 9 /3 3x/3 = 9/3 Simplify x = 3 Dantam (Mines CSCI-56) Lisp Fall 28 6 / 92

S-Expressions Implementing Expressions S-Expression 3x + = Abstract Syntax Tree = S-expression (= (+ (* 3 x) ) ) + * 3 x (= (+ (* 3 x) ) ) Dantam (Mines CSCI-56) Lisp Fall 28 7 / 92

S-Expressions Implementing Expressions Cell Diagram 3x + = (= (+ (* 3 x) ) ) NIL = NIL + 3 NIL * x Dantam (Mines CSCI-56) Lisp Fall 28 8 / 92

S-Expressions Implementing Expressions List vs. Tree List Tree s t r u c t cons { void f i r s t ; s t r u c t cons r e s t ; } ; s t r u c t t r e e n o d e { void f i r s t ; s t r u c t cons c h i l d r e n ; } ; s t r u c t cons { void f i r s t ; s t r u c t cons r e s t ; } ; Dantam (Mines CSCI-56) Lisp Fall 28 9 / 92

S-Expressions Implementing Expressions Data Structure, Redux 3x + = = = NIL * + + NIL 3 x 3 * x NIL Dantam (Mines CSCI-56) Lisp Fall 28 / 92

S-Expressions Implementing Expressions Exercise : S-Expression 2(x+) = 4 2(x + ) = 4 Dantam (Mines CSCI-56) Lisp Fall 28 / 92

S-Expressions Implementing Expressions Exercise 2: S-Expression a (b x) (c x) a (b x) + (c x) Dantam (Mines CSCI-56) Lisp Fall 28 2 / 92

S-Expressions Implementing Expressions Exercise 2: S-Expression a (b x) + (c x) continued Dantam (Mines CSCI-56) Lisp Fall 28 3 / 92

S-Expressions List Manipulation CONStruct Creating Lists (cons α β) (α. β) α β (cons NIL) (. nil) () NIL (cons 2 (cons NIL)) (2. (. nil)) (2 ) 2 NIL Dantam (Mines CSCI-56) Lisp Fall 28 4 / 92

S-Expressions List Manipulation Dotted List Notation (x. y) x y (x y) NIL x y (x. (y z)) NIL x y z (x (y z)) NIL x NIL y z Dantam (Mines CSCI-56) Lisp Fall 28 5 / 92

S-Expressions List Manipulation List Access CAR / CDR CAR CDR (car (α. β)) α CONS cell: (cdr (α. β)) β Dantam (Mines CSCI-56) Lisp Fall 28 6 / 92

S-Expressions List Manipulation Example: CAR / CDR (car (x. y)) x (cdr (x. y)) y (car (x y z)) x (cdr (x y z)) NIL y z Dantam (Mines CSCI-56) Lisp Fall 28 7 / 92

S-Expressions List Manipulation List Function (list) NIL (list α) (cons α NIL) α NIL (list α β) (cons α (list β)) α β NIL (list α β γ) (cons α (list β γ)) α β γ NIL Dantam (Mines CSCI-56) Lisp Fall 28 8 / 92

S-Expressions List Manipulation S-Expression Quoting Expressions vs. Execution Execute: (fun a b c) return value of fun called on arguments a, b, and c Expression: (fun a b c) The s-expression (fun a b c) Examples: (list 2 3) ( 2 3) (list (+ 2) 3) (list 3 3) (3 3) (list (+ 2) 3) ((+ 2) 3) (list + ( 2 3)) (list + 6) (+ 6) Dantam (Mines CSCI-56) Lisp Fall 28 9 / 92

S-Expressions List Manipulation Exercise: List Construction (cons x y) (cons x (y z)) (cons x (list y z)) (list (+ 2 3)) (list (+ 2 3)) (list (+ 2 2) ( 2 2)) (list + ( a 2) ( 3 4)) Dantam (Mines CSCI-56) Lisp Fall 28 2 / 92

Lisp Outline S-Expressions Rewrite Systems Implementing Expressions List Manipulation Lisp Common Lisp by Example Implementation Details Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI-56) Lisp Fall 28 2 / 92

Lisp What is Lisp? Definition: Lisp A family of programming languages that are based on s-expressions. Dantam (Mines CSCI-56) Lisp Fall 28 22 / 92

Lisp Major Lisp Dialects Scheme Common Lisp Clojure IEEE Standard Simple and clean ANSI Standard Featureful Good compilers Efficient C interop. JVM-based Good Java interop. CLR and Javascript also Concurrency features Dantam (Mines CSCI-56) Lisp Fall 28 23 / 92

Lisp Common Lisp by Example Hello World C #i n c l u de <s t d i o. h> Common Lisp ( format t h e l l o, world % ) main ( ) { p r i n t f ( h e l l o, world \n ) ; } Dantam (Mines CSCI-56) Lisp Fall 28 24 / 92

Lisp Common Lisp by Example Booleans and Equality Math Lisp Notes False nil equivalent to the empty list () True t or any non-nil value a (not a) a = b (= a b) numerical comparison a = b (eq a b) same object a = b (eql a b) same object, same number and type, or same character a = b (equal a b) eql objects, or lists/arrays with equal elements a = b (equalp a b) = numbers, or same character (case-insensitive), or recursively-equalp cons cells, arrays, structures, hash tables a b (not (= a b)) similarly for other equality functions Dantam (Mines CSCI-56) Lisp Fall 28 25 / 92

Lisp Common Lisp by Example Example: Lisp Equality Operators (= ) t (eq ) t (= (eq (eql integer {}}{ integer {}}{ (equal integer {}}{ (equalp float {}}{. ) t float {}}{. ) nil integer {}}{ float {}}{. ) nil integer {}}{ float {}}{. ) nil float {}}{. ) t (= "a" "a") error (eq "a" "a") nil (eql "a" "a") nil (equal "a" "a") t (equal "a" "A") nil (equalp "a" "A") t (not t) nil (not nil) t (not "a") nil Dantam (Mines CSCI-56) Lisp Fall 28 26 / 92

Lisp Common Lisp by Example Exercise: Lisp Equality Operators (not ) (not ) (eq (list a b ) (list a b )) (equal (list a b ) (list a b )) (eq t (not nil)) (eq t ) (eq nil (not )) (eq (list a b ) (list a B )) (equal (list a b ) (list a B )) (eq nil (not "a")) (equalp (list a b ) (list a B )) Dantam (Mines CSCI-56) Lisp Fall 28 27 / 92

Lisp Common Lisp by Example Inequality Math Lisp a < b (< a b) a b (<= a b) a > b (> a b) a b (>= a b) Dantam (Mines CSCI-56) Lisp Fall 28 28 / 92

Lisp Common Lisp by Example Boolean Operators Math Lisp a (not a) a b (and a b) a b (or a b) Dantam (Mines CSCI-56) Lisp Fall 28 29 / 92

Lisp Common Lisp by Example Function Definition Procedure increment(n) return n + ; function name {}}{ (defun increment (+ n ) ) }{{} result function arguments {}}{ (n) Dantam (Mines CSCI-56) Lisp Fall 28 3 / 92

Lisp Common Lisp by Example Exercise: Function Definition nand(a, b) (a b) a b a b nand(a,b) Pseudocode Common Lisp Procedure nand(θ) return (a b); Dantam (Mines CSCI-56) Lisp Fall 28 3 / 92

Lisp Common Lisp by Example Conditional IF Procedure even?(n) if = mod(n, 2) then 2 return true; 3 else 4 return false; (defun even? (n) test {}}{ (if (= (mod n 2)) then clause {}}{ t )) nil }{{} else clause Dantam (Mines CSCI-56) Lisp Fall 28 32 / 92

Lisp Common Lisp by Example Conditional COND Procedure sign(n) if n > then 2 return ; 3 else if n < then 4 return ; 5 else 6 return ; (defun sign (n) test {}}{ (cond ((> n ) test {}}{ ((< n ) test {}}{ ( t result {}}{ ) result {}}{ ) result {}}{ ))) Dantam (Mines CSCI-56) Lisp Fall 28 33 / 92

Lisp Common Lisp by Example Exercise: Conditionals a b c AND NAND If Cond Dantam (Mines CSCI-56) Lisp Fall 28 34 / 92

Lisp Common Lisp by Example Example: Factorial n! = { if n = n (n )! if n Pseudocode Procedure factorial(x) if = x then 2 return ; 3 else 4 return x factorial(x ); Common Lisp ( defun f a c t o r i a l ( n ) ( i f (= n ) ( n ( f a c t o r i a l ( n ) ) ) ) ) Dantam (Mines CSCI-56) Lisp Fall 28 35 / 92

Lisp Common Lisp by Example Exercise: Fibonacci Sequence (,, 2, 3, 5, 8, 3, 2, 34, 55,...) if n = fib(n) = if n = fib(n ) + fib(n 2) if n 2 Dantam (Mines CSCI-56) Lisp Fall 28 36 / 92

Lisp Common Lisp by Example Exercise: Fibonacci Sequence continued Pseudocode Common Lisp Dantam (Mines CSCI-56) Lisp Fall 28 37 / 92

Lisp Implementation Details Data Types Examples Definition: Data type A classification of data/objects based on how the data/object is intended to or able to be use. The set of values a variable may take. int float List String Structures: int string float 4 Function: int int bool Dantam (Mines CSCI-56) Lisp Fall 28 38 / 92

Lisp Implementation Details Data Type Systems Type Checking/Binding Static: Check types at compile time (statically) Dynamic: Check types at run time (dynamically). Type Enforcement Strong: Object types are strictly enforced Weak: Objects can be treated as different types (casting, type punning ) Features of a language make it more or less static/dynamic and strong/weak Compare with polymorphism (single interface to multiple types) Dantam (Mines CSCI-56) Lisp Fall 28 39 / 92

Lisp Implementation Details Comparison of Language Type Systems C++ Static Java, ML/Haskell C Weak Common Lisp Strong Assembly, Shell Perl Dynamic Python Dantam (Mines CSCI-56) Lisp Fall 28 4 / 92

Lisp Implementation Details Machine Words Representing Data x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x 3 word 3 unsigned 42 x2a 3 signed -42 xffffffd6 3 float 42. =.325 2 5 x4228 Dantam (Mines CSCI-56) Lisp Fall 28 4 / 92

Lisp Implementation Details Words and Types word 3 xc49fd? 68953648 (signed) xc49fd? 32263648 (unsigned) xc49fd? 3.459 (float) xc49fd? valid pointer Dantam (Mines CSCI-56) Lisp Fall 28 42 / 92

Lisp Implementation Details Type Tags Data Tag 3 SBCL Tags (32-bit) Type Tag Even Fixnum b Odd Fixnum b Instance Pointer b List Pointer b Function Pointer b data {}}{ x892fa tag {}}{ b even fixnum (x892fa >> 2) 865342 Dantam (Mines CSCI-56) Lisp Fall 28 43 / 92

Lisp Implementation Details Example: Tagged Storage 64-bit SBCL: Fixnum: (eq ) t 64 bit 64 bit 64 bit 64 bit 64 bit 64 bit Single Float: (eq.s.s) t eq NIL Double Float: (eq.d.d) nil eq.. NIL eq.d.d NIL Dantam (Mines CSCI-56) Lisp Fall 28 44 / 92

Lisp Implementation Details Example: SBCL Arrays ( l e t ( ( a ( make array 5 : element type d o u b l e f l o a t ) ) ) ; ;.... ) data 64 bit 64 bit 64 bit 64 bit 64 bit.d.d.d.d.d A type descriptor (SIMPLE-ARRAY DOUBLE-FLOAT (5)) Dantam (Mines CSCI-56) Lisp Fall 28 45 / 92

Lisp Implementation Details Manual Memory Management malloc(n) free(ptr). Find a free block of at least n bytes 2. If no such block, get more memory from the OS 3. Return pointer to the block. Add block back to the free list(s) Dantam (Mines CSCI-56) Lisp Fall 28 46 / 92

Lisp Implementation Details Garbage Collection CPU registers r r... Roots Global Variables h h Heap h 4 h 6 r k r k h 2 h 3 h 5 h 7 Local Variables Stack Dantam (Mines CSCI-56) Lisp Fall 28 47 / 92

Functional Programming Outline S-Expressions Rewrite Systems Implementing Expressions List Manipulation Lisp Common Lisp by Example Implementation Details Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI-56) Lisp Fall 28 48 / 92

Functional Programming Functional Programming Features Functions are first class object Prefer immutable state Garbage collection Dantam (Mines CSCI-56) Lisp Fall 28 49 / 92

Functional Programming Closures Closure Definition (Closure) A function and an associated set of variable definitions. From closed expression. / D e f i n i t i o n / s t r u c t c o n t e x t { i n t v a l ; } ; C Function Pointer i n t adder ( s t r u c t c o n t e x t cx, i n t x ) { r e t u r n cx >a + x ; } / Usage / s t r u c t c o n t e x t c ; c. v a l = ; i n t y = adder ( c, 2 ) ; Java Class // D e f i n i t i o n c l a s s Adder { p u b l i c i n t a ; p u b l i c Adder ( i n t a ) { a = a ; } p u b l i c i n t c a l l ( i n t x ) { r e t u r n x+a ; } } // Usage Adder A = new Adder ( ) ; i n t y = A. c a l l ( 2 ) ; Dantam (Mines CSCI-56) Lisp Fall 28 5 / 92

Functional Programming Closures Closure in Lisp Local Function ( l e t ( ( a ) ) ( l a b e l s ( ( adder ( x ) (+ x a ) ) ) ( adder 2 ) ) ) Lambda Expression ( l e t ( ( a ) ) ( f u n c a l l ( lambda ( x ) (+ x a ) ) 2 ) ) Dantam (Mines CSCI-56) Lisp Fall 28 5 / 92

Functional Programming Recursion Example: Recursion Iterative Recursive Function accumulate(s) a ; 2 i ; 3 while i < S do 4 a a + S i ; 5 return a; Function accumulate(s) if S then // Recursive Case 2 return car(s) + accumulate (cdr (S)); 3 else // Base Case 4 return ; Dantam (Mines CSCI-56) Lisp Fall 28 52 / 92

Functional Programming Recursion Example: Recursive Accumulate in Lisp Recursive Implementation of Accumulate ( defun accumulate ( l i s t ) ( i f l i s t ; ; r e c u r s i v e c a s e (+ ( c a r l i s t ) ( accumulate ( cdr l i s t ) ) ) ; ; base c a s e ) ) Dantam (Mines CSCI-56) Lisp Fall 28 53 / 92

Functional Programming Recursion Example: Recursive Accumulate Execution Trace (accumulate ( 2 3)) Recursive Implementation of Accumulate ( defun accumulate ( l i s t ) ( i f l i s t ; ; r e c u r s i v e c a s e (+ ( c a r l i s t ) ( accumulate ( cdr l i s t ) ) ) ; ; base c a s e ) ) (+ (accumulate (2 3))) (+ (+ 2 (accumulate (3)))) (+ (+ 2 (+ 3 (accumulate nil)))) (+ (+ 2 (+ 3 ))) Dantam (Mines CSCI-56) Lisp Fall 28 54 / 92

Functional Programming Recursion Example: Alternate Recursive Accumulate Accumulate ( defun accumulate ( l i s t ) ( i f l i s t ; ; r e c u r s i v e c a s e (+ ( c a r l i s t ) ( accumulate ( cdr l i s t ) ) ) ; ; base c a s e ) ) Alternate Accumulate ( defun accumulate ( l i s t ) ( l a b e l s ( ( r e c ( l i s t accum ) ( i f l i s t ; ; r e c u r s i v e c a s e ( r e c ( cdr l i s t ) (+ ( c a r l i s t ) accum ) ) ; ; base c a s e accum ) ) ) ( r e c l i s t ) ) ) Dantam (Mines CSCI-56) Lisp Fall 28 55 / 92

Functional Programming Recursion Example: Alternate Accumulate Execution Trace Recursive Implementation of Accumulate (accumulate ( 2 3)) ( defun accumulate ( l i s t ) ( l a b e l s ( ( r e c ( l i s t accum ) ( i f l i s t ; ; r e c u r s i v e c a s e ( r e c ( cdr l i s t ) (+ ( c a r l i s t ) accum ) ) ; ; base c a s e accum ) ) ) ( r e c l i s t ) ) ) (rec ( 2 3) ) (rec (2 3) ) (rec (3) 3) (rec () 6) Dantam (Mines CSCI-56) Lisp Fall 28 56 / 92

Functional Programming Recursion Exercise: Recursive Reverse (a a... a n a n ) reverse (a n a n... a a ) Procedure reverse(l) Dantam (Mines CSCI-56) Lisp Fall 28 57 / 92

Functional Programming Functional Operators Map Definition (map) Apply a function to every member of a sequence. map : (D R) }{{}}{{} D n }{{} R n function sequence result Function Application (f (s ), f (s 2 ),..., f (s n )) Dantam (Mines CSCI-56) Lisp Fall 28 58 / 92

Functional Programming Functional Operators Map Pseudocode Procedural Function map(f,s) foreach s i S do 2 r i f (s i ); 3 return r; Recursive Function map(f,s) if S then // Recursive Case 2 a f (car(s)); 3 b map (f, cdr (S)); 4 return cons(a, b) 5 else // Base Case 6 return (); Dantam (Mines CSCI-56) Lisp Fall 28 59 / 92

Functional Programming Functional Operators Map in Lisp Map in Lisp (map l i s t ; r e s u l t type ( lambda ( x ) (+ x ) ) ; f u n c t i o n ( l i s t 2 3 ) ) ; sequence ; ; RESULT : (2 3 4) Dantam (Mines CSCI-56) Lisp Fall 28 6 / 92

Functional Programming Functional Operators Example: A Map Implementation Example Implementation of Map ( defun mymap ( f u n c t i o n l i s t ) ( l a b e l s ( ( h e l p e r ( l i s t ) ( i f l i s t ; ; R e c u r s i v e Case : ( cons ( f u n c a l l f u n c t i o n ( c a r l i s t ) ) ( h e l p e r ( cdr l i s t ) ) ) ; ; Base Case : n i l ) ) ) ( h e l p e r l i s t ) ) ) Dantam (Mines CSCI-56) Lisp Fall 28 6 / 92

Functional Programming Functional Operators Fold-left Definition (fold-left) Apply a binary function to every member of a sequence and the result of the previous call, starting from the left-most (initial) element. fold-left : (Y X Y) Y }{{} function }{{} init. }{{} X n sequence Y }{{} result Function Application... f f y x x f... x n Dantam (Mines CSCI-56) Lisp Fall 28 62 / 92

Functional Programming Functional Operators Fold-left Pseudocode Procedural Function fold-left(f,y,x) i ; 2 while i < X do 3 y f (y, X i ) ; 4 return y; Recursive Function fold-left(f,y,x) if X then // Recursive Case 2 y f (y, car(x )); 3 return fold-left (f, y, cdr (X )); 4 else // Base Case 5 return y; Dantam (Mines CSCI-56) Lisp Fall 28 63 / 92

Functional Programming Functional Operators Fold-left in Lisp Fold-Left in Lisp ( reduce # + ; f u n c t i o n ( 2 3) ; sequence : i n i t i a l v a l u e ) ; ; ; R e s u l t 6 Dantam (Mines CSCI-56) Lisp Fall 28 64 / 92

Functional Programming Functional Operators Exercise: Fold-Left Reverse (a a... a n a n ) reverse (a n a n... a a ) Procedure reverse(l) Dantam (Mines CSCI-56) Lisp Fall 28 65 / 92

Functional Programming Functional Operators Fold-right Definition (fold-right) Apply a binary function to every member of a sequence and the result of the previous call, starting from the right-most (final) element. fold-right : (X Y Y) Y }{{} function }{{} init. }{{} X n sequence Y }{{} result Function Application f x x f f...... f x n y Dantam (Mines CSCI-56) Lisp Fall 28 66 / 92

Functional Programming Functional Operators Fold-right Pseudocode Procedural Function fold-right(f,y,x) i X ; 2 while i do 3 y f (X i, y) ; 4 return y; Recursive Function fold-right(f,y,x) if X then // Recursive Case 2 y fold-right (f, y, cdr (X )); 3 return f (car(x ), y ); 4 else // Base Case 5 return y; Dantam (Mines CSCI-56) Lisp Fall 28 67 / 92

Functional Programming Functional Operators Fold-right in Lisp Fold-Right in Lisp ( reduce # ; f u n c t i o n ( 2 3) ; sequence : i n i t i a l v a l u e : from end t ) ; ; ; R e s u l t Dantam (Mines CSCI-56) Lisp Fall 28 68 / 92

Functional Programming Functional Operators MapReduce (parallel) map (serial) reduce/fold Provides scalability, fault-tolerance Implementations Google MapReduce Apache Hadoop Function MapReduce(f,g,X) Y parallel-map(f, X ); 2 return reduce(g, Y ); Dantam (Mines CSCI-56) Lisp Fall 28 69 / 92

Programming Environment Outline S-Expressions Rewrite Systems Implementing Expressions List Manipulation Lisp Common Lisp by Example Implementation Details Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI-56) Lisp Fall 28 7 / 92

Programming Environment Lisp Programming Environment C Programming Lisp Programming Lisp on unix source code compile binary source code edit,compile,debug editor lisp edit output debug output Dantam (Mines CSCI-56) Lisp Fall 28 7 / 92

Programming Environment Demo SLIME, pstree Read-Eval-Print-Loop (REPL) DEFUN DISASSEMBLE Re-DEFUN Dantam (Mines CSCI-56) Lisp Fall 28 72 / 92

Programming Environment SLIME Basics C: control M: Meta / Alt Frequently used: C-c C-k Compile and load file C-x C-e Evaluate expression before the point C-M-x Evaluate defun surround the point See SLIME drop-down in menu bar for more https://common-lisp.net/project/slime/doc/html/ Dantam (Mines CSCI-56) Lisp Fall 28 73 / 92

Programming Environment Summary S-Expressions Rewrite Systems Implementing Expressions List Manipulation Lisp Common Lisp by Example Implementation Details Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI-56) Lisp Fall 28 74 / 92

Appendix Outline S-Expressions Rewrite Systems Implementing Expressions List Manipulation Lisp Common Lisp by Example Implementation Details Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI-56) Lisp Fall 28 75 / 92

Appendix LET Creates a new scope and variable bindings Dantam (Mines CSCI-56) Lisp Fall 28 76 / 92

Appendix Example: LET C Block Scope C Lisp Output { } i n t a = ; i n t b = ; p r i n t f ( (%d %d )\ n, a, b ) ; ( l e t ( ( a ) ( b 2 ) ) ( p r i n t ( l i s t a b ) ) ) ( 2) Dantam (Mines CSCI-56) Lisp Fall 28 77 / 92

Appendix Example: LET Scope Nesting C Lisp Output { } i n t a = ; p r i n t f ( %d\n, a ) ; { i n t a = 2 ; p r i n t f ( %d\n, a ) ; } p r i n t f ( %d\n, a ) ; ( l e t ( ( a ) ) ( p r i n t a ) ( l e t ( ( a 2 ) ) ( p r i n t a ) ) ( p r i n t a ) ) 2 Dantam (Mines CSCI-56) Lisp Fall 28 78 / 92

Appendix Example: LET Parallel assignments Lisp ( l e t ( ( a ) ( b 2 ) ) ( l e t ( ( a 3) ( b a ) ) ( p r i n t ( l i s t a b ) ) ) ) Output (3 ) Dantam (Mines CSCI-56) Lisp Fall 28 79 / 92

Appendix Example: LET* Consecutive assignments Lisp ( l e t ( ( a ) ( b 2 ) ) ( l e t ( ( a 3) ( b a ) ) ( p r i n t ( l i s t a b ) ) ) ) Output (3 3) Dantam (Mines CSCI-56) Lisp Fall 28 8 / 92

Appendix DOTIMES Iterate a for n steps Dantam (Mines CSCI-56) Lisp Fall 28 8 / 92

Appendix Example: DOTIMES f o r ( i n t i = ; i < 5 ; i ++ ) { p r i n t f ( %d, i ) ; } C Lisp ( dotimes ( i 5) ( p r i n t i ) ) Output 2 3 4 Dantam (Mines CSCI-56) Lisp Fall 28 82 / 92

Appendix DOLIST Iterate over a list Dantam (Mines CSCI-56) Lisp Fall 28 83 / 92

Appendix Example: DOLIST Lisp ( d o l i s t ( x ( a b c ) ) ( p r i n t x ) ) Output A B C Dantam (Mines CSCI-56) Lisp Fall 28 84 / 92

Appendix Example: LOOP counting Lisp ( loop f o r i below 5 do ( p r i n t i ) ) Output 2 3 4 Dantam (Mines CSCI-56) Lisp Fall 28 85 / 92

Appendix Example: LOOP list iteration Lisp ( loop f o r x i n ( a b c ) do ( p r i n t x ) ) Output A B C Dantam (Mines CSCI-56) Lisp Fall 28 86 / 92

Appendix Example: LOOP collecting Lisp ( l e t ( ( x ( loop f o r i below 5 when ( evenp i ) c o l l e c t i ) ) ) ( p r i n t x ) ) Output ( 2 4) Dantam (Mines CSCI-56) Lisp Fall 28 87 / 92

Appendix Example: REDUCE collecting Lisp ( l e t ( ( x ( reduce ( lambda ( a x ) ( i f ( evenp x ) ( cons x a ) a ) ) ( 4 3 2 ) : i n i t i a l v a l u e n i l ) ) ) Output ( 2 4) ( p r i n t x ) ) Dantam (Mines CSCI-56) Lisp Fall 28 88 / 92

Appendix Case Control structure Selects clause that matches the test argument Dantam (Mines CSCI-56) Lisp Fall 28 89 / 92

Appendix Example: CASE C switch ( B ) { case A : puts ( Got A ) ; break ; case B : puts ( Got B ) ; break ; case C : puts ( Got C ) ; break ; } Lisp ( case b ( a ( p r i n t Got A ) ) ( b ( p r i n t Got B ) ) ( c ( p r i n t Got C ) ) Output Got B Dantam (Mines CSCI-56) Lisp Fall 28 9 / 92

Appendix Example: S-Expression to XML Lisp ( l a b e l s ( ( v i s i t ( e i ) ( i f ( l i s t p e ) ( progn ; ; opening tag ( format t &< A> ( c a r e ) ) ; ; Re cur se on arguments ( d o l i s t ( e ( cdr e ) ) ( v i s i t e (+ i 2 ) ) ) ; ; C l o s i n g tag ( format t &</ A> ( c a r e ) ) ) ; ; Else, p r i n t the element ( format t & A e ) ) ) ) ( v i s i t ( and x ( or y z ) ) ) ) Output <AND> X <OR> Y Z </OR> </AND> Dantam (Mines CSCI-56) Lisp Fall 28 9 / 92

Appendix Example: S-Expression to XML w/ indentation Lisp ( l a b e l s ( ( v i s i t ( e i ) ( l e t ( ( i n d e n t ( make string i : i n i t i a l e l e m e n t #\Space ) ) ) ( i f ( l i s t p e ) ( progn ; ; opening tag ( format t & A< A> i n d e n t ( c a r e ) ) ; ; Recurse on arguments ( d o l i s t ( e ( cdr e ) ) ( v i s i t e (+ i 2 ) ) ) ; ; C l o s i n g tag ( format t & A</ A> i n d e n t ( c a r e ) ) ) ; ; Else, p r i n t the element ( format t & A A i n d e n t e ) ) ) ) ) ( v i s i t ( and x ( or y z ) ) ) ) Output <AND> X <OR> Y Z </OR> </AND> Dantam (Mines CSCI-56) Lisp Fall 28 92 / 92