Download presentation
Presentation is loading. Please wait.
1
Scheme for Python Programmers CSE 399 005 Valeria Montero
2
Scheme LISP origins AI (MIT): High level features reprogram Slower Teaching Scheme and Python: Lambda, deep lexicals
3
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
4
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
5
Getting Started Define ;Create new objects: Global Variable or Function (define ) Operators ( …) Common Operators: and, or, not, -, +, *, %, /
6
Scheme Python Pre-Operator Order ( #t(+ 3 5) => 8(and #t #f) => #f (Blocks)
7
Quick Sort
9
Conditional (cond ( ) ( ) ( )) Absolute Value: (define (abs x) (cond ((< x 0) (- x)) (x))) Python: If Elif Else
10
CAR and CDR (car list) => first element (car '(a b c)) => a (cdr list) => list excluding car (cdr '(a b c)) => (b c)
11
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)
12
APPEND and CONS Append: Concatenates two lists Cons: Constructs list with input: car, cdr > (append '(1 2) '(3 4)) (1 2 3 4) > (cons '(1 2) '(3 4)) ((1 2) 3 4)
14
Let Variables are bounded locally within Let body. (let ((variable 1 value 1 )...) expression 1 expression 2...) (let ((a (* 4 4))) (+ a a)) => 32 (let ((+ *)) (+ 2 3)) => 6 Absolute Value: (define (abs n) (let ((a n) (b (-n))) (max a b)))
15
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
17
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 (variable 1...) expression 1 expression 2...) value 1...) λ Absolute Value: (define abs (λ (n) (if (>= n 0) n (- n))))
18
List Recursion Style Style Style …
19
Permutations
20
For Comparison
21
Do: Iterative vs. Recursive (do ((variable 1 value 1 ) (variable 2 value 2 ) …) ((exitCondition) (exitFunction)) Body)
22
Fun Debugging in DrScheme (car '()) > car: expects argument of type ; given () UESTIONS?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.