scheme for python programmers cse 399 005 valeria montero
Post on 20-Dec-2015
227 views
TRANSCRIPT
Scheme for Python Programmers
CSE 399 005Valeria Montero
Scheme
LISP origins
AI (MIT): High level features
reprogram
Slower
Teaching
Scheme and Python: Lambda, deep lexicals
Scheme vs. Python
Synthax: Prefix vs. main-stream
Lexical Scope: Parentheses vs. Whitespace
Control Structures: Functional
(tail-recursion-optimizing) vs. . . mainstream (while, for loops)
Support: Standard (R5RS, IEEE) vs. not
OOP: indirect support vs.
Standard object system
Orientation: Mathematical vs. Processor
Scheme vs. Python Implementation: Many vs. 2 synchronized
. (Portable)
Garbage collection: Strong (cycles) vs. weak
Built in: No standard library vs.
many data types, functions . (regular expressions,
internet connectivity)
Macros vs. not
Scheme great for scripting/extension
Getting StartedDefine
;Create new objects: Global Variable or Function
(define <GlobalVariable1> <value1>)
(define <function1 <expression1>)
Operators
(<operator> <expression1> <expression2> …)
Common Operators: and, or, not, -, +, *, %, /
Scheme
Python
Pre-Operator Order
(< a b) => #t(+ 3 5) => 8(and #t #f) => #f(Blocks)
Quick Sort
Conditional(cond (<condition1> <execute1>) (<condition2> <execute2>) (<conditionn> <executen>))
Absolute Value:
(define (abs x) (cond ((< x 0) (- x)) (x)))
Python:IfElifElse
CAR and CDR
(car list) => first element
(car '(a b c)) => a
(cdr list) => list excluding car
(cdr '(a b c)) => (b c)
Play Around with Nested Lists:
Reference maximum four car/cdr
(cadar '((1 2 3) 3)) => 2
(cadr '(1 2 3)) => 2
(cadar '((1 2 3) 4 5 6)) => 2
(cddddr '(1 2 3 4 5 6)) =>(5 6)
APPEND and CONS
Append: Concatenates two listsCons: Constructs list with input: car, cdr
> (append '(1 2) '(3 4))(1 2 3 4)> (cons '(1 2) '(3 4))((1 2) 3 4)
Let
Variables are bounded locally within Let body.
(let ((variable1 value1) ...) expression1 expression2 ...)
(let ((a (* 4 4))) (+ a a)) => 32
(let ((+ *)) (+ 2 3)) => 6
Absolute Value:
(define (abs n) (let ((a n) (b (-n))) (max a b)))
Let vs. Let*
Let evaluates the variables in parallel.
Let* evaluates the variables sequentially.
(let ((a 5)
(b (* a 10)))
b)
=> reference to undefined identifier: a
(let* ((a 5)
(b (* a 10)))
b)
=> 50
Python
Parallel assignments
a, b = b, a
Scheme Functional Programming: Lambda Calculus
COND Absolute Value:
(define (abs x) (cond ((< x 0) (- x)) (x)))
LET Absolute Value:
(define (abs n) (let ((a n) (b (-n))) (max a b)))
Lambda (λ)((lambda (variable1 ...) expression1 expression2 ...) value1 ...)
λ Absolute Value:
(define abs (λ (n) (if (>= n 0) n (- n))))
List Recursion Style Style Style …
Permutations
For Comparison
Do: Iterative vs. Recursive
(do ((variable1 value1) (variable2 value2) …) ((exitCondition) (exitFunction)) Body)
Fun Debugging in DrScheme
(car '())
> car: expects argument of type <pair>; given ()
UESTIONS?