Lisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88

Size: px
Start display at page:

Download "Lisp Introduction. Dr. Neil T. Dantam. Spring CSCI-498/598 RPM, Colorado School of Mines. Dantam (Mines CSCI, RPM) Lisp Spring / 88"

Transcription

1 Lisp Introduction Dr. Neil T. Dantam CSCI-498/598 RPM, Colorado School of Mines Spring 28 Dantam (Mines CSCI, RPM) Lisp Spring 28 / 88

2 Outline Lisp Common Lisp by Example Implementation Details Typing Memory Management Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI, RPM) Lisp Spring 28 2 / 88

3 Lisp Outline Lisp Common Lisp by Example Implementation Details Typing Memory Management Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI, RPM) Lisp Spring 28 3 / 88

4 Lisp What is Lisp? Definition: Lisp A family of programming languages that are based on s-expressions. Dantam (Mines CSCI, RPM) Lisp Spring 28 4 / 88

5 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, RPM) Lisp Spring 28 5 / 88

6 Common Lisp by Example Outline Lisp Common Lisp by Example Implementation Details Typing Memory Management Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI, RPM) Lisp Spring 28 6 / 88

7 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, RPM) Lisp Spring 28 7 / 88

8 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, RPM) Lisp Spring 28 8 / 88

9 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, RPM) Lisp Spring 28 9 / 88

10 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, RPM) Lisp Spring 28 / 88

11 Common Lisp by Example Function Definition Procedure increment(n) return n + ; function name {}}{ (defun increment (+ n ) ) }{{} result function arguments {}}{ (n) Dantam (Mines CSCI, RPM) Lisp Spring 28 / 88

12 Common Lisp by Example Exercise: Function Definition sinc θ = sin θ θ Pseudocode Common Lisp Procedure sinc(θ) return sin(θ)/θ; Dantam (Mines CSCI, RPM) Lisp Spring 28 2 / 88

13 Common Lisp by Example Limit of sinc θ sin θ lim θ θ l Hôpital s rule lim θ d dθ sin θ cos θ d dθ θ lim θ Dantam (Mines CSCI, RPM) Lisp Spring 28 3 / 88

14 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, RPM) Lisp Spring 28 4 / 88

15 Common Lisp by Example Exercise: Conditionals IF sinc(θ) = { if θ = sin θ θ if θ Pseudocode Common Lisp Procedure sinc(θ) if = θ then 2 return ; 3 else 4 return sin(θ)/θ; Dantam (Mines CSCI, RPM) Lisp Spring 28 5 / 88

16 Common Lisp by Example Taylor Series Represent function f (x) as infinite sum of derivatives around point a: f (x) = f (a) + f (a) (! = n= (x a) + f (a) 2! ) f (n) (a) (x a) n n! (x a) + f (a) (x a) ! Polynomial approximation of functions Dantam (Mines CSCI, RPM) Lisp Spring 28 6 / 88

17 Common Lisp by Example Sinc Taylor Series 2 sin θ θ = θ2 6 + θ4 2 θ θ θ n = n = n = 2 n = 4 n = 6 n = 8 n = Dantam (Mines CSCI, RPM) Lisp Spring 28 7 / 88

18 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, RPM) Lisp Spring 28 8 / 88

19 Common Lisp by Example Exercise: Conditionals COND Pseudocode sin θ θ = θ2 6 + θ Common Lisp Procedure sinc(θ) if = θ then return ; 2 else if θ 2 <. then 3 return θ 2 /6 + θ 4 /2; 4 else return sin(θ)/θ ; Dantam (Mines CSCI, RPM) Lisp Spring 28 9 / 88

20 Common Lisp by Example Example: Factorial n! = { if n = n (n )! if n Pseudocode Common Lisp Procedure factorial(x) if = x then 2 return ; 3 else 4 return x factorial(x ); ( 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, RPM) Lisp Spring 28 2 / 88

21 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, RPM) Lisp Spring 28 2 / 88

22 Common Lisp by Example Exercise: Fibonacci Sequence continued Pseudocode Common Lisp Dantam (Mines CSCI, RPM) Lisp Spring / 88

23 Common Lisp by Example Numerical Integration Runge-Kutta Methods Given: Find: x(t n ) d Derivative: x(t) = f (x, t) dt Initial time: t Final time: t n Initial value: x(t ) Solution: Follow derivative along discrete time intervals t from t to t n Dantam (Mines CSCI, RPM) Lisp Spring / 88

24 Common Lisp by Example Example: Runge-Kutta (Euler s Method).2.8 x i+ x i + t f (x i, t i ) Dantam (Mines CSCI, RPM) Lisp Spring / 88

25 Common Lisp by Example Example: Runge-Kutta (Euler s Method) continued x i+ x i + t f (x i, t i ) Procedure euler-step(dx, dt, x ) return x + dx dt; ( defun e u l e r s t e p ( dx dt x ) (+ x ( dx dt ) ) ) Dantam (Mines CSCI, RPM) Lisp Spring / 88

