02-interaction-post

02-interaction-post - Interaction Readings: As required...

Info iconThis preview shows pages 1–9. 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

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 Readings: As required from the Guide and Reference manuals in DrSchemes Help Desk. Without proper textbook support for this topic, it becomes even more important to attend lectures and tutorials, and to ask clarifying questions. CS 136 Fall 2009 02: Interaction 1 Printing Recall that a Scheme program is a series of definitions and expressions. Each top-level expression is reduced to a value. When a program is run in DrScheme, or in a shell window using mzscheme , these values are printed. We will now explore additional ways of printing information while a program is being evaluated. CS 136 Fall 2009 02: Interaction 2 The begin special form The begin special form evaluates all of its arguments, and produces the value of its last argument. ( begin ( + 1 2 ) ( * 3 4 ) (- 5 6 )) - 1 This is of no use in the CS 135 teaching languages. It is only useful when the expressions whose values are thrown away have some other effect. CS 136 Fall 2009 02: Interaction 3 Void The primitive function void consumes any number of arguments and produces the special value # < void > . This value is not printed when it is the result of evaluating a top-level expression. The program below has no visible effect at all. Nothing is printed. ( begin ( + 1 2 ) ( * 3 4 ) ( void )) CS 136 Fall 2009 02: Interaction 4 The display function The display function consumes a value and produces # < void > . When ( display v ) is evaluated, as a side effect , it prints the consumed value v immediately (even if this application is not a top-level expression). ( begin ( display " The answer is " ) ( display ( sqrt 2 )) ( display " \ n " )) When evaluated in DrScheme, this prints: The answer is 1.4142135623730951 > CS 136 Fall 2009 02: Interaction 5 Formatted printing ( define n 2 ) ( define s ( sqrt n )) ( printf " The square root of a is a \ n " n s ) When evaluated in DrScheme, this prints: The square root of 2 is 1.4142135623730951 > In the format string which is the first argument to printf , a (the tilde character followed by a) means display the next argument. There are a number of other formatted escape sequences that can be used (see Help Desk for details if you are curious). CS 136 Fall 2009 02: Interaction 6 This idea is used in some other functions. The format function does not print, but produces the string that printf would have printed (so it can be used in further computation). ( format " The square root of a is a " 2 ( sqrt 2 )) " The square root of 2 is 1.4142135623730951 " The error function can be provided with a format string and extra arguments to be printed. ( error my-fn " Expected a, got a " x y ) CS 136 Fall 2009 02: Interaction 7 Repetition for side effects The built-in function for-each is like map in that it consumes a function f and a list lst , and applies f to each element of lst ....
View Full Document

This note was uploaded on 10/30/2011 for the course CS 136 taught by Professor Becker during the Spring '08 term at Waterloo.

Page1 / 51

02-interaction-post - Interaction Readings: As required...

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