Symbolic Computation and Common Lisp

Size: px
Start display at page:

Download "Symbolic Computation and Common Lisp"

Transcription

1 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

2 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

3 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

4 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

5 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

6 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

7 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

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

9 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

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

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

12 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

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

14 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

15 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

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

17 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

18 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

19 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

20 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

21 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

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

23 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 / 92

24 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 / 92

25 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 / 92

26 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 / 92

27 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 / 92

28 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 / 92

29 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 / 92

30 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

31 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

32 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 / 92

33 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 / 92

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

35 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 / 92

36 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 / 92

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

38 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 / 92

39 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 / 92

40 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

41 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. = x4228 Dantam (Mines CSCI-56) Lisp Fall 28 4 / 92

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

43 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) Dantam (Mines CSCI-56) Lisp Fall / 92

44 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 / 92

45 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 / 92

46 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 / 92

47 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 / 92

48 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 / 92

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

50 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

51 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

52 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 / 92

53 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 / 92

54 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 / 92

55 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 / 92

56 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 / 92

57 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 / 92

58 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 / 92

59 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 / 92

60 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

61 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

62 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 / 92

63 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 / 92

64 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 / 92

65 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 / 92

66 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 / 92

67 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 / 92

68 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 / 92

69 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 / 92

70 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

71 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

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

73 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 Dantam (Mines CSCI-56) Lisp Fall / 92

74 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 / 92

75 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 / 92

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

77 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 / 92

78 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 / 92

79 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 / 92

80 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

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

82 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 Dantam (Mines CSCI-56) Lisp Fall / 92

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

84 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 / 92

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

86 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 / 92

87 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 / 92