26 Common Lisp by Example Example: Runge-Kutta 2 (Midpoint Method) x i+ x i + t ẋ(t i + t 2 {}} ) { f (x i + t 2 f (x i, t i ), t + t 2 ) Procedure rk2-mid(f, t, x, dt) function ks(c, k) is 2 x euler-step(k, c dt, x ); 3 return f (x, t + c dt); 4 k f (x, t ); 5 k ks(/2, k ); 6 return x + dt k ; ( defun rk2 mid step ( f t x dt ) ( l a b e l s ( ( ks ( c k ) ( f u n c a l l f ( e u l e r s t e p k ( c dt ) x ) (+ t ( c dt ) ) ) ) ) ( l e t ( ( k ( f u n c a l l f x t ) ) ( k ( ks (/ 2) k ) ) ) (+ x ( dt k ) ) ) ) ) Dantam (Mines CSCI, RPM) Lisp Spring / 88

27 Common Lisp by Example Exercise: Runge-Kutta 2 (Heun s Method) ẋ(t x i+ x i + t i ) ẋ(t+ t) {}}{ 2 f (x i, t i ) + t {}}{ 2 f (x i + ( t)f (x i, t i ), t + t) x i + t 2 k + t 2 k Dantam (Mines CSCI, RPM) Lisp Spring / 88

28 Common Lisp by Example Exercise: Runge-Kutta 2 (Heun s Method) continued Procedure rk2-heun(f, t, x, dt) function ks(c, k) is 2 x euler-step(k, c dt, x ); 3 return f (x, t + c dt); 4 k f (x, t ); 5 k ks(, k ); 6 return x + dt/2 (k + k ); Dantam (Mines CSCI, RPM) Lisp Spring / 88

29 Common Lisp by Example Exercise: Runge-Kutta 4 where: k = f (x i, t i ) x i+ x i + t 6 ẋ(t i ) {}}{ k + t 3 ẋ(t i + t 2 ) {}}{ k (start) k = f (x i + t 2 k, t i + t 2 ) (midpoint) k 2 = f (x i + t 2 k, t i + t 2 ) (midpoint) k 3 = f (x i + ( t)k 2, t i + t) (end) + t 3 ẋ(t i + t 2 ) {}}{ k 2 + t 6 ẋ(t i + t) {}}{ k 3 Dantam (Mines CSCI, RPM) Lisp Spring / 88

30 Common Lisp by Example Exercise: Runge-Kutta 4 continued Dantam (Mines CSCI, RPM) Lisp Spring 28 3 / 88

31 Common Lisp by Example Euler Integration Procedure int-euler(f, t, t n, dt, x ) if t t n then // Base Case 2 return x ; 3 else // Recursive Case 4 dx f (x, t ); 5 x euler-step(dx, dt, x ); 6 return int-euler(f, t + dt, t n, dt, x); ( defun i n t e u l e r ( f t t dt x ) ( i f (>= t t ) x ( l e t ( ( dx ( f u n c a l l f x t ) ) ( x ( e u l e r s t e p dx dt x ) ) ) ( i n t e u l e r f (+ t dt ) t dt x ) ) ) ) Dantam (Mines CSCI, RPM) Lisp Spring 28 3 / 88

32 Common Lisp by Example RK-2 Integration Procedure int-rk2(f, t, t n, dt, x ) if t t n then // Base Case 2 return x ; 3 else // Recursive Case 4 x rk2-heun(f, t, x, dt); 5 return int-rk2(f, t + dt, t n, dt, x); ( defun int rk2 ( f t t dt x ) ( i f (>= t t ) x ( int rk2 f (+ t dt ) t dt ( rk2 heun f t x dt ) ) ) ) Dantam (Mines CSCI, RPM) Lisp Spring / 88

33 Common Lisp by Example Multi-method RK Integration Procedure integrate(s, f, t, t n, dt, x ) Dantam (Mines CSCI, RPM) Lisp Spring / 88

34 Implementation Details Outline Lisp Common Lisp by Example Implementation Details Typing Memory Management Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI, RPM) Lisp Spring / 88

35 Implementation Details Typing Data Types 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. Examples int float List String Structures: int string float 4 Dantam (Mines CSCI, RPM) Lisp Spring / 88

36 Implementation Details Typing Data Type Systems Type Checking 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 ) Dantam (Mines CSCI, RPM) Lisp Spring / 88

37 Implementation Details Typing Comparison of Language Type Systems C++ Static Java, ML/Haskell C Weak Common Lisp Strong Assembly, Shell Perl Dynamic Python Dantam (Mines CSCI, RPM) Lisp Spring / 88

38 Implementation Details Typing 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, RPM) Lisp Spring / 88

39 Implementation Details Typing Words and Types word 3 xc49fd? (signed) xc49fd? (unsigned) xc49fd? (float) xc49fd? valid pointer Dantam (Mines CSCI, RPM) Lisp Spring / 88

40 Implementation Details Typing 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, RPM) Lisp Spring 28 4 / 88

41 Implementation Details Typing 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, RPM) Lisp Spring 28 4 / 88

42 Implementation Details Typing 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, RPM) Lisp Spring / 88

43 Implementation Details Memory Management 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, RPM) Lisp Spring / 88

44 Implementation Details Memory Management 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, RPM) Lisp Spring / 88

45 Functional Programming Outline Lisp Common Lisp by Example Implementation Details Typing Memory Management Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI, RPM) Lisp Spring / 88

46 Functional Programming Functional Programming Features Functions are first class object Prefer immutable state Garbage collection Dantam (Mines CSCI, RPM) Lisp Spring / 88

47 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, RPM) Lisp Spring / 88

48 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, RPM) Lisp Spring / 88

49 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, RPM) Lisp Spring / 88

50 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, RPM) Lisp Spring 28 5 / 88

51 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, RPM) Lisp Spring 28 5 / 88

52 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, RPM) Lisp Spring / 88

53 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, RPM) Lisp Spring / 88

54 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, RPM) Lisp Spring / 88

55 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, RPM) Lisp Spring / 88

56 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, RPM) Lisp Spring / 88

57 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, RPM) Lisp Spring / 88

58 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, RPM) Lisp Spring / 88

59 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, RPM) Lisp Spring / 88

60 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, RPM) Lisp Spring 28 6 / 88

61 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, RPM) Lisp Spring 28 6 / 88

