; CS 111 - Week 2 Lab - 11:00 - 2016-09-02 (require 2htdp/image) (require 2htdp/universe) ; our first Racket function from previously: ; math: f(x) = 3x ; in Racket, you can say this: (define (f x) (* 3 x) ) ; in Math, once you have f(x) = 3x, ; you can say: f(10) = 30 ; in Racket, once you define function f as above, ; you can now use/call the new function in compound ; expressions: (f 10) (check-expect (f 10) 30) ; wow, f is not a description name, though -- ; let's make a better version, called triple ; again in Math terms: ; triple(x) = 3x ; * what is x? x is a variable, standing for ; a value I'll give later ; * in a computer function, we're going to call this ; a PARAMETER variable (or parameter for short) ; in Racket, your new function can have as many ; parameter variables as you would like... ; (these are the information the function needs ; from the USER to do its task) ; when you write/define a function, you use parameter ; variable to stand in for future argument expressions ; here is the general syntax for defining a function ; in BSL Racket: ; ; (define (desired-new-function-name param param ...) ; expression ; ) ; ; the part: ; (define (desired-new-function-name param param ...) ; ...is called the FUNCTION HEADER ; the part: ; expression ; ) ; ...is called the FUNCTION BODY ; let's give triple a MORE DESCRIPTIVE parameter variable ; than x: (define (triple num) (* 3 num) ) (triple 5) (check-expect (triple 5) 15) (* (triple 8) 10) ; now we'll start using our first version of the ; DESIGN RECIPE to write some more functions: ; first: THINK about what you want; ; what TYPES are data are involved? ; (and how much data is involved?) ; (data analysis, & data definition maybe-but-not-yet) ; OK, I decide I have need of many purple stars of ; different sizes; ; that can use number for the size, image for the ; result; ; second: write a SIGNATURE comment for the desired ; function ; (deciding/writing the name of the new function. ; and the number and type of its parameters ; signature: purple-star: number -> image ; third: write a PURPOSE STATEMENT comment for the ; desired function, DESCRIBING what it expects ; and DESCRIBING what it returns ; (and DESCRIBING its side-effects, if any) ; purpose: expects the desire distance in pixels ; between points of a 5-pointed star, ; and returns a solid purple star image ; of that size ; fourth: write the function header, giving a ; DESCRIPTIVELY-NAMED parameter variable ; for each of its parameters, ; (and giving an empty-template body of ...) for now) ;(define (purple-star size) ; ... ;) ; fifth: develop and write SPECIFIC TESTS/testing examples ; FOR my new function ; course style: always write at least 2 tests, and ; more based on the circumstances (check-expect (purple-star 15) (star 15 "solid" "purple")) (check-expect (purple-star 30) (star 30 "solid" "purple")) ; sixth (yes AFTER YOU WRITE THE TESTS) THEN complete ; the body of the function, ; REPLACING the ... with its useful body expression ; (which had better use the parameter variables ; somehow...) ; yes, it IS ok -- this is an exception -- to ; write check-expects for a function BEFORE you ; define the function...! (define (purple-star size) (star size "solid" "purple") ) ; let's put an example call where we can SEE the result (purple-star 125) ; another example... ; I want a function that will exclaim an exclamation ; once and "repeat" strongly a statement twice ; gee, this involves strings ; signature: doubt-it: string string -> string ; purpose: expects an exclamation and a statement ; to doubt, and returns a new string with ; that exclamation followed by an ! and a blank ; the statement followed by two question marks ; and a blank and the statement again followed by ; two question marks ;(define (doubt-it exclam stmt-to-doubt) ; ... ;) (check-expect (doubt-it "oh" "bigfoot is green") "oh! bigfoot is green?? bigfoot is green??") (check-expect (doubt-it "what" "my team lost") "what! my team lost?? my team lost??") (define (doubt-it exclam stmt-to-doubt) (string-append exclam "! " stmt-to-doubt "?? " stmt-to-doubt "??") ) (doubt-it "no" "the chocolate is gone")