{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

02-interaction-post3up

02-interaction-post3up - Interaction Readings As required...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Interaction Readings: As required from the Guide and Reference manuals in DrScheme’s 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
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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 . But it applies f only for its side effects. The values produced by f are discarded (no list is created).
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern