;=====
; CS 111 - string-list-smush example - 2024-10-09

(require 2htdp/image)
(require 2htdp/universe)
(require 2htdp/batch-io)

;=====
; DATA DEFINITION
; an Anything is an expression of any type

;=====
; DATA DEFINITION
; a list is one of:
;    - empty
;    - (cons Anything list)  ; cons for CONStruct a list


;==== TEMPLATE for a function that needs
;     to "walk through" all of the elements of a
;     variable-length list
;
; (define (my-list-funct ... my-list ...)
;     (cond
;         [(empty? my-list) ...]
;         [else
;             (... (... (first my-list) ...)
;                  (my-list-funct ... (rest my-list) ...) ...)]
;     )
; )

;=====
; write-file requires a single string to write to a file...
;
; WHAT IF... I had a LIST of strings that I wanted to write to a file?
; am I stuck?
; NO -- I need a helper function!
;
; What if I had a function that expected a list of strings and
;    returned those strings as a single string?
;    ...say with a \n character tacked on the end of each string in that list
;    of strings?
; THEN a call to that function could be used as an argument to write-file,
;    to be able to then write a list of strings to a file.

;=====
; signature: string-list-smush: list -> string
; purpose: expects any list of strings, and returns a single string
;    containing the strings from the given list each followed by a
;    newline character, \n

; since this should be able to handle a list of strings of any length,
;    I *NEEEEEED* to include tests for the empty-list case as well as
;    a non-empty-list case!

(check-expect (string-list-smush empty) "")

(check-expect (string-list-smush
                  (list "How" "are" "you?" "I" "am" "fine."))
              "How\nare\nyou?\nI\nam\nfine.\n")

(define (string-list-smush string-list)
     (cond
         [(empty? string-list) ""]
         [else
             (string-append
                  (first string-list)
                  "\n"
                  (string-list-smush (rest string-list)))]
     )
)

(string-list-smush (list "How" "are" "you?" "I" "am" "fine."))

;=====
; just for fun!

;=====
; from the Week 2 Lab Exercise:

;=====
; signature: say-hi: string -> string
; purpose: expects a person's name
;     and returns a personalized greeting to that person

(check-expect (say-hi "Jo")
              "Howdy, Jo!")

(check-expect (say-hi "Terry")
              "Howdy, Terry!")

(define (say-hi name)
   (string-append "Howdy, " name "!")
)

;=== end of part from Week 2 Lab Exercise

;=====
; I would like to greet everyone in a list of names!

;=====
; signature: make-greet-list: list -> list
; purpose: expects a list of strings that are names,
;    and returns a list of strings where each is
;    a personalized greeting to that name

(check-expect (make-greet-list empty) empty)

(check-expect
    (make-greet-list (list "Larry" "Bob" "Steve" "Grace"
                           "Tom" "Jerry"))
    (cons "Howdy, Larry!"
          (cons "Howdy, Bob!"
                (cons "Howdy, Steve!"
                      (cons "Howdy, Grace!"
                            (cons "Howdy, Tom!"
                                  (cons "Howdy, Jerry!" empty)))))))
                    
(define (make-greet-list name-list)
    (cond
       [(empty? name-list) empty]
       
       [else (cons (say-hi (first name-list))
                   (make-greet-list (rest name-list)))]
    )
)

;==== END of parts copied from 111lect06-2.rkt ===

; what if I want to create a file greetings.txt that has greetings
;    to EVERYONE in the class?
; my local file 111-names.txt happens to have everyone's name in it (I hope!)

(write-file "greetings.txt"
            (string-list-smush
                (make-greet-list
                    (read-lines "111-names.txt"))))