Lecture 2 module

Lecture 2 module - Interaction The values of evaluated...

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

View Full Document Right Arrow Icon
Interaction The values of evaluated expressions are printed into the Interactions window when a program is run in DrScheme. They are printed into the shell window by mzscheme . We can also print during an evaluation, not just at the end. The display function consumes any Scheme value, prints it, and produces the special “invisible” value # < void > . The begin special form consumes several values and produces the last one. This lets us sequence printing. CS 136 Winter 2009 02: Interaction 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
( begin ( display " The answer is " ) ( display ( sqrt 2 )) ( newline )) results in The answer is 1.4142135623730951 > ( newline ) is shorthand for ( display " \ n " ) . CS 136 Winter 2009 02: Interaction 2
Background image of page 2
Rewriting begin expressions Informally, we evaluate and throw away the resulting value for all but the last expression of a begin . This suggests the following rules: ( begin val exp1 . . . expn ) ( begin exp1 . . . expn ) ( begin exp ) exp HtDP also uses ( void ) instead of # < void > . We need a rewrite rule which simply erases such values. The primitive function void consumes any number of arguments and produces # < void > . CS 136 Winter 2009 02: Interaction 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
Implicit begins We can use multiple expressions without begin in the following places: ˆ The body of a function definition; ˆ The body of a lambda ; ˆ The body of a local ; ˆ The answer of a cond . Definitions are permitted at the beginning of the body of a function definition or lambda (“implicit local ”). CS 136 Winter 2009 02: Interaction 4
Background image of page 4
Formatted printing ( define n 2 ) ( define s ( sqrt n )) ( printf " The square root of ˜a is ˜a \ n " n s ) results in The square root of 2 is 1.4142135623730951 > In the format string which is the first argument to printf , ˜a means “display the next argument”. CS 136 Winter 2009 02: Interaction 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
This idea is used in some other functions. ( format " The square root of ˜a is ˜a " 2 ( sqrt 2 )) " The square root of 2 is 1.4142135623730951 " ( error my-fn " Expected ˜a, got ˜a " x y ) There are a number of other “formatted escape” sequences that can be used. CS 136 Winter 2009 02: Interaction 6
Background image of page 6
A first C program #include <stdio.h> int main( void ) { printf("To C, or not to C: "); printf("that is the question.\n"); return 0; } CS 136 Winter 2009 02: Interaction 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
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 . But it applies f only for its side effects . The values produced by f are discarded. for-each produces # < void > . Example: printing the values in list my-list without the parentheses. ( for-each ( lambda ( x ) ( printf " ˜a " x )) my-list ) CS 136 Winter 2009 02: Interaction 8
Background image of page 8
for-each is built-in, but we can write it ourselves, just like map . ;; for-each: (X
Background image of page 9

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

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

Page1 / 33

Lecture 2 module - Interaction The values of evaluated...

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

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