day08

day08 - CS1 Introduction to Computation Day 8 Introduction...

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

Caltech CS 1 -- Fall 2008 CS1: Introduction to Computation Day 8: October 22, 2008 Introduction to Data Abstraction

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

View Full Document
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
Caltech CS 1 -- Fall 2008 Today • Motivate – Composite data Data abstraction • Introduce cons , car , and cdr • Data abstraction examples – Complex numbers – Complex quadratic equation

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

View Full Document
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…
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

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

View Full Document
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
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?!

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

View Full Document
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))
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 )

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

View Full Document
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)))
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)))

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

View Full Document
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)
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)))

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

View Full Document
Caltech CS 1 -- Fall 2008 Redundant operations • foo(A,B,C) = A+B*C
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 09/25/2010 for the course CS 1 taught by Professor Chang during the Spring '08 term at UMBC.

Page1 / 73

day08 - CS1 Introduction to Computation Day 8 Introduction...

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

View Full Document
Ask a homework question - tutors are online