day08

day08 - CS1: Introduction to Computation Day 8: October 22,...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
Caltech CS 1 -- Fall 2008 CS1: Introduction to Computation Day 8: October 22, 2008 Introduction to Data Abstraction
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 Previously • Functions • Evaluation model • Recursion • First-class functions • Types: – numbers, booleans, functions • A sufficient set to compute anything – but not necessarily a convenient one
Background image of page 2
Caltech CS 1 -- Fall 2008 Today • Motivate – Composite data Data abstraction • Introduce cons , car , and cdr • Data abstraction examples – Complex numbers – Complex quadratic equation
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 Complex Numbers • Consider: • x = a + b i • How do we represent and use them? • For motivation, let’s just use what we know…
Background image of page 4
Caltech CS 1 -- Fall 2008 Complex Numbers • Need both numbers ( a=real ) and ( b=imaginary ) for each complex number • Define some operations… – add – subtract – negate – multiply – magnitude
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 Complex Addition ( ar + ai × i ) + ( br + bi × i ) = ( ar + br ) + ( ai + bi ) × i • Add real components to get real component of sum • Add imaginary components to get imaginary component of sum
Background image of page 6
Caltech CS 1 -- Fall 2008 Defining complex-add (define ( complex-add ar ai br bi) …) • What does it return? – Can only return a number – But we need two results: real part of result imaginary part of result – So… have to write two separate functions?!
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 complex-add • Break into two functions: 1) Return real part (define ( complex-add-real ar ai br bi) (+ ar br)) 2) Return imaginary part (define ( complex-add-imag ar ai br bi) (+ ai bi))
Background image of page 8
Caltech CS 1 -- Fall 2008 Complex Multiplication ( ar + ai × i ) × ( br + bi × i ) = ( ar × br + ai × bi × i 2 ) + ( ar × bi × i + ai × br × i ) = ( ar × br - ai × bi ) + ( ar × bi + ai × br ) × i Real part of result: ( ar × br - ai × bi ) Imaginary part of result: ( ar × bi + ai × br )
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 Defining complex-multiply Real part of result: ( ar × br - ai × bi ) (define ( complex-multiply-real ar ai br bi) (- (* ar br) (* ai bi))) Imaginary part of result: ( ar × bi + ai × br ) (define ( complex-multiply-imag ar ai br bi) (+ (* ar bi) (* ai br)))
Background image of page 10
Caltech CS 1 -- Fall 2008 Consider • foo(A,B,C) = A+B*C (where A, B, C complex) • (define ( foo-real ar ai br bi cr ci) (complex-add-real ar ai (complex-multiply-real br bi cr ci) (complex-multiply-imag br bi cr ci))) • (define ( foo-imag ar ai br bi cr ci) (complex-add-imag ar ai (complex-multiply-real br bi cr ci) (complex-multiply-imag br bi cr ci)))
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 Yuck. • Have to handle every component separately – Makes it hard to understand – Exposes lots of underlying complexity • Have to return each component separately • Have to write separate operations (perform lots of operations redundantly)
Background image of page 12
Caltech CS 1 -- Fall 2008 Redundant operations • foo(A,B,C) = A+B*C • (define (foo-real ar ai br bi cr ci) (complex-add-real ar ai (complex-multiply-real br bi cr ci) (complex-multiply-imag br bi cr ci))) • (define (foo-imag ar ai br bi cr ci) (complex-add-imag ar ai (complex-multiply-real br bi cr ci) (complex-multiply-imag br bi cr ci)))
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caltech CS 1 -- Fall 2008 Redundant operations • foo(A,B,C) = A+B*C
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 73

day08 - CS1: Introduction to Computation Day 8: October 22,...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online