Download Scheme

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Scheme for Python
Programmers
CSE 399 005
Valeria 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 Started
Define
;Create new objects: Global Variable or Function
(define <GlobalVariable1> <value1>)
(define <function1
<expression1>)
Operators
(<operator> <expression1>
<expression2> …)
Common Operators: and, or, not, -, +, *, %, /
Scheme
Pre-Operator Order
(< a b) => #t
(+ 3 5) => 8
(and #t #f) => #f
(Blocks)
Python
Quick Sort
Python:
If
Elif
Else
Conditional
(cond (<condition1> <execute1>)
(<condition2> <execute2>)
(<conditionn> <executen>))
Absolute Value:
(define (abs x)
(cond ((< x 0) (- x))
(x)))
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 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)
Let
Variables are bounded locally
within Let body.
(let ((variable1 value1) ...)
expression1 expression2 ...)
(let ((a (* 4 4)))
(+ a a))
=> 32
(let ((+ *))
(+ 2 3))
Absolute Value:
(define (abs n)
(let ((a n) (b (-n)))
(max a b)))
=> 6
Let vs. Let*
Let evaluates the variables in parallel.
Let* evaluates the variables sequentially.
(let
((a 5)
(b (* a 10)))
b)
Python
=> reference to undefined identifier:
a
Parallel
(let* ((a 5)
assignments
(b (* a 10)))
a, b = b, a
b)
=> 50
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?
Related documents