Anda di halaman 1dari 33

Computer Science 136: Elementary Algorithm Design and Data Abstraction Winter 2012

David R. Cheriton School of Computer Science

Lecture 1 Welcome to CS136


CS 136 builds on CS 135. Introduction of C (along with Scheme) Details of effective use of programming languages in "real-world" environments Focus on design, analysis and implementation of fundamental algorithms and data structures Provide tools and concepts necessary to solve computational problems in a robust, efcient and veriable manner

CS 136 Winter 2012

01 Introduction to CS136

1/1

Winter 2012 Instructors, Tutors, IAs

Instructors: Mark Giesbrecht (DC 1310)

mwg@uwaterloo.ca
M. Jason Hinek (DC 3322)

mjhinek@uwaterloo.ca
Mark Petrick (DC 3529)

mdtpetri@uwaterloo.ca
Olga Zorin (DC 3119)

ozorin@cs.uwaterloo.ca
Course email: cs136@student.cs.uwaterloo.ca

CS 136 Winter 2012

01 Introduction to CS136

2/1

Tutors: Joe Istead jwistead@yahoo.ca Gregory Ling Kit Chow glkchow@uwaterloo.ca Rui Zhao rui.zhao@uwaterloo.ca Erik Karl Traikov ektraiko@uwaterloo.ca Huma Zafar hzafar@uwaterloo.ca Instructional Assistants (IAs): David Ryan Loker dloker@uwaterloo.ca Caelyn McAulay cmcaulay@uwaterloo.ca Lesley Ann Northam lanortha@uwaterloo.ca Richard Jason Peasgood jpeasgo@uwaterloo.ca Please send email to cs136@student.uwaterloo.ca for the fastest response.

CS 136 Winter 2012

01 Introduction to CS136

3/1

Welcome to CS 136 (Winter 2012)

Web page: Your main information source:

http://www.student.cs.uwaterloo.ca/~cs136/
Discussion: Your main discussion forum Piazza

https://piazza.com/
Tutorials: See course website Reading week: February 2024 Midterm: Monday, 27 Feb 7:00pm8:50pm

CS 136 Winter 2012

01 Introduction to CS136

4/1

Materials, Marking, Software

Textbooks:

How to Design Programs (HtDP) by Felleisen, Flatt, Findler, Krishnamurthi

http://www.htdp.org
C Programming: A Modern Approach (CP:AMA) by K. N. King. Presentation handouts: Available on web page, one week before lecture

CS 136 Winter 2012

01 Introduction to CS136

5/1

Marking Scheme: 30% assignments 30% midterm 40% nal exam Your assignment average and weighted exam average must both be greater than 50% or you will not pass! A1 is due in one week! (But well do some together.) Software: DrRacket (and maybe the command line and RunC) will be used for Racket. RunC will be used for C. VirtualBox (go to a tutorial, see website) is by far the easiest.

CS 136 Winter 2012

01 Introduction to CS136

6/1

The General Theme

Write programs that do stuff in the real world

CS 136 Winter 2012

01 Introduction to CS136

7/1

The General Theme

Write programs that do stuff in the real world Interact with users, devices, other programs
Input from the outside world Output to the outside world (...more input, more output, input, output...)

CS 136 Winter 2012

01 Introduction to CS136

7/1

The General Theme

Write programs that do stuff in the real world Interact with users, devices, other programs
Input from the outside world Output to the outside world (...more input, more output, input, output...)

Abstraction is how we will interact with other code

CS 136 Winter 2012

01 Introduction to CS136

7/1

The General Theme

Write programs that do stuff in the real world Interact with users, devices, other programs
Input from the outside world Output to the outside world (...more input, more output, input, output...)

Abstraction is how we will interact with other code Store and Manipulate data and state

CS 136 Winter 2012

01 Introduction to CS136

7/1

Specic Topics
In CS135, wrote functions
Data included as literals; exact same output every time

CS 136 Winter 2012

01 Introduction to CS136

8/1

Specic Topics
In CS135, wrote functions
Data included as literals; exact same output every time

In CS136, we will write programs. Behaviour now depends on interaction with other data/programs/users.

CS 136 Winter 2012

01 Introduction to CS136

8/1

Specic Topics
In CS135, wrote functions
Data included as literals; exact same output every time

In CS136, we will write programs. Behaviour now depends on interaction with other data/programs/users. I/O For interaction with other data/programs/users Modularization a.k.a. abstraction, a.k.a. hiding detail.
Somebodys code maybe does something complicated, but using it to do that thing is simple