88 Appendix Example: REDUCE collecting Lisp ( l e t ( ( x ( reduce ( lambda ( a x ) ( i f ( evenp x ) ( cons x a ) a ) ) ( ) : 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 / 92

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

90 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

91 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

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 / 92

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

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86 Symbolic Reasoning Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Spring 2019 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 1 / 86 Introduction Definition: Symbolic Reasoning Inference

More information

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

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals INF4820: Algorithms for Artificial Intelligence and Natural Language Processing Common Lisp Fundamentals Stephan Oepen & Murhaf Fares Language Technology Group (LTG) August 30, 2017 Last Week: What is

More information

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

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89 Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic expressions. Symbolic manipulation: you do it all the

More information

Common LISP Tutorial 1 (Basic)

Common LISP Tutorial 1 (Basic) Common LISP Tutorial 1 (Basic) CLISP Download https://sourceforge.net/projects/clisp/ IPPL Course Materials (UST sir only) Download https://silp.iiita.ac.in/wordpress/?page_id=494 Introduction Lisp (1958)

More information

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

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp INF4820: Algorithms for Artificial Intelligence and Natural Language Processing More Common Lisp Stephan Oepen & Murhaf Fares Language Technology Group (LTG) September 6, 2017 Agenda 2 Previously Common

More information

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

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides CS 480 Lisp J. Kosecka George Mason University Lisp Slides Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic

More information

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

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in

More information

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages. Lecture LISP Programming Language An Introduction Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

More information

Project 2: Scheme Interpreter

Project 2: Scheme Interpreter Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

Documentation for LISP in BASIC

Documentation for LISP in BASIC Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have

More information

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

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

Recursion & Iteration

Recursion & Iteration Recursion & Iteration York University Department of Computer Science and Engineering 1 Overview Recursion Examples Iteration Examples Iteration vs. Recursion Example [ref.: Chap 5,6 Wilensky] 2 Recursion

More information

Allegro CL Certification Program

Allegro CL Certification Program Allegro CL Certification Program Lisp Programming Series Level I Review David Margolies 1 Summary 1 A lisp session contains a large number of objects which is typically increased by user-created lisp objects

More information

Common Lisp. Blake McBride

Common Lisp. Blake McBride Contents Common Lisp Blake McBride (blake@mcbride.name) 1 Data Types 2 2 Numeric Hierarchy 3 3 Comments 3 4 List Operations 4 5 Evaluation and Quotes 5 6 String Operations 5 7 Predicates 6 8 Math Predicates

More information

Functional programming with Common Lisp

Functional programming with Common Lisp Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

More information

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

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp. Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also

More information

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

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7 Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

More information

MIDTERM EXAMINATION - CS130 - Spring 2005

MIDTERM EXAMINATION - CS130 - Spring 2005 MIDTERM EAMINATION - CS130 - Spring 2005 Your full name: Your UCSD ID number: This exam is closed book and closed notes Total number of points in this exam: 231 + 25 extra credit This exam counts for 25%

More information

Chapter 1. Fundamentals of Higher Order Programming

Chapter 1. Fundamentals of Higher Order Programming Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

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

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 FP Foundations, Scheme (2 In Text: Chapter 15 LISP: John McCarthy 1958 MIT List Processing => Symbolic Manipulation First functional programming language Every version after the

More information

Type Checking and Type Inference

Type Checking and Type Inference Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

More information

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

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

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

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia CPL 2016, week 10 Clojure functional core Oleg Batrashev Institute of Computer Science, Tartu, Estonia April 11, 2016 Overview Today Clojure language core Next weeks Immutable data structures Clojure simple

More information

Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N. Lecture 11 Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

More information

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

Debugging in LISP. trace causes a trace to be printed for a function when it is called trace causes a trace to be printed for a function when it is called ;;; a function that works like reverse (defun rev (list) (cons (first (last list)) (rev (butlast list)))) USER: (trace rev) ; note trace

More information

Lecture Notes on Lisp A Brief Introduction

Lecture Notes on Lisp A Brief Introduction Why Lisp? Lecture Notes on Lisp A Brief Introduction Because it s the most widely used AI programming language Because Prof Peng likes using it Because it s good for writing production software (Graham

More information

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

Pierce Ch. 3, 8, 11, 15. Type Systems Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using

More information

SOFTWARE ARCHITECTURE 6. LISP

SOFTWARE ARCHITECTURE 6. LISP 1 SOFTWARE ARCHITECTURE 6. LISP Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ 2 Compiler vs Interpreter Compiler Translate programs into machine languages Compilers are

More information

1.3. Conditional expressions To express case distinctions like

1.3. Conditional expressions To express case distinctions like Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

More information

The Typed Racket Guide

The Typed Racket Guide The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

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

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial About this Document This document was written to accompany an in-person Lisp tutorial. Therefore, the information on this document alone is not likely to be sufficient

More information

Robot Programming with Lisp

Robot Programming with Lisp 4. Functional Programming: Higher-order Functions, Map/Reduce, Lexical Scope Institute for Artificial University of Bremen 9 of November, 2017 Functional Programming Pure functional programming concepts

More information

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G. Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

More information

Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

More information

Lecture #24: Programming Languages and Programs

Lecture #24: Programming Languages and Programs Lecture #24: Programming Languages and Programs A programming language is a notation for describing computations or processes. These range from low-level notations, such as machine language or simple hardware

More information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

More information

Typed Racket: Racket with Static Types

Typed Racket: Racket with Static Types Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type

More information

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

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

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

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015 SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

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

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

Scheme as implemented by Racket

Scheme as implemented by Racket Scheme as implemented by Racket (Simple view:) Racket is a version of Scheme. (Full view:) Racket is a platform for implementing and using many languages, and Scheme is one of those that come out of the

More information

CSCI337 Organisation of Programming Languages LISP

CSCI337 Organisation of Programming Languages LISP Organisation of Programming Languages LISP Getting Started Starting Common Lisp $ clisp i i i i i i i ooooo o ooooooo ooooo ooooo I I I I I I I 8 8 8 8 8 o 8 8 I \ `+' / I 8 8 8 8 8 8 \ `-+-' / 8 8 8 ooooo

More information

FP Foundations, Scheme

FP Foundations, Scheme FP Foundations, Scheme In Text: Chapter 15 1 Functional Programming -- Prelude We have been discussing imperative languages C/C++, Java, Fortran, Pascal etc. are imperative languages Imperative languages

More information

CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

More information

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

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

More information

CPSC 3740 Programming Languages University of Lethbridge. Control Structures

CPSC 3740 Programming Languages University of Lethbridge. Control Structures Control Structures A control structure is a control statement and the collection of statements whose execution it controls. Common controls: selection iteration branching Control Structures 1 15 Howard

More information

User-defined Functions. Conditional Expressions in Scheme

User-defined Functions. Conditional Expressions in Scheme User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

More information

A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

More information

Lecture #2 Kenneth W. Flynn RPI CS

Lecture #2 Kenneth W. Flynn RPI CS Outline Programming in Lisp Lecture #2 Kenneth W. Flynn RPI CS Items from last time Recursion, briefly How to run Lisp I/O, Variables and other miscellany Lists Arrays Other data structures Jin Li lij3@rpi.edu

More information

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml COSE212: Programming Languages Lecture 3 Functional Programming in OCaml Hakjoo Oh 2017 Fall Hakjoo Oh COSE212 2017 Fall, Lecture 3 September 18, 2017 1 / 44 Why learn ML? Learning ML is a good way of

More information

A LISP Interpreter in ML

A LISP Interpreter in ML UNIVERSITY OF OSLO Department of Informatics A LISP Interpreter in ML Mandatory Assignment 1 INF3110 September 21, 2009 Contents 1 1 Introduction The purpose of this assignment is to write an interpreter,

More information

A Brief Introduction to Common Lisp

A Brief Introduction to Common Lisp A Brief Introduction to Common Lisp David Gu Schloer Consulting Group david_guru@gty.org.in A Brief History Originally specified in 1958, Lisp is the second-oldest highlevel programming language in widespread

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

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

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am. The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper

More information

Imperative languages

Imperative languages Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered

More information

Robot Programming with Lisp

Robot Programming with Lisp 2. Imperative Programming Institute for Artificial University of Bremen Lisp the Language LISP LISt Processing language 2 Lisp the Language LISP LISt Processing language (LISP Lots of Irritating Superfluous

More information

Func%onal Programming in Scheme and Lisp

Func%onal Programming in Scheme and Lisp Func%onal Programming in Scheme and Lisp http://www.lisperati.com/landoflisp/ Overview In a func(onal programming language, func(ons are first class objects You can create them, put them in data structures,

More information

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

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited CMSC 330: Organization of Programming Languages Type Systems, Names & Binding Topics Covered Thus Far Programming languages Syntax specification Regular expressions Context free grammars Implementation

More information

INTRODUCTION TO SCHEME

INTRODUCTION TO SCHEME INTRODUCTION TO SCHEME PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2019 Dalhousie University 1/110 SCHEME: A FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be passed as

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

Functional Languages. Hwansoo Han

Functional Languages. Hwansoo Han Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

More information

Programming Languages

Programming Languages Programming Languages Lambda Calculus and Scheme CSCI-GA.2110-003 Fall 2011 λ-calculus invented by Alonzo Church in 1932 as a model of computation basis for functional languages (e.g., Lisp, Scheme, ML,

More information

Func%onal Programming in Scheme and Lisp

Func%onal Programming in Scheme and Lisp Func%onal Programming in Scheme and Lisp http://www.lisperati.com/landoflisp/ Overview In a func(onal programming language, func(ons are first class objects You can create them, put them in data structures,

More information

Introduction to lambda calculus Part 3

Introduction to lambda calculus Part 3 Introduction to lambda calculus Part 3 Antti-Juhani Kaijanaho 2017-01-27... 1 Untyped lambda calculus... 2 Typed lambda calculi In an untyped lambda calculus extended with integers, it is required that

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

Artificial Intelligence Programming

Artificial Intelligence Programming Artificial Intelligence Programming Rob St. Amant Department of Computer Science North Carolina State University Lisp basics NC State University 2 / 99 Why Lisp? Some recent Lisp success stories include

More information

Pace University. Fundamental Concepts of CS121 1

Pace University. Fundamental Concepts of CS121 1 Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction

More information

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

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits. LISP Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits. From one perspective, sequences of bits can be interpreted as a code for ordinary decimal digits,

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Type Systems, Names and Binding CMSC 330 - Spring 2013 1 Topics Covered Thus Far! Programming languages Ruby OCaml! Syntax specification Regular expressions

More information

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

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

Control in Sequential Languages

Control in Sequential Languages CS 242 2012 Control in Sequential Languages Reading: Chapter 8, Sections 8.1 8.3 (only) Section 7.3 of The Haskell 98 Report, Exception Handling in the I/O Monad, http://www.haskell.org/onlinelibrary/io-13.html

More information

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

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8 Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns

More information

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

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures Dan Grossman Fall 2014 Hi! I m not Hal J I love this stuff and have taught

More information

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

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming Closures Mooly Sagiv Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming t ::= x x. t t t Call-by-value big-step Operational Semantics terms variable v ::= values abstraction x.

More information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

More information

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

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17 List of Programs xxv List of Figures xxix List of Tables xxxiii Preface to second version xxxv PART 1 Structured Programming 1 1 Getting started 3 1.1 Programming 3 1.2 Editing source code 5 Source code

More information

Types and Type Inference

Types and Type Inference CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of

More information

John McCarthy IBM 704

John McCarthy IBM 704 How this course works SI 334: Principles of Programming Languages Lecture 2: Lisp Instructor: an arowy Lots of new languages Not enough class time to cover all features (e.g., Java over the course of 34-36:

More information

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

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

More information

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

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012 SCHEME COMPUTER SCIENCE 6A October 29th, 202 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually

More information

Recap: Functions as first-class values

Recap: Functions as first-class values Recap: Functions as first-class values Arguments, return values, bindings What are the benefits? Parameterized, similar functions (e.g. Testers) Creating, (Returning) Functions Iterator, Accumul, Reuse

More information

Functional Programming

Functional Programming Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used

More information

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

Introduction to Functional Programming in Racket. CS 550 Programming Languages Jeremy Johnson Introduction to Functional Programming in Racket CS 550 Programming Languages Jeremy Johnson 1 Objective To introduce functional programming in racket Programs are functions and their semantics involve

More information

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages. Programming Language Paradigm CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

More information

INF4820. Common Lisp: Closures and Macros

INF4820. Common Lisp: Closures and Macros INF4820 Common Lisp: Closures and Macros Erik Velldal University of Oslo Oct. 19, 2010 Erik Velldal INF4820 1 / 22 Topics for Today More Common Lisp A quick reminder: Scope, binding and shadowing Closures

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

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

(Func&onal (Programming (in (Scheme)))) Jianguo Lu (Func&onal (Programming (in (Scheme)))) Jianguo Lu 1 Programming paradigms Func&onal No assignment statement No side effect Use recursion Logic OOP AOP 2 What is func&onal programming It is NOT what you

More information

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages Chapter 15 Functional Programming Languages Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: Lisp Introduction

More information

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages

More information

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

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming The ML Programming Language General purpose programming language designed by Robin Milner in 1970 Meta Language

More information

NOTE: Answer ANY FOUR of the following 6 sections:

NOTE: Answer ANY FOUR of the following 6 sections: A-PDF MERGER DEMO Philadelphia University Lecturer: Dr. Nadia Y. Yousif Coordinator: Dr. Nadia Y. Yousif Internal Examiner: Dr. Raad Fadhel Examination Paper... Programming Languages Paradigms (750321)

More information

A little bit of Lisp

A little bit of Lisp B.Y. Choueiry 1 Instructor s notes #3 A little bit of Lisp Introduction to Artificial Intelligence CSCE 476-876, Fall 2017 www.cse.unl.edu/~choueiry/f17-476-876 Read LWH: Chapters 1, 2, 3, and 4. Every

More information

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Notes on Higher Order Programming in Scheme. by Alexander Stepanov by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

More information