;; CMPU101, Spring 2013 ;; Lecture #2 ;; Announcements: ;; - The labs are only scheduled for 75 minutes, so if you need more ;; time but have to leave, tell a coach or your prof and set an ;; appointment to finish the lab. ;; ;; - Section 51 will have lab on Tuesday and Thursday this week at ;; 4:35 to 5:50 pm. From this point on, the section 51 lab will ;; meet on Thursday at 4:35 to 5:50 pm. ; ; DEFAULT EVALUATION RULE FOR NON-QUOTED LISTS: ; ; Every non-trivial program you write will call some primitive ; function(s). The functions you write will be compound lists ; (lists within lists). ; ; Pattern to use when typing a FUNCTION CALL for default ; evaluation of pre-defined function: ; ; - type a left parenthesis ( ; ; - type a function (OPERATOR) name followed by a space ; ; - type values or expressions as ARGUMENTS (aka INPUTS) ; to the function and separate each with a space ; ; Note: the inputs must match the number & type given ; in the function contract (see above for how to look ; up the function in the Help Desk). ; ; - type a right parenthesis ) ; ; Every time Racket scans an open (left) parenthesis, it expects ; the first item to the right of the open parenthesis to be a ; function name. ; ; ; What are the primitive functions that are available for us to use ; when writing our own functions? Check out the Help menu and the ; Help Desk. ; ; ; To look up LIBRARY (BUILT-IN) FUNCTIONS: ; ; 1. If you don't know the name of the function: ; Help menu --> Help Desk --> How to Design Programs ; Languages --> Pre-Defined Functions. Look down the left ; hand side of the page for a list of all the pre-defined ; functions (aka operations), separated according to ; data type. Clicking on the link for an operator will ; bring up its description and usage. ; ; 2. If you know the function name, but are not sure about ; the number or type of arguments or the type of the ; return value: go to Help menu --> Help Desk, then ; type the function name in at the top of the screen. ; ; 3. Type the name of the function in the Definitions window, ; then click the mouse with the cursor inside the function ; name and press fn-F1 (Mac) or Ctrl-F1...a browser window ; will open to the help page that describes the function. ; This will not work if the expression is in a comment. ; ; 4. Click on the function name in the on-line HtDP textbooks. ; ; ; Function input and output: ; ; - Functions in Racket can consume any number of input ; arguments, including an arbitrary number or none. ; ; - Functions in Racket produce only ONE output. ; ; ; FUNCTION CONTRACTS (in on-line help) ; ; CONTRACT: abbreviated prelude to the function entry ; ; Gives number and type of arguments function CONSUMES ; and type of output it PRODUCES. ; ; ------------------------------------------------------- ; EXAMPLES: ; ; If you search for the abs function, you will see ; that this function consumes only one argument, a real, ; and returns a real: ; ; (abs x) -> real ;; 1 parameter ; x : real ; ; Determines the absolute value of a real number. ; ; > (abs -12) ; 12 ; ; If you search for the + function, you will see ; ; (+ x ...) -> number ;; 0 or more parameters ; x : number ; ; Adds all given numbers. ; ; > (+ 2/3 1/16) ; 35/48 ; > (+ 3 2 5 8) ; 18 ; > (+ 1) ; 1 ; > (+) ; 0 ; ; If you seach for the - function, you will see ; ; (- x y ...) -> number ;; 1 or more parameters ; x : number ; y : number ; ; Subtracts the second (and following) number(s) ; from the amount on the left. Negates the number ; if only 1 arg. ; ; > (- 5) ; -5 ; > (- 5 3) ; 2 ; > (- 5 3 1) ; 1 ; ; ; For functions like - and /, you need to keep in mind the ; order of the arguments when calling the function. I.e., ; ; (- (+ 2 3) (* 2 4)) ==> 5 - 8 ==> -3 ; ; ; Contracts should be as specific as possible about type of ; inputs a function consumes. E.g., whole numbers greater than or ; equal to 0, integers, or any real number. ; ; ; ; CALLING PRIMITIVE FUNCTIONS (OPERATORS): PREFIX NOTATION ; ; Racket function invocations are written in "prefix notation". ; ; Infix notation: operator in-between operands, (1 + 1). ; ; Prefix notation: operator written before operands, (+ 1 1). ; ; ; The advantages of prefix notation: ; ; 1. Allows functions like + and < to take any number of ; parameters with only one function invocation. ; ; 2. Prefix notation doesn't need precedence rules. ; ; ; How to convert Infix --> Prefix notation: ; ; 1. Convert all mathematical shorthand to infix expressions: ; (e.g., 3^2 = (3 * 3), 6y = (6 * y), 4/9 = (/ 4 9)) ; ; 2. Fully parenthesize the expression with two operands for each ; operator and using PEMDAS rules. ; ; 3. Move each operator to position immediately to right of closest ; enclosing left parenthesis, leaving everything else in the ; same order it was in before. In particular, make sure all ; the operands are in the same order in the prefix and infix ; expressions. ; ; EX: 2x + 4/5y - 32 ; ; Step 1) 2 * x + 4 / 5 * y - 32 ; ; Step 2) (((2 * x) + ((4 / 5) * y)) - 32) ; ; Step 3) (- (+ (* 2 x) (* (/ 4 5) y)) 32) ; ; ; NESTING FUNCTIONS ; ; Function calls can be nested to any level as long as some ; expression can be directly evaluated. An expression can be ; directly evaluated when all arguments to the function being ; called are primitive Racket entities. Evaluation is done ; from left to right when there is more than one expression ; that can be directly evaluated. ; ; For example, in the following expression, what is the first ; expression to be evaluated? What is the second, third and so ; on? ; (* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2)) ; ; Using the Stepper in one of the HtDP languages below the ; Advanced Student level allows you to "step through" a function ; call and see what is evaluated and in what order. ; ;(* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2)) ;;Example evaluations: ;;--->A function application to sum the numbers between 1 and 5: ;(+ 1 (+ 2 (+ 3 (+ 4 5)))) ;;--->Another way to calculate the sum from 1 to 5: ;(+ 1 2 3 4 5) ;; Ask yourself: How am I supposed to know ;; the + operator can take so many inputs? ;; Look it up in the Help Desk. ;;--->The square root of 16: ;(sqrt 16) ;;--->The remainder of 12 divided by 5: ;(remainder 12 5) ;; CLASS EXERCISE: Write a prefix expression to represent the ;; standard arithmetic expression 7x - (3+x)/(y+2). Expand and ;; parenthesize the expression and then convert it to prefix form. ; ; DISPLAY, PRINTF, AND NEWLINE FUNCTIONS: (note: each of these ; is for screen display only...return value is void) ; ; CONTRACT OF DISPLAY: ; (display x) --> void ; x : any ; Prints the argument to stdout (without quotes on ; symbols and strings, etc.). Can embed newline in ; string argument to skip a line with \n ; > (display 10) ; 10 ; > (display "hello") ; hello ; > (display "hello \nworld") ; hello ; world ; > (display 'hello) ; hello ; ; CONTRACT OF PRINTF: ; (printf f x ...) --> void ; f : string ; x : any ; Formats the rest of the arguments according to the first ; argument and print it. Embedded values are "~a" or "~A" ; and newlines are "~%". ; > (printf "Hello, ~a, how are you?~%" 'Helen) ; Hello, Helen, how are you? ; > (printf "x = ~A, y = ~a, z = ~A~%" 5 7 3) ; x = 5, y = 7, z = 3 ; > ; ; the number of arguments after the string argument ; ; must match the number of ~a characters in the ; ; first argument ; ; CONTRACT OF NEWLINE: ; (newline) --> void ; Prints a newline. ; ; These functions are covered on pages ; ; ; NEXT LECTURE: ; ; Defining names in the global environment: the DEFINE ; special form. ; ; Creating new, unnamed functions: the LAMBDA special form ;