62 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, RPM) Lisp Spring / 88

63 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, RPM) Lisp Spring / 88

64 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, RPM) Lisp Spring / 88

65 Programming Environment Outline Lisp Common Lisp by Example Implementation Details Typing Memory Management Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI, RPM) Lisp Spring / 88

66 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, RPM) Lisp Spring / 88

67 Programming Environment Demo SLIME, pstree Read-Eval-Print-Loop (REPL) DEFUN DISASSEMBLE Re-DEFUN Dantam (Mines CSCI, RPM) Lisp Spring / 88

68 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, RPM) Lisp Spring / 88

69 Programming Environment Summary Lisp Common Lisp by Example Implementation Details Typing Memory Management Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI, RPM) Lisp Spring / 88

70 Appendix Outline Lisp Common Lisp by Example Implementation Details Typing Memory Management Functional Programming Closures Recursion Functional Operators Programming Environment Appendix Dantam (Mines CSCI, RPM) Lisp Spring 28 7 / 88

71 Appendix L Hôpital s Rule Evaluate limits using derivatives when f (a) g(a) (( ) ( )) lim f (x) = lim g(x) = x a x a (similarly for ): = ( f (x) lim x a g(x) = lim f ) (x) x a g (x) Dantam (Mines CSCI, RPM) Lisp Spring 28 7 / 88

72 Appendix LET Creates a new scope and variable bindings Dantam (Mines CSCI, RPM) Lisp Spring / 88

73 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, RPM) Lisp Spring / 88

74 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, RPM) Lisp Spring / 88

75 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, RPM) Lisp Spring / 88

76 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, RPM) Lisp Spring / 88

77 Appendix DOTIMES Iterate a for n steps Dantam (Mines CSCI, RPM) Lisp Spring / 88

78 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, RPM) Lisp Spring / 88

79 Appendix DOLIST Iterate over a list Dantam (Mines CSCI, RPM) Lisp Spring / 88

80 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, RPM) Lisp Spring 28 8 / 88

81 Appendix Example: LOOP counting Lisp ( loop f o r i below 5 do ( p r i n t i ) ) Output Dantam (Mines CSCI, RPM) Lisp Spring 28 8 / 88

82 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, RPM) Lisp Spring / 88

83 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, RPM) Lisp Spring / 88

84 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, RPM) Lisp Spring / 88

85 Appendix Case Control structure Selects clause that matches the test argument Dantam (Mines CSCI, RPM) Lisp Spring / 88

86 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, RPM) Lisp Spring / 88

87 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, RPM) Lisp Spring / 88

88 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, RPM) Lisp Spring / 88

1. Write a program to calculate distance traveled by light

1. Write a program to calculate distance traveled by light G. H. R a i s o n i C o l l e g e O f E n g i n e e r i n g D i g d o h H i l l s, H i n g n a R o a d, N a g p u r D e p a r t m e n t O f C o m p u t e r S c i e n c e & E n g g P r a c t i c a l M a

More information

Outline. policies for the first part. with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014

Outline. policies for the first part. with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014 Outline 1 midterm exam on Friday 11 July 2014 policies for the first part 2 questions with some potential answers... MCS 260 Lecture 10.0 Introduction to Computer Science Jan Verschelde, 9 July 2014 Intro

More information

Computer Science Introductory Course MSc - Introduction to Java

Computer Science Introductory Course MSc - Introduction to Java Computer Science Introductory Course MSc - Introduction to Java Lecture 1: Diving into java Pablo Oliveira ENST Outline 1 Introduction 2 Primitive types 3 Operators 4 5 Control Flow

More information

Propositional Calculus

Propositional Calculus Propositional Calculus Dr. Neil T. Dantam CSCI-498/598 RPM, Colorado School of Mines Spring 2018 Dantam (Mines CSCI, RPM) Propositional Calculus Spring 2018 1 / 64 Calculus? Definition: Calculus A well

More information

Meta-Circularity, and Vice-Versa

Meta-Circularity, and Vice-Versa 1/60, and Vice-Versa didier@lrde.epita.fr http://www.lrde.epita.fr/ didier ACCU 2011 Thursday, April 14th 2/60 Table of contents 1 2 Symbolic Expressions 3 The of LISP 4 5 6 7 8 Dynamic 9 4/60 circularity

More information

6.001 Recitation 22: Streams

6.001 Recitation 22: Streams 6.001 Recitation 22: Streams RI: Gerald Dalley, dalleyg@mit.edu, 4 May 2007 http://people.csail.mit.edu/dalleyg/6.001/sp2007/ The three chief virtues of a programmer are: Laziness, Impatience and Hubris

More information

Python. chrysn

Python. chrysn Python chrysn 2008-09-25 Introduction Structure, Language & Syntax Strengths & Weaknesses Introduction Structure, Language & Syntax Strengths & Weaknesses Python Python is an interpreted,

More information

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 43

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 43 Finite Automata Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2018 Dantam (Mines CSCI-561) Finite Automata Fall 2018 1 / 43 Outline Languages Review Traffic Light Example Deterministic Finite

More information

Recursion - 1. Recursion. Recursion - 2. A Simple Recursive Example

Recursion - 1. Recursion. Recursion - 2. A Simple Recursive Example Recursion - 1 Only looping method in pure Lisp is recursion Recursion Based on Chapter 6 of Wilensky Recursive function f includes calls to f in its definition For a recursive function to eventually terminate

More information

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 35

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 35 Finite Automata Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Finite Automata Fall 2017 1 / 35 Outline Dantam (Mines CSCI-561) Finite Automata Fall 2017 2 / 35

More information

Functional Programming with F# Overview and Basic Concepts

