;; 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.5.1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; Worked exercise 22.5.1: ; A list-of-numbers is either ; empty or ; a nelon (non-empty list of numbers). #| (define (function-on-lon L) ; L a list of numbers (cond [(empty? L) ...] [(cons? L) (function-on-nelon L)] )) |# ; A nelon looks like ; (cons number lon ) #| (define (function-on-nelon L) ; L a cons ; (first L) a number ; (rest L) a lon ; (function-on-lon (rest L)) whatever this returns ...) |# ; count-numbers : lon -> number (check-expect (count-numbers empty) 0) (check-expect (count-numbers (cons -4 empty)) 1) (check-expect (count-numbers (cons 5 (cons 2 (cons 8 (cons 6 empty))))) 4) (check-expect (count-numbers-on-nelon (cons -4 empty)) 1) (check-expect (count-numbers-on-nelon (cons 5 (cons 2 (cons 8 (cons 6 empty))))) 4) (define (count-numbers L) ; L a lon (cond [(empty? L) 0] [(cons? L) (count-numbers-on-nelon L)] )) (define (count-numbers-on-nelon L) ; L a nelon ; (first L) a string ; (rest L) a lon ; (count-numbers (rest L)) a number (+ 1 (count-numbers (rest L))) ) ; Alternative version, all in one function #| (define (count-numbers L) ; L a lon (cond [(empty? L) 0] [(cons? L) ; L a nelon ; (first L) a number ; (rest L) a lon ; (count-numbers (rest L)) a number (+ 1 (count-numbers (rest L)))] )) |#