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

; CS 111 - Week 6 Lecture 2 - 2016-09-29

;========
; the promised list shortcut
; (this is an example of "syntactic sugar" --
;    when a programming language provides
;    syntax for something JUST to make it
;    easier than the other ways also provided
;    in the language)

; UNDER THE HOOD, lists are STILL all those
;    cons expressions we've been using!

; the list function expects as many expressions
;   as you'd like, and it results in a
;   cons'd list of (the values of) those expressions

empty
(list 1 2 3)

(equal? (list 1 2 3)
        (cons 1 (cons 2 (cons 3 empty))))

(list 1 (list 2 3 4))

(list "moo")

(list)

(cons 3 (list))

(first (list 30 40 50))
(rest (list 30 40 50))

(cons 45 (list "moo" "baa" "la la la"))

(equal? (rest (list (star 15 "solid" "purple")))
        empty)
(rest (list (star 15 "solid" "purple")))

; want to see this list notation in the DrRacket
;    Interactions window, also?
; ...select the "Beginning Student with List Abbreviations"
;    Teaching Language (go to Language->Choose Language...)


(define COSTS (list 23.45 .03 3.50))
(define PROFITS (list 3.45 .01 1.50))

;========
; it is a pain to use cons to APPEND two lists --
;   to turn 2 lists into a list of the top-level
;   elements of both lists;

(cons COSTS PROFITS)

(length (cons COSTS PROFITS))

;========
; SO, append is an operation provided for this

(append COSTS PROFITS)

(length (append COSTS PROFITS))

(cons 1 (list 2 3 4))

(append (cons 1 (list 2 3 4))
        (list 5 6 7))

;========
; you CAN grab an image from a file or a URL

; I can grab an image from an image file
;    into Racket with bitmap/file:
;    (give the file name as a string,
;    and the filename is assumed to be
;    in the same directory as where your
;    .rkt file is running from
;    (I hope that's true for Windows also!))

(bitmap/file "floating-penguin.png")
(define FLOATY (bitmap/file "floating-penguin.png"))
FLOATY

;========
; bitmap/url does the same thing except from an image
;   file on the web, given by its URL
;   (uniform resource locator)

(bitmap/url
    "http://users.humboldt.edu/smtuttle/dropping-dot.gif")
(define DOTTY (bitmap/url
    "http://users.humboldt.edu/smtuttle/dropping-dot.gif"))
(check-expect
    DOTTY
    (bitmap/url
        "http://users.humboldt.edu/smtuttle/dropping-dot.gif"))

;========
; we can read more from files than just an image...

; and we know some expressions have side-effects
;    (something that results BESIDES a value they
;    return)

; big-bang returns the final value of its world,
;   but has MANY side-effects in-between, displaying
;   scenes of the world value at different times

; define doesn't return anything, but has the side-effect
;    of making an identifier a simple expression representing
;    a value OR making it a function

;========
; file input/output can also be a useful side-effect!

; BSL Racket has a teachpack for simple stream input/output
;   (for simple "batch" input and output from files)

(require 2htdp/batch-io)

;========
; write-file expects a file name given as a string,
;    and a string, and it returns the name of the
;    file it should have tried to write to,
;    and hopefully has the side-effect of returning
;    the name of the new file (overwritten existing file)
;    with that name, containing that string
;    (hopefully be in the directory you are running
;    your .rkt file from)
;    (does throw error if the write fails!)

(write-file "sample.txt" "212")
(write-file "looky.txt" "I\nam\ngroot the great\nagain !!!")
(write-file "moo.txt" (string-append "m" "oooooooo"))

;========
; read-file expects a string representing the name of
;    a file, and tries to return a string containing
;    that file's contents

(read-file "sample.txt")
(read-file "looky.txt")
(read-file "shopping.txt")

(write-file "looky-clicker.txt" (number->string (* 3 7)))
(read-file "looky-clicker.txt")

;========
; read-lines expects a file name given as a string
;   are returns a LIST of strings, one string
;   per line in the file

(read-lines "looky.txt")

; yes, you CAN read the lines of this very .rkt file...!
;    (uncomment to see this, if you indeed have a file
;    with this name in your current folder)
;
;(read-lines "111lect06-2-projected.rkt")

;========
; read-words expects a string containing the name
;   of a file, and tries to return a list
;   of each white-space separated "token" or "word"
;   in that file

(read-words "looky.txt")

; and there's more...

(read-words/line "looky.txt")