02-interaction-post3up

02-interaction-post3up - Interaction in Scheme An overview...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Interaction in Scheme An overview of this module Displaying output to the user Receiving input from the user Standard input Input from a file CS 136 Spring 2011 02: Interaction in Scheme 1 Displaying a value The function ( display v ) Consumes: A value v Produces: ( void ) ( void ) means nothing Not the same thing as empty Side Effect: Displays the value of v on standard output Example: ( display " Hello, world " ) ( void ) but evaluating this expression will display Hello, world on the screen CS 136 Spring 2011 02: Interaction in Scheme 2 Example: ( display " Hello, \ nworld " ) ( void ) but evaluating this expression will display: Hello, world \ n used in a string indicates a new line Represented as the character # \ newline Note that ( string-length " \ n " ) 1 ( display " \ n " ) can be abbreviated in DrRacket as ( newline ) CS 136 Spring 2011 02: Interaction in Scheme 3 begin begin : Evaluates a sequence of expressions and produces what is produced by the last expression in the sequence ( begin ( + 3 10 ) ( display " CS 136 " ) ( cons 1 ( cons 2 empty )) ( + 1 1 )) 2 Evaluating this expression has the side effect of displaying CS 136 CS 136 Spring 2011 02: Interaction in Scheme 4 Implicit begin For conveniece, we can omit begin in the following four places: The body of a function definition The body of a lambda expression The body of a local expression The answer of a cond This can be very useful when debugging (example on next slide) CS 136 Spring 2011 02: Interaction in Scheme 5 ( define ( fib x ) ( cond [( < = x 2 ) ( display " base case reached \ n " ) 1 ] [ else ( display " recursive call with x = " ) ( display x ) ( display " \ n " ) ( + ( fib (- x 1 )) ( fib (- x 2 )))])) CS 136 Spring 2011 02: Interaction in Scheme 6 Implicit local We can also omit local in the following two places: The body of a function definition The body of a lambda expression ( define ( sum-list lst ) ( define ( sum-list-acc lst0 acc ) ( cond [( empty? lst0 ) acc ] [ else ( sum-list-acc ( rest lst0 ) ( + acc ( first lst0 )))])) ( sum-list-acc lst )) CS 136 Spring 2011 02: Interaction in Scheme 7 Formatted printing ( printf ) The function ( printf s v1 v2 ... vn ) Consumes: A string s containing n instances of a and values v 1 ,...,v n Produces: ( void ) Side Effect: Displays the string s on standard output with the values v 1 ,...,v n in place of a Example: ( display " There are a cats " 10 ) ( void ) but displays There are 10 cats CS 136 Spring 2011 02: Interaction in Scheme 8 Example: ( define x 10 ) ( define y 12 ) ( display " a plus a is a " x y ( + x y )) This displays 10 plus 12 is 22 CS 136 Spring 2011 02: Interaction in Scheme 9 In the fib example, we used display in the following way: ( display " recursive call with x = " ) ( display x ) ( display " \ n " ) This can be re-written using printf : ( printf " recursive call with x = a \ n " x ) CS 136 Spring 2011...
View Full Document

Page1 / 15

02-interaction-post3up - Interaction in Scheme An overview...

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

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