;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname 22.3.2) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
; Figure 22.1

; An empty list has no parts.
(define-struct empty-list ())
; make-empty-list : nothing -> empty-list
; empty-list? : anything -> boolean
; A nelos has two parts: first (a string) and rest (a los).
(define-struct nelos (first rest))
; make-nelos : string los -> nelos
; nelos-first : nelos -> string
; nelos-rest : nelos -> los
; nelos? : anything -> boolean
#|
(define (function-on-nelos L)
; L a nelos
; (nelos-first L) a string
; (nelos-rest L) a los
; (function-on-los (nelos-rest L)) whatever this returns
...)
|#
; A los is either an empty-list or a nelos.
#|
(define (function-on-los L)
; L a los
(cond [(empty-list? L) ...]
[(nelos? L) (function-on-nelos L)]
))
|#
(define nothing (make-empty-list))
(define english (make-nelos "hello" nothing))
(define span-eng (make-nelos "buenos dias" english))
(define heb-span-eng (make-nelos "shalom" span-eng))
(define fhse (make-nelos "bonjour" heb-span-eng))
(define afhse (make-nelos "salaam" fhse))
(define dwarfs (make-nelos "sleepy" (make-nelos "sneezy" (make-nelos "dopey" (make-nelos "doc" (make-nelos "happy" (make-nelos "bashful" (make-nelos "grumpy" nothing))))))))



; Worked exercise 22.3.2

; count-strings : los -> number
; count-strings-on-nelos : nelos -> number

(check-expect (count-strings nothing) 0)
(check-expect (count-strings english) 1)
(check-expect (count-strings span-eng) 2)
(check-expect (count-strings afhse) 5)
(check-expect (count-strings dwarfs) 7)

; can’t call (count-strings-on-nelos nothing)
; because nothing isn’t a nelos
(check-expect (count-strings-on-nelos english) 1)
(check-expect (count-strings-on-nelos span-eng) 2)
(check-expect (count-strings-on-nelos afhse) 5)
(check-expect (count-strings-on-nelos dwarfs) 7)

(define (count-strings L)
  ; L a los
  (cond [(empty-list? L) 0]
        [(nelos? L) (count-strings-on-nelos L)]
        ))

(define (count-strings-on-nelos L)
  ; L a nelos
  ; (nelos-first L) a string
  ; (nelos-rest L) a los
  ; (count-strings (nelos-rest L)) a number
  (+ 1 (count-strings (nelos-rest L))) )



; Alternative version, all in one function
#|
(define (count-strings L)
  ; L a los
  (cond [(empty-list? L) 0]
        [(nelos? L)
         ; L a nelos
         ; (nelos-first L) a string
         ; (nelos-rest L) a los
         ; (count-strings (nelos-rest L)) a number
         (+ 1 (count-strings (nelos-rest L))) ]
        ))
|#