Efciency Are our programs fast enough? Could we do better? Mutation For memory storing data or state

CS 136 Winter 2012

01 Introduction to CS136

8/1

Nuts and Bolts


Two languages: Racket/Scheme and C
You now can access the full language:

http://docs.racket-lang.org/reference/ http://docs.racket-lang.org/guide/

CS 136 Winter 2012

01 Introduction to CS136

9/1

Nuts and Bolts


Two languages: Racket/Scheme and C
You now can access the full language:

http://docs.racket-lang.org/reference/ http://docs.racket-lang.org/guide/

Can offer radically different approaches to programming


Racket (as we have seen it so far) is functional
about dening behaviour of functions ability to construct functions, and then execute them (lambda) variables do not change once dened

CS 136 Winter 2012

01 Introduction to CS136

9/1

Nuts and Bolts


Two languages: Racket/Scheme and C
You now can access the full language:

http://docs.racket-lang.org/reference/ http://docs.racket-lang.org/guide/

Can offer radically different approaches to programming


Racket (as we have seen it so far) is functional
about dening behaviour of functions ability to construct functions, and then execute them (lambda) variables do not change once dened

C is imperative
repetition emphasized over recursion ability to manipulating memory directly

Racket is actually multi-paradigm as we will see.

CS 136 Winter 2012

01 Introduction to CS136

9/1

Nuts and Bolts


Two languages: Racket/Scheme and C
You now can access the full language:

http://docs.racket-lang.org/reference/ http://docs.racket-lang.org/guide/

Can offer radically different approaches to programming


Racket (as we have seen it so far) is functional
about dening behaviour of functions ability to construct functions, and then execute them (lambda) variables do not change once dened

C is imperative
repetition emphasized over recursion ability to manipulating memory directly

Racket is actually multi-paradigm as we will see.

What you will learn can be transferred to any language Abstraction.


CS 136 Winter 2012 01 Introduction to CS136 9/1

More Nuts and More Bolts: Demo Time!

Assignment 1, Problem 1 (A1P1)


VirtualBox Gedit and runC Marmoset for Submission

Sums of numbers, modularity, efciency


Build a Racket module that provides a service to a client program Think about efciency of a program

Reading:

http://docs.racket-lang.org/guide/module-basics.html

CS 136 Winter 2012

01 Introduction to CS136

10/1

Beyond the teaching languages

In CS 136 we will use the full Racket language Put #lang racket as the very rst line of your program. This wraps your entire program in a module, which is a way of packaging code. More precisely, this wraps your code in a module as follows:
( module filename scheme ... your code here ... )

Your code is put into a module of the same name as that of the le containing the code (removing the sufx .rkt).

CS 136 Winter 2012

01 Introduction to CS136

11/1

Modules
Modules allow you to control what goes in and what comes out.
module

(provide my-fn1 ...)

bindings
(require "myle.rkt")

#lang racket makes the full Racket language available inside the module.

Well talk about provide and require next class.

CS 136 Winter 2012

01 Introduction to CS136

12/1

The full Racket language

Racket is a Scheme/Lisp variant, derived most recently from PLT Scheme. Racket is an industrial strength programming language.

CS 136 Winter 2012

01 Introduction to CS136

13/1

The full Racket language

Racket is a Scheme/Lisp variant, derived most recently from PLT Scheme. Racket is an industrial strength programming language. There are some important differences
check-expect and the Stepper are gone true and false are represented by actual values #t and #f

No safety net any more!

CS 136 Winter 2012

01 Introduction to CS136

13/1

The full Racket language (2)

The teaching languages enforce the restriction that the second argument to cons must be a list. R5RS and Racket do not.

CS 136 Winter 2012

01 Introduction to CS136

14/1

The full Racket language (2)

The teaching languages enforce the restriction that the second argument to cons must be a list. R5RS and Racket do not. The value of the expression (cons 1 2) is printed in the Interactions window as '(1 . 2)
We will not use this feature in CS 136. If you see dotted notation in your lists, you are probably doing something wrong. Check the arguments of the cons expressions in your code.

CS 136 Winter 2012

01 Introduction to CS136

14/1

a1p1 - candidate solution

;; a1p1.rkt #lang racket ;; Output the result of the sum of the numbers ;; from 1 to 10 (+ 1 2 3 4 5 6 7 8 9 10)

CS 136 Winter 2012

01 Introduction to CS136

15/1

Sum from 1 to N - Function