Functional Programming with F# Overview and Basic Concepts Functional Programming with F# Overview and Basic Concepts Radu Nicolescu Department of Computer Science University of Auckland 27 Sep 2017 1 / 52 1 Background 2 Overview 3 Type System and Type Inference

More information

Lambda Calculus! Gunnar Gotshalks! LC-1

Lambda Calculus! Gunnar Gotshalks! LC-1 Lambda Calculus! LC-1 λ Calculus History! Developed by Alonzo Church during mid 1930 s! One fundamental goal was to describe what can be computed.! Full definition of λ-calculus is equivalent in power

More information

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review:

Mathematical Background. Unsigned binary numbers. Powers of 2. Logs and exponents. Mathematical Background. Today, we will review: Mathematical Background Mathematical Background CSE 373 Data Structures Today, we will review: Logs and eponents Series Recursion Motivation for Algorithm Analysis 5 January 007 CSE 373 - Math Background

More information

Big-O Notation and Complexity Analysis

Big-O Notation and Complexity Analysis Big-O Notation and Complexity Analysis Jonathan Backer backer@cs.ubc.ca Department of Computer Science University of British Columbia May 28, 2007 Problems Reading: CLRS: Growth of Functions 3 GT: Algorithm

More information

Remainders. We learned how to multiply and divide in elementary

Remainders. We learned how to multiply and divide in elementary Remainders We learned how to multiply and divide in elementary school. As adults we perform division mostly by pressing the key on a calculator. This key supplies the quotient. In numerical analysis and

More information

Programming Language Concepts, CS2104 Lecture 3

Programming Language Concepts, CS2104 Lecture 3 Programming Language Concepts, CS2104 Lecture 3 Statements, Kernel Language, Abstract Machine 31 Aug 2007 CS2104, Lecture 3 1 Reminder of last lecture Programming language definition: syntax, semantics

More information

Reference counting:

