; CS 111 Week 2 Lecture 2 - 2016-09-01

;========
; making the functions and other items in the
;    2htdp/image and 2tdp/universe teachpacks
;    available for use in the rest of this
;    file

(require 2htdp/image)
(require 2htdp/universe)

;========
; another 2htdp/image function:

;--------
; signature: star: number string color -> image
; purpose: expects the desired length in pixels
;    between the points of a 5-pointed star,
;    "solid" or "outline", and a color,
;    and returns a star image with those
;    specifications

(star 75 "outline" "magenta")

;--------
; and some operations in 2htdp/image expect
;    images and return new images:

;--------
; signature: overlay: image image ... -> image
; purpose: expects any number of images, and returns
;    a new image that is the first image on top of
;    the second image on top of the ..., all lined
;    up (probably) by their centers

(overlay (circle 15 "solid" "red")
         (rectangle 40 60 "outline" "blue")
         (regular-polygon 70 5 "solid" "green"))

;--------
; signature: beside: image image ... -> image
; purpose: expects any number of images, and
;    returns an image that is the first image
;    to the left of the second image to the
;    left of the third image and so on lined up
;    by their centers

(beside (circle 15 "solid" "red")
        (rectangle 40 60 "outline" "blue")
        (regular-polygon 70 5 "solid" "green"))

;--------
; signature: above: image image ... -> image
; purpose: expects any number of images, and
;    returns an image that is the first image
;    above the second image which is
;    above the third image and so on lined up
;    by their centers

;--------
; signature: text: string number color -> image
; purpose: expects a string you want to get an
;    image version of, a desired font-size (in pixels),
;    and a color, and it returns an image of
;    the given string in that font-size and color

(above (star 75 "outline" "magenta")
       (text "WOW!" 16 "blue"))

(overlay (star 75 "outline" "magenta")
       (text "WOW!" 16 "blue"))

;--------
; you can also save an image into a .png file

;--------
; signature: save-image: image string -> boolean
; purpose: expects an image to be saved and a
;    string representing the name of the file to
;    save it to, and it tries to have the side-effect
;    of creating a file with that name containing
;    a version of that image, and returns #true if
;    successful (and I think #false if not)

(save-image (overlay (star 75 "outline" "magenta")
                     (text "WOW!" 16 "blue"))
            "wow-star.png")

(beside (overlay (star 75 "outline" "magenta")
                 (text "WOW!" 16 "blue"))
        (overlay (star 75 "outline" "magenta")
                 (text "WOW!" 16 "blue"))
        (overlay (star 75 "outline" "magenta")
                 (text "WOW!" 16 "blue"))
        (overlay (star 75 "outline" "magenta")
                 (text "WOW!" 16 "blue"))
        (overlay (star 75 "outline" "magenta")
                 (text "WOW!" 16 "blue")))

;========
; gee, wouldn't it be nice if there were a
;    shorter way to reuse an expression one
;    is using more than once?

;--------
; a name that is determined by a programmer
;    writing a program is called an IDENTIFIER

;--------
; there are syntax rules for identifiers:
;    just about any set of non-blank
;    characters that are not also following
;    the syntax rules for a built-in type
;    and do not use:
;    ( ) [ ] { } " , ' ` # | \
;    can be a Racket identifier;

;--------
; it is good style to start with a letter (usually)
;    and it is ALWAYS EXPECTED STYLE to use
;    meaningful, descriptive, not-misleading
;    choices of name for an identifier

;--------
; if you give a value a name, (and that value
;     for that name is NOT intended to change),
;     that identifier is called a NAMED CONSTANT

;--------
; (and another style bit:
;     named constants will be written in all-uppercase...)

;--------
; here's how we will informally define named
;    constants in BSL Racket:

; (define DESIRED-NEW-CONST expression)

;--------
; define doesn't return a value!!
;    it has the side-effect of making the
;    given identifier (the first argument)
;    a simple expression whose value is
;    the value of the second argument

(define WOWSTAR (overlay (star 75 "outline" "magenta")
                         (text "WOW!" 16 "blue")))

; WOWSTAR is now a simple expression of type image,
;    able to be used anywhere an image expression is
;    allowed

(beside WOWSTAR WOWSTAR WOWSTAR)

;--------
; the SCOPE of an identifier is the part(s) of the
;    program in which that identifier has meaning --
; for a named constant, it is from the define
;    defining it until the end of the Definitions window/
;    end of the Racket file

;--------
; sometimes people distinguish between simple expressions
;    that are identifiers, and simple expressions that
;    are not;
;    we'll often call simple expressions that are
;    not identifiers -- 13 "truthy" #true etc. --
;    LITERALS

;========
; Racket provides several operations that let you
;    check if something is the case in your code --
;    to TEST your code more conveniently

;--------
; one of these is check-expect
; (check-expect desired-expresson-to-test
;               expected-value-expression)

; ^ this has odd semantics/meaning!
; it returns nothing, BUT has the side-effect
;    of comparing the values of its 2 arguments,
;    and if they are the same, this is a passed test,
;    and if they are different, it is a failed test.
; A passed test results in a message after the whole
;    program is done summarizing the number of tests
;    passed;
; A failed test results in a pop-up window with
;    info about the failure

(check-expect (+ (* 3 5)
                 (* 4 6))
              39)
(check-expect WOWSTAR
              (overlay (star 75 "outline" "magenta")
                         (text "WOW!" 16 "blue")))
(check-expect (number? "13")
              #false)

;========
; abstraction, part 2 --
; what if the thing I want to use over and over
;    isn't a single value?
; what if it is something I want to DO over and
;    over?
; then I want to define my own operation,
;      I want to write my own function;

; in MATH...
; f(x) = 3x
; f(7) = 21
; f(10) = 30

; in BSL Racket...
;     here's an example of the above mathematical function
;     in BSL Racket:

(define (f x)
  (* 3 x)
)

; and once defined, can use that function in compound
;    expressions:

(f 10)
(check-expect (f 7)
              21)

; more on writing your own functions tomorrow!