#lang racket ;; Function that sums the numbers from 1 to n ;; Output the result of the sum of the numbers ;; from 1 to n ( define ( sum-1-to-n n) (cond [ (= n 0) 0] [ else (+ n ( sum-1-to-n (sub1 n))) ] ))

CS 136 Winter 2012

01 Introduction to CS136

16/1

Sum from 1 to N - Functions - A little safer/friendlier?


#lang racket ;; Function that sums the numbers from 1 to n ;; PLUS a little error-checking ;; Check for crazy inputs ( define ( sum-1-to-n n) (cond [( not ( integer? n)) ( error "You can 't sum up to a non-integer! ")] [(< n 0) 0] [else ( sum-1-to-n-helper n)])) ;; Output the result of the sum of the numbers ;; from 1 to n ( define ( sum-1-to-n-helper n) (cond [ (= n 0) 0] [ else (+ n ( sum-1-to-n-helper (sub1 n))) ]))
CS 136 Winter 2012 01 Introduction to CS136 17/1

Sum from 1 to N - Module


#lang racket ;; This file is called "sum-1-to-n.rkt" ;; Function that sums the numbers from 1 to n ;; PLUS a little error-checking ( provide sum-1-to-n ) ;; Check for crazy inputs ( define ( sum-1-to-n n) (cond [( not ( integer? n)) ( error "You can 't sum up to a non-integer! ")] [(< n 0) 0] [else ( sum-1-to-n-helper n)])) ;; Output the result of the sum of the numbers ;; from 1 to n ( define ( sum-1-to-n-helper n) (cond [ (= n 0) 0] [ else (+ n ( sum-1-to-n-helper (sub1 n))) ]))
CS 136 Winter 2012 01 Introduction to CS136 18/1

Sum from 1 to N - Module and Client


#lang racket ;; This file is called "sum-1-to-n.rkt" ;; Function that sums the numbers from 1 to n ;; PLUS a little error-checking ( provide sum-1-to-n ) ;; ...bla bla implementation bla bla who cares...

#lang racket ;; This file is called "testsums.rkt" ( require " sum-1-to-n .rkt") ( sum-1-to-n 'foo) ( sum-1-to-n " eleventy ") (time ( sum-1-to-n 10)) (time ( sum-1-to-n 5000000)) (time ( sum-1-to-n 10000000)) (time ( sum-1-to-n 20000000)) (time ( sum-1-to-n 80000000))
CS 136 Winter 2012 01 Introduction to CS136 19/1

Sum from 1 to N - A Faster Implementation


#lang racket ;; This file is called "sum-1-to-n-acc.rkt" ;; Function that sums the numbers from 1 to n ;; PLUS a little error-checking ( provide sum-1-to-n ) ;; Check for crazy inputs ( define ( sum-1-to-n n) (cond [( not ( integer? n)) ( error "You can 't sum up to a non-integer! ")] [(< n 0) 0] [else ( sum-1-to-n-helper n)])) ;; Output the result of the sum of the numbers ;; from 1 to n ( define ( sum-1-to-n-helper n acc) (cond [ (= n 0) acc ] [ else ( sum-1-to-n-helper (sub1 n) (+ n acc ))]))
CS 136 Winter 2012 01 Introduction to CS136 20/1

Sum from 1 to N - A Faster Implementation

We should be a little careful accumulative recursion is not always faster than structural recursion. We havent even dened what it means for one implementation to be faster than another. Accumulative recursion does save space here; the recursive call is now tail-recursive. Well talk about algorithm and implementation efciency (for time and space) shortly.

CS 136 Winter 2012

01 Introduction to CS136

21/1

Sum from 1 to N - An Even Faster Implementation


#lang racket ;; This file is called "sum-1-to-n-form.rkt" ;; Function that sums the numbers from 1 to n ;; PLUS a little error-checking ( provide sum-1-to-n ) ;; Check for crazy inputs ( define ( sum-1-to-n n) (cond [(< n 0) 0] [( integer? n) ( sum-1-to-n-helper n)] [else ( error "You can 't sum to a non-integer! ")])) ;; Output the result of the sum of the numbers ;; from 1 to n ( define ( sum-1-to-n-helper n) (/ (* n (add1 n)) 2))

CS 136 Winter 2012

01 Introduction to CS136

22/1

Summary

Abstraction through modularity


Lets you share your code, use that of others. Makes use of the module implementation-independent. Improvements to the modules implementation can be made without requiring changes to the way the module is used by the client.
Here we made the implementation a bit more efcient.

CS 136 Winter 2012

01 Introduction to CS136

23/1

Anda mungkin juga menyukai