; CS 111 - Week 2 Lab - 1:00 - 2016-09-02 (require 2htdp/image) (require 2htdp/universe) ; Math: ; f(x) = 3x ; f(6) = 3*6 = 18 ; in BSL Racket: (define (f x) (* 3 x) ) (f 6) (check-expect (f 6) 18) ; gee, f is not a descriptive name -- ; how about triple instead? ; and gee, x is not descriptive, either; ; how about num? (define (triple num) (* 3 num) ) (triple 5) (check-expect (triple 10) 30) ; in Math, in f(x), we say that x is a variable; ; that x stands for a value to be given later ; in computer programs, ; (define (triple num) ; that num is a PARAMETER VARIABLE, ; which stands for an ARGUMENT EXPRESSION to be given ; later ; and, let's now summarize the basic function definition ; syntax: ; ; (define (desired-new-funct-name param param ...) ; expression ; ) ; ; NOTE that first part -- ; (define (desired-new-funct-name param param ...) ; is called the function's HEADER, ; and the rest, ; expression ; ) ; is called the function's BODY ; we will be writing functions using a certain ; approach, called a DESIGN RECIPE ; here's our FIRST design recipe version: ; first, think about what you want to do, ; and what type/types of data are involved; ; (later on, you might decide you need to ; define data type(s) for what you want to do) ; this is called data analysis and data definition ; say I am going to need solid purple stars in ; in different sizes; ; a size can be of type number, ; the resulting star can be of type image, ; I think I'm good to go on; ; second: write a SIGNATURE COMMENT for the function ; you want to write ; (decide on a descriptive function name, ; how many parameters you need, and their types, ; and the type the function will return) ; signature: purple-star: number -> image ; third: write a PURPOSE STATEMENT comment for the ; new function-to-be, DESCRIBING each parameter expected ; and DESCRIBING what is returned as a result ; (and, if applicable, DESCRIBING any side-effects) ; purpose: expects the desired distance (in pixels) ; between the points of a 5-pointed star, ; and returns a solid purple star of that size ; fourth: write a function HEADER for the ; new function, ; giving a DESCRIPTIVE name to each parameter, ; and for NOW using a template-body of: ; ... ; ) ;(define (purple-star dist) ; ... ;) ; fifth, develop and write SPECIFIC tests/example expressions ; for the new function -- ; at least two (and sometimes more are needed based on ; the situation); ; when you can, write these as check-expects ; (or other check- expressions) ; (note: this is an exception, you CAN write check-expect ; expressions BEFORE the function they are checking!) (check-expect (purple-star 12) (star 12 "solid" "purple")) (check-expect (purple-star 25) (star 25 "solid" "purple")) ; sixth, NOW try to develop/write the function's body, ; making sure to use the parameter variables ; appropriately, and replace the ... with the result (define (purple-star dist) (star dist "solid" "purple") ) (purple-star 50) ; I decide a want to be able to react "noticeably" ; to a given statement, including a given exclamation; ; gee these are all strings ; signature: doubt-it: string string -> string ; purpose: expects an exclamation and a statement to doubt, ; and returns a new string that contains that ; exclamation followed by a ! and a blank ; then the statement to doubt followed by ?? and a blank ; followed by the statement to doubt again followed by ?? ;(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 "oh" "bigfoot is green")