02-mutation

02-mutation - Computer Science 136 Elementary Algorithm...

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

View Full Document Right Arrow Icon
Computer Science 136: David R. Cheriton School of Computer Science Elementary Algorithm Design and Data Abstraction Winter 2012 Lecture 2 – I/O and a bit of mutation Side-effects, Input/Output Formatted output Memory through mutation More Encapsulation Games! CS 136 – Winter 2012 02 – Mutation 1/30
Background image of page 1

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

View Full Document Right Arrow Icon
Recap of Last Time Nuts and Bolts: VirtualBox, runC, Marmoset Adding numbers 1 to n Modularization Efficiency Assignment 1 in progress CS 136 – Winter 2012 02 – Mutation 2/30
Background image of page 2
Questions from Last Time What is a module? In Racket, a module is: A file, e.g. "mymodule.rkt" , with code in it that can be accessed from another .rkt file (the client , which might itself be a module) by using the command ( require "mymodule.rkt") What makes a .rkt file a module? #lang racket at the beginning (but there are other ways. ..) So every .rkt file you write this term is in fact a module. CS 136 – Winter 2012 02 – Mutation 3/30
Background image of page 3

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

View Full Document Right Arrow Icon
What does ( require "mymodule.rkt") do? Accesses file "mymodule.rkt" and evaluates everything in it, as though you were “running” the file in Dr. Racket Triggers any side-effects , like printing results - e.g. a1p1 Only definitions of functions listed in ( provide ...) will be accessible within the client program/module. ( provide ...) only specifies which functions accessible to other files . Within the module, it’s business as usual. You can ( require "module.rkt") more than once, but only the first time does it have an effect. See the Racket Reference for Modules (§1.1.10) CS 136 – Winter 2012 02 – Mutation 4/30
Background image of page 4
The begin special form ( begin ( sqrt 2) ( sqrt 3) ( sqrt 4)) Produces 2 > Syntax: ( begin < expr1> < expr2> ... < exprn> ) Each < expr_> is evaluated, in order left-to-right Result of ( begin ...) is result of last expression This doesn’t seem terribly useful. .. CS 136 – Winter 2012 02 – Mutation 5/30
Background image of page 5

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

View Full Document Right Arrow Icon
Basic Printing ( begin ( display "The answer is ") ( display ( sqrt 2)) ( display "\n")) results in The answer is 1.4142135623730951 > Expressions evaluated are printed in the Interactions window. Can print during an evaluation, not just at the end. The display function consumes any Racket value, prints it, and produces the value “ # < void> ”. # < void> is special constant that doesn’t print as a result of a top-level expression. " \ n" is shorthand for generating an “end-of-line” in the output. CS 136 – Winter 2012 02 – Mutation 6/30
Background image of page 6
Implicit begin You can use multiple expressions without begin in the following: The body of a function definition; The body of a lambda The body of a local The answer of a cond ( define ( dumb-question ) ( display "The answer is ") ( display ( sqrt 2)) ( display "\n")) CS 136 – Winter 2012 02 – Mutation 7/30
Background image of page 7

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

View Full Document Right Arrow Icon
Implicit local Definitions are permitted at the beginning of the body of a function definition or lambda Recall from CS135 your code to use Heron’s formula to compute
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 30

02-mutation - Computer Science 136 Elementary Algorithm...

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

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