Reference counting: Invariant: Reference counting: Reference counting: 2 0 3 2 0 2 2 0 2 2 2 2 2 white gray r r r black do not ;; init-allocator : -> void? (define (init-allocator) (for ([i (in-range 0 (heap-size))])

More information

Algorithm efficiency analysis

Algorithm efficiency analysis Algorithm efficiency analysis Mădălina Răschip, Cristian Gaţu Faculty of Computer Science Alexandru Ioan Cuza University of Iaşi, Romania DS 2017/2018 Content Algorithm efficiency analysis Recursive function

More information

Introduction to Programming (Java) 3/12

Introduction to Programming (Java) 3/12 Introduction to Programming (Java) 3/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction

More information

Taylor and Maclaurin Series

Taylor and Maclaurin Series Taylor and Maclaurin Series MATH 211, Calculus II J. Robert Buchanan Department of Mathematics Spring 2018 Background We have seen that some power series converge. When they do, we can think of them as

More information

Complexity (Pre Lecture)

Complexity (Pre Lecture) Complexity (Pre Lecture) Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2018 Dantam (Mines CSCI-561) Complexity (Pre Lecture) Fall 2018 1 / 70 Why? What can we always compute efficiently? What

More information

I. Numerical Computing

I. Numerical Computing I. Numerical Computing A. Lectures 1-3: Foundations of Numerical Computing Lecture 1 Intro to numerical computing Understand difference and pros/cons of analytical versus numerical solutions Lecture 2

More information

Numerical Methods - Preliminaries

Numerical Methods - Preliminaries Numerical Methods - Preliminaries Y. K. Goh Universiti Tunku Abdul Rahman 2013 Y. K. Goh (UTAR) Numerical Methods - Preliminaries 2013 1 / 58 Table of Contents 1 Introduction to Numerical Methods Numerical

More information

6.001 SICP September? Procedural abstraction example: sqrt (define try (lambda (guess x) (if (good-enuf? guess x) guess (try (improve guess x) x))))

6.001 SICP September? Procedural abstraction example: sqrt (define try (lambda (guess x) (if (good-enuf? guess x) guess (try (improve guess x) x)))) September? 600-Introduction Trevor Darrell trevor@csail.mit.edu -D5 6.00 web page: http://sicp.csail.mit.edu/ section web page: http://www.csail.mit.edu/~trevor/600/ Abstractions Pairs and lists Common

More information

Context-Free Languages (Pre Lecture)

Context-Free Languages (Pre Lecture) Context-Free Languages (Pre Lecture) Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Context-Free Languages (Pre Lecture) Fall 2017 1 / 34 Outline Pumping Lemma

More information

Searching, mainly via Hash tables

Searching, mainly via Hash tables Data structures and algorithms Part 11 Searching, mainly via Hash tables Petr Felkel 26.1.2007 Topics Searching Hashing Hash function Resolving collisions Hashing with chaining Open addressing Linear Probing

More information

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Outline. Last class. Instructor: X. Zhang Lecture 2 Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2 Outline Introduction to algorithm analysis: fibonacci seq calculation counting number of computer steps recursive formula

More information

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2

Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2 Computer Algorithms CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Lecture 2 Outline Introduction to algorithm analysis: fibonacci seq calculation counting number of computer steps recursive formula

More information

Space Complexity. The space complexity of a program is how much memory it uses.

Space Complexity. The space complexity of a program is how much memory it uses. Space Complexity The space complexity of a program is how much memory it uses. Measuring Space When we compute the space used by a TM, we do not count the input (think of input as readonly). We say that

More information

Pushdown Automata (Pre Lecture)

Pushdown Automata (Pre Lecture) Pushdown Automata (Pre Lecture) Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Pushdown Automata (Pre Lecture) Fall 2017 1 / 41 Outline Pushdown Automata Pushdown

More information

Building Algorithmic Polytopes. David Bremner with D. Avis, H.R. Tiwary, and O. Watanabe December 16, 2014

Building Algorithmic Polytopes. David Bremner with D. Avis, H.R. Tiwary, and O. Watanabe December 16, 2014 Building Algorithmic Polytopes David Bremner with D. Avis, H.R. Tiwary, and O. Watanabe December 16, 2014 Outline Matchings in graphs Given G = (V, E), M E is called a matching {e M v e} 1 v V A matching

More information

ITI Introduction to Computing II

ITI Introduction to Computing II (with contributions from R. Holte) School of Electrical Engineering and Computer Science University of Ottawa Version of January 11, 2015 Please don t print these lecture notes unless you really need to!

More information

Checking the Radioactive Decay Euler Algorithm

Checking the Radioactive Decay Euler Algorithm Lecture 2: Checking Numerical Results Review of the first example: radioactive decay The radioactive decay equation dn/dt = N τ has a well known solution in terms of the initial number of nuclei present

More information

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction

Math 324 Summer 2012 Elementary Number Theory Notes on Mathematical Induction Math 4 Summer 01 Elementary Number Theory Notes on Mathematical Induction Principle of Mathematical Induction Recall the following axiom for the set of integers. Well-Ordering Axiom for the Integers If

More information

Operational Semantics

Operational Semantics Operational Semantics Semantics and applications to verification Xavier Rival École Normale Supérieure Xavier Rival Operational Semantics 1 / 50 Program of this first lecture Operational semantics Mathematical

More information

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang

Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Algorithm Analysis, Asymptotic notations CISC4080 CIS, Fordham Univ. Instructor: X. Zhang Last class Introduction to algorithm analysis: fibonacci seq calculation counting number of computer steps recursive

More information

Big O (Asymptotic Upper Bound)

Big O (Asymptotic Upper Bound) Big O (Asymptotic Upper Bound) Linear search takes O(n) time. Binary search takes O(lg(n)) time. (lg means log 2 ) Bubble sort takes O(n 2 ) time. n 2 + 2n + 1 O(n 2 ), n 2 + 2n + 1 O(n) Definition: f

More information

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes. Analysis of Algorithms Fibonacci Heaps Andres Mendez-Vazquez October 29, 2015 1 / 119 Outline 1 Introduction Basic Definitions Ordered Trees 2 Binomial Trees Example 3 Fibonacci Heap Operations Fibonacci

More information

CMSC 313 Lecture 16 Announcement: no office hours today. Good-bye Assembly Language Programming Overview of second half on Digital Logic DigSim Demo

CMSC 313 Lecture 16 Announcement: no office hours today. Good-bye Assembly Language Programming Overview of second half on Digital Logic DigSim Demo CMSC 33 Lecture 6 nnouncement: no office hours today. Good-bye ssembly Language Programming Overview of second half on Digital Logic DigSim Demo UMC, CMSC33, Richard Chang Good-bye ssembly

More information

Computation Theory Finite Automata

Computation Theory Finite Automata Computation Theory Dept. of Computing ITT Dublin October 14, 2010 Computation Theory I 1 We would like a model that captures the general nature of computation Consider two simple problems: 2 Design a program

More information

Computer Science. Questions for discussion Part II. Computer Science COMPUTER SCIENCE. Section 4.2.

Computer Science. Questions for discussion Part II. Computer Science COMPUTER SCIENCE. Section 4.2. COMPUTER SCIENCE S E D G E W I C K / W A Y N E PA R T I I : A L G O R I T H M S, T H E O R Y, A N D M A C H I N E S Computer Science Computer Science An Interdisciplinary Approach Section 4.2 ROBERT SEDGEWICK

More information

MATHEMATICAL OBJECTS in

MATHEMATICAL OBJECTS in MATHEMATICAL OBJECTS in Computational Tools in a Unified Object-Oriented Approach Yair Shapira @ CRC Press Taylor & Francis Group Boca Raton London New York CRC Press is an imprint of the Taylor & Francis

More information

Solutions to EoPL3 Exercises

Solutions to EoPL3 Exercises Solutions to EoPL3 Exercises Release 0.1.0 Cheng Lian May 16, 2017 Contents 1 Contents 3 2 Overview 29 i ii Author Cheng Lian Contents 1 2 Contents CHAPTER 1 Contents Chapter 1.

More information

Problem Set 5 Solutions

Problem Set 5 Solutions UNIVERSITY OF ALABAMA Department of Physics and Astronomy PH 25 / LeClair Spring 2009. Problem 7.5 from your textbook Problem Set 5 Solutions (a) We are given the initial and final position vectors d i

More information

Problem Set 1 Solutions

Problem Set 1 Solutions Introduction to Algorithms September 24, 2004 Massachusetts Institute of Technology 6.046J/18.410J Professors Piotr Indyk and Charles E. Leiserson Handout 7 Problem Set 1 Solutions Exercise 1-1. Do Exercise

More information

Lecture 22: Multithreaded Algorithms CSCI Algorithms I. Andrew Rosenberg

Lecture 22: Multithreaded Algorithms CSCI Algorithms I. Andrew Rosenberg Lecture 22: Multithreaded Algorithms CSCI 700 - Algorithms I Andrew Rosenberg Last Time Open Addressing Hashing Today Multithreading Two Styles of Threading Shared Memory Every thread can access the same

More information

1 What is numerical analysis and scientific computing?

1 What is numerical analysis and scientific computing? Mathematical preliminaries 1 What is numerical analysis and scientific computing? Numerical analysis is the study of algorithms that use numerical approximation (as opposed to general symbolic manipulations)

More information

More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010

More About Methods. Hsuan-Tien Lin. Deptartment of CSIE, NTU. OOP Class, March 8-9, 2010 More About Methods Hsuan-Tien Lin Deptartment of CSIE, NTU OOP Class, March 8-9, 2010 H.-T. Lin (NTU CSIE) More About Methods OOP 03/08-09/2010 0 / 24 Methods: the Basic Method (1/2, Callee s View) 1 p

More information

PHYS 301 HOMEWORK #5

PHYS 301 HOMEWORK #5 PHY 3 HOMEWORK #5 olutions On this homework assignment, you may use Mathematica to compute integrals, but you must submit your Mathematica output with your assignment.. For f x = -, - < x

More information

Shared on QualifyGate.com

Shared on QualifyGate.com CS-GATE-05 GATE 05 A Brief Analysis (Based on student test experiences in the stream of CS on 8th February, 05 (Morning Session) Section wise analysis of the paper Section Classification Mark Marks Total

More information

Numerical integration for solving differential equations

Numerical integration for solving differential equations Numerical integration for solving differential equations After integration, it is natural to consider how to find numerical solutions to differential equations on the computer. Simple equations of motion

More information

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1 Higher-Order Programming: Iterative computation (CTM Section 3.2) Closures, procedural abstraction, genericity, instantiation, embedding (CTM Section 3.6.1) Carlos Varela RPI September 15, 2015 Adapted

More information

CS Non-recursive and Recursive Algorithm Analysis

CS Non-recursive and Recursive Algorithm Analysis CS483-04 Non-recursive and Recursive Algorithm Analysis Instructor: Fei Li Room 443 ST II Office hours: Tue. & Thur. 4:30pm - 5:30pm or by appointments lifei@cs.gmu.edu with subject: CS483 http://www.cs.gmu.edu/

More information

We are going to discuss what it means for a sequence to converge in three stages: First, we define what it means for a sequence to converge to zero

We are going to discuss what it means for a sequence to converge in three stages: First, we define what it means for a sequence to converge to zero Chapter Limits of Sequences Calculus Student: lim s n = 0 means the s n are getting closer and closer to zero but never gets there. Instructor: ARGHHHHH! Exercise. Think of a better response for the instructor.

More information

ITI Introduction to Computing II

ITI Introduction to Computing II (with contributions from R. Holte) School of Electrical Engineering and Computer Science University of Ottawa Version of January 9, 2019 Please don t print these lecture notes unless you really need to!

More information

The Big Picture. Python environment? Discuss Examples of unpredictability. Chaos, Scientific American (1986)

The Big Picture. Python environment? Discuss Examples of unpredictability. Chaos, Scientific American (1986) The Big Picture Python environment? Discuss Examples of unpredictability Email homework to me: chaos@cse.ucdavis.edu Chaos, Scientific American (1986) Odds, Stanislaw Lem, The New Yorker (1974) 1 Nonlinear

More information

Practice Midterm Exam

Practice Midterm Exam CS/MATH320L - Applied Discrete Math Spring 2013 Instructor: Marc Pomplun Practice Midterm Exam Sample Solutions Question 1: out of points Question 2: out of points Question 3: out of points Question 4:

More information

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture CS429: Computer Organization and Architecture Warren Hunt, Jr. and Bill Young Department of Computer Sciences University of Texas at Austin Last updated: September 3, 2014 at 08:38 CS429 Slideset C: 1

More information

Data Structures and Algorithms Chapter 2

Data Structures and Algorithms Chapter 2 1 Data Structures and Algorithms Chapter 2 Werner Nutt 2 Acknowledgments The course follows the book Introduction to Algorithms, by Cormen, Leiserson, Rivest and Stein, MIT Press [CLRST]. Many examples

More information

CSCI 2200 Foundations of Computer Science Spring 2018 Quiz 3 (May 2, 2018) SOLUTIONS

CSCI 2200 Foundations of Computer Science Spring 2018 Quiz 3 (May 2, 2018) SOLUTIONS CSCI 2200 Foundations of Computer Science Spring 2018 Quiz 3 (May 2, 2018) SOLUTIONS 1. [6 POINTS] For language L 1 = {0 n 1 m n, m 1, m n}, which string is in L 1? ANSWER: 0001111 is in L 1 (with n =

More information

Recursion: Introduction and Correctness

Recursion: Introduction and Correctness Recursion: Introduction and Correctness CSE21 Winter 2017, Day 7 (B00), Day 4-5 (A00) January 25, 2017 http://vlsicad.ucsd.edu/courses/cse21-w17 Today s Plan From last time: intersecting sorted lists and

More information

COMS 6100 Class Notes

COMS 6100 Class Notes COMS 6100 Class Notes Daniel Solus September 20, 2016 1 General Remarks The Lecture notes submitted by the class have been very good. Integer division seemed to be a common oversight when working the Fortran

More information

7. The Recursion Theorem

7. The Recursion Theorem 7. The Recursion Theorem Main result in this section: Kleene s Recursion Theorem. Recursive functions are closed under a very general form of recursion. For proof we will use the S-m-n-theorem. Used in

More information

Topic 17. Analysis of Algorithms

Topic 17. Analysis of Algorithms Topic 17 Analysis of Algorithms Analysis of Algorithms- Review Efficiency of an algorithm can be measured in terms of : Time complexity: a measure of the amount of time required to execute an algorithm

More information

Regular Expressions (Pre Lecture)

Regular Expressions (Pre Lecture) Regular Expressions (Pre Lecture) Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2017 Dantam (Mines CSCI-561) Regular Expressions (Pre Lecture) Fall 2017 1 / 39 Regular Expressions Outline

More information

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00 FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING MODULE CAMPUS CSC2A10 OBJECT ORIENTED PROGRAMMING AUCKLAND PARK CAMPUS (APK) EXAM JULY 2014 DATE 07/2014 SESSION 8:00-10:00 ASSESOR(S)

More information

Aside: Golden Ratio. Golden Ratio: A universal law. Golden ratio φ = lim n = 1+ b n = a n 1. a n+1 = a n + b n, a n+b n a n

Aside: Golden Ratio. Golden Ratio: A universal law. Golden ratio φ = lim n = 1+ b n = a n 1. a n+1 = a n + b n, a n+b n a n Aside: Golden Ratio Golden Ratio: A universal law. Golden ratio φ = lim n a n+b n a n = 1+ 5 2 a n+1 = a n + b n, b n = a n 1 Ruta (UIUC) CS473 1 Spring 2018 1 / 41 CS 473: Algorithms, Spring 2018 Dynamic

More information

Errata List Numerical Mathematics and Computing, 7th Edition Ward Cheney & David Kincaid Cengage Learning (c) March 2013

Errata List Numerical Mathematics and Computing, 7th Edition Ward Cheney & David Kincaid Cengage Learning (c) March 2013 Chapter Errata List Numerical Mathematics and Computing, 7th Edition Ward Cheney & David Kincaid Cengage Learning (c) 202 9 March 203 Page 4, Summary, 2nd bullet item, line 4: Change A segment of to The

More information

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS CST.2016.2.1 COMPUTER SCIENCE TRIPOS Part IA Tuesday 31 May 2016 1.30 to 4.30 COMPUTER SCIENCE Paper 2 Answer one question from each of Sections A, B and C, and two questions from Section D. Submit the

More information

Practical Information

Practical Information MA2501 Numerical Methods Spring 2018 Norwegian University of Science and Technology Department of Mathematical Sciences Semester Project Practical Information This project counts for 30 % of the final

More information

Python. Tutorial. Jan Pöschko. March 22, Graz University of Technology

Python. Tutorial. Jan Pöschko. March 22, Graz University of Technology Tutorial Graz University of Technology March 22, 2010 Why? is: very readable easy to learn interpreted & interactive like a UNIX shell, only better object-oriented but not religious about it slower than

More information

Functional Programming Laboratory 3

Functional Programming Laboratory 3 Functional Programming Laboratory 3 Define new functions, Recursion Isabela Drămnesc March 12, 2014 1 Concepts Bound and free variables Recursive functions Simple and double recursion 2 Questions from

More information

CSCI Honor seminar in algorithms Homework 2 Solution

CSCI Honor seminar in algorithms Homework 2 Solution CSCI 493.55 Honor seminar in algorithms Homework 2 Solution Saad Mneimneh Visiting Professor Hunter College of CUNY Problem 1: Rabin-Karp string matching Consider a binary string s of length n and another

More information

7. The Recursion Theorem

7. The Recursion Theorem 7. The Recursion Theorem Main result in this section: Kleene s Recursion Theorem. Recursive functions are closed under a very general form of recursion. For the proof we will use the S-m-n-theorem. Used

More information

7. The Recursion Theorem

7. The Recursion Theorem 7. The Recursion Theorem Main result in this section: Kleene s Recursion Theorem. Recursive functions are closed under a very general form of recursion. For proof we will use the S-m-n-theorem. Used in

More information

1 + lim. n n+1. f(x) = x + 1, x 1. and we check that f is increasing, instead. Using the quotient rule, we easily find that. 1 (x + 1) 1 x (x + 1) 2 =

1 + lim. n n+1. f(x) = x + 1, x 1. and we check that f is increasing, instead. Using the quotient rule, we easily find that. 1 (x + 1) 1 x (x + 1) 2 = Chapter 5 Sequences and series 5. Sequences Definition 5. (Sequence). A sequence is a function which is defined on the set N of natural numbers. Since such a function is uniquely determined by its values

More information

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course.

INTRODUCTION. This is not a full c-programming course. It is not even a full 'Java to c' programming course. C PROGRAMMING 1 INTRODUCTION This is not a full c-programming course. It is not even a full 'Java to c' programming course. 2 LITTERATURE 3. 1 FOR C-PROGRAMMING The C Programming Language (Kernighan and

More information

ECE260: Fundamentals of Computer Engineering

ECE260: Fundamentals of Computer Engineering Data Representation & 2 s Complement James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy Data

More information

R: A Quick Reference

R: A Quick Reference R: A Quick Reference Colorado Reed January 17, 2012 Contents 1 Basics 2 1.1 Arrays and Matrices....................... 2 1.2 Lists................................ 3 1.3 Loading Packages.........................

More information

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015 CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015 Today Registration should be done. Homework 1 due 11:59pm next Wednesday, April 8 th. Review math

More information

Ordinary Differential Equations II: Runge-Kutta and Advanced Methods

Ordinary Differential Equations II: Runge-Kutta and Advanced Methods Ordinary Differential Equations II: Runge-Kutta and Advanced Methods Sam Sinayoko Numerical Methods 3 Contents 1 Learning Outcomes 2 2 Introduction 2 2.1 Note................................ 4 2.2 Limitations

More information

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS CST.2016.6.1 COMPUTER SCIENCE TRIPOS Part IB Thursday 2 June 2016 1.30 to 4.30 COMPUTER SCIENCE Paper 6 Answer five questions. Submit the answers in five separate bundles, each with its own cover sheet.

More information

ww.padasalai.net

ww.padasalai.net t w w ADHITHYA TRB- TET COACHING CENTRE KANCHIPURAM SUNDER MATRIC SCHOOL - 9786851468 TEST - 2 COMPUTER SCIENC PG - TRB DATE : 17. 03. 2019 t et t et t t t t UNIT 1 COMPUTER SYSTEM ARCHITECTURE t t t t

More information

Anatomy of SINGULAR talk at p. 1

Anatomy of SINGULAR talk at p. 1 Anatomy of SINGULAR talk at MaGiX@LIX 2011- p. 1 Anatomy of SINGULAR talk at MaGiX@LIX 2011 - Hans Schönemann hannes@mathematik.uni-kl.de Department of Mathematics University of Kaiserslautern Anatomy

More information

GATE 2015 A Brief Analysis (Based on student test experiences in the stream of CS on 8th February, 2015 (Morning Session)

GATE 2015 A Brief Analysis (Based on student test experiences in the stream of CS on 8th February, 2015 (Morning Session) CS-GATE-05 GATE 05 A Brief Analysis (Based on student test experiences in the stream of CS on 8th February, 05 (Morning Session) Section wise analysis of the paper Section Classification Mark Marks Total

More information

Algorithms and Their Complexity

Algorithms and Their Complexity CSCE 222 Discrete Structures for Computing David Kebo Houngninou Algorithms and Their Complexity Chapter 3 Algorithm An algorithm is a finite sequence of steps that solves a problem. Computational complexity

More information

MiniMat: Matrix Language in OCaml LLVM

MiniMat: Matrix Language in OCaml LLVM Terence Lim - tl2735@columbia.edu August 17, 2016 Contents 1 Introduction 4 1.1 Goals........................................ 4 1.1.1 Flexible matrix notation......................... 4 1.1.2 Uncluttered................................

More information

Meta-programming & you

Meta-programming & you Meta-programming & you Robin Message Cambridge Programming Research Group 10 th May 2010 What s meta-programming about? 1 result=somedb. customers. select 2 { first_name+ +last_name } 3 where name LIKE

More information

Differential Equations

Differential Equations Differential Equations Definitions Finite Differences Taylor Series based Methods: Euler Method Runge-Kutta Methods Improved Euler, Midpoint methods Runge Kutta (2nd, 4th order) methods Predictor-Corrector

More information

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1

Adapted with permission from: Seif Haridi KTH Peter Van Roy UCL. C. Varela; Adapted w. permission from S. Haridi and P. Van Roy 1 Higher-Order Programming: Iterative computation (CTM Section 3.2) Closures, procedural abstraction, genericity, instantiation, embedding (CTM Section 3.6.1) Carlos Varela RPI September 15, 2017 Adapted

More information

Übung Informatik I - Programmierung - Blatt 7

Übung Informatik I - Programmierung - Blatt 7 RHEINISCH- WESTFÄLISCHE TECHNISCHE HOCHSCHULE AACHEN LEHR- UND FORSCHUNGSGEBIET INFORMATIK II RWTH Aachen D-52056 Aachen GERMANY http://programmierung.informatik.rwth-aachen.de LuFG Informatik II Prof.

More information

Advanced topic: Space complexity

Advanced topic: Space complexity Advanced topic: Space complexity CSCI 3130 Formal Languages and Automata Theory Siu On CHAN Chinese University of Hong Kong Fall 2016 1/28 Review: time complexity We have looked at how long it takes to

More information

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019 CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis Ruth Anderson Winter 2019 Today Algorithm Analysis What do we care about? How to compare two algorithms Analyzing Code Asymptotic Analysis

More information

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS

LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS G.PULLAIAH COLLEGE OF ENGINEERING AND TECHNOLOGY LECTURE NOTES ON DESIGN AND ANALYSIS OF ALGORITHMS Department of Computer Science and Engineering 1 UNIT 1 Basic Concepts Algorithm An Algorithm is a finite

More information

Recursive Definitions

Recursive Definitions Recursive Definitions Example: Give a recursive definition of a n. a R and n N. Basis: n = 0, a 0 = 1. Recursion: a n+1 = a a n. Example: Give a recursive definition of n i=0 a i. Let S n = n i=0 a i,

More information

COMPUTER ARITHMETIC. 13/05/2010 cryptography - math background pp. 1 / 162

COMPUTER ARITHMETIC. 13/05/2010 cryptography - math background pp. 1 / 162 COMPUTER ARITHMETIC 13/05/2010 cryptography - math background pp. 1 / 162 RECALL OF COMPUTER ARITHMETIC computers implement some types of arithmetic for instance, addition, subtratction, multiplication

More information

Motivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis

Motivation. Dictionaries. Direct Addressing. CSE 680 Prof. Roger Crawfis Motivation Introduction to Algorithms Hash Tables CSE 680 Prof. Roger Crawfis Arrays provide an indirect way to access a set. Many times we need an association between two sets, or a set of keys and associated

More information

Lecture 1: The arithmetic hierarchy

Lecture 1: The arithmetic hierarchy MODEL THEORY OF ARITHMETIC Lecture 1: The arithmetic hierarchy Tin Lok Wong 8 October, 2014 [These theorems] go a long way to explaining why recursion theory is relevant to the study of models of arithmetic.

More information

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure

Measuring Goodness of an Algorithm. Asymptotic Analysis of Algorithms. Measuring Efficiency of an Algorithm. Algorithm and Data Structure Measuring Goodness of an Algorithm Asymptotic Analysis of Algorithms EECS2030 B: Advanced Object Oriented Programming Fall 2018 CHEN-WEI WANG 1. Correctness : Does the algorithm produce the expected output?

More information

Scripting Languages Fast development, extensible programs

Scripting Languages Fast development, extensible programs Scripting Languages Fast development, extensible programs Devert Alexandre School of Software Engineering of USTC November 30, 2012 Slide 1/60 Table of Contents 1 Introduction 2 Dynamic languages A Python

More information