;; 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 lists) (read-case-sensitive #t) (teachpacks ((lib "draw.ss" "teachpack" "htdp"))) (htdp-settings #8(#t constructor repeating-decimal #f #t none #f ((lib "draw.ss" "teachpack" "htdp"))))) ;; an empty list empty ;; a one-element list (cons 'apple empty) ;; a two-element list (cons 'apple (cons 'banana empty)) ;; storing a list in a variable (define fruit (cons 'apple (cons 'banana (cons 'orange empty)))) ;; fruit is now a list fruit ;; getting the first element of a list (first (cons 'apple (cons 'banana empty))) (first fruit) ;; a list consists of the first element and ;; the rest of the list. ;; The corresponding selectors are "first" and "rest" ;; "rest" retirns a list (rest fruit) (rest (cons 'apple empty)) ;; to get to further elements, one needs to apply "rest" ;; multiple times and then first (first (rest fruit)) (first (rest (rest fruit))) ;; checking if a list is empty (empty? fruit) (empty? empty) ;; functions can take a list as well, but don't know how ;; many elements ;; Contract: a list of symbols -> boolean ;; Purpose: to determine whether a list of symbols starts with 'apple (define (starts-with-apple alos) ;; alos = a list of symbol (cond [(empty? alos) false] ;; why do we need this? [(symbol=? (first alos) 'apple) true] [else false] ) ) (starts-with-apple empty) ;; expected value false (starts-with-apple (cons 'strawberry fruit)) ;; expected value false (starts-with-apple fruit) ;; expected value true ;; Contract: list of numbers -> number ;; Purpose: to find a sum of elements of a three-element ;; list of numbers ;; If a smaller list is given, an error occurs ;; If a larger list is given, the sum of the first three elements ;; is returned (define (sum-of-three alon) (+ (first alon) (first (rest alon)) (first (rest (rest alon)))) ) (sum-of-three (cons 1 (cons 2 (cons 3 empty)))) ;; expected value 6 (sum-of-three (cons 1.5 (cons 2.5 (cons -4 empty)))) ;; expected value 0 ;; What happens if we don't know the lists length? ;; Contract: a list of numbers -> number ;; Purpose: finds the sum of elements of a list of numbers (define (sum-list alon) (cond [(empty? alon) 0] ;; the sum of an empty list is 0 [else (+ (first alon) (sum-list (rest alon)))] ;; the sum of the first element ;; and the sum of the rest ) ) (sum-list (cons 2 (cons 5 (cons 7 empty)))) ;; expected value 14 (sum-list empty) ;; expected value 0 (sum-list (cons 5 empty)) ;; expected value 5 ;; a list of numbers -> boolean ;; Returns true if the given list of numbers contains 5, ;; returns false otherwise (define (contains-5? alon) (cond [(empty? alon) false] [(= (first alon) 5) true] [else (contains-5? (rest alon))] ) ) (contains-5? (cons 2 (cons 3 (cons 4 empty)))) ;; expected value false (contains-5? (cons 2 (cons 5 (cons 4 empty)))) ;; expected value true (contains-5? (cons 5 (cons 5 (cons 5 empty)))) ;; expected value true (contains-5? (cons 2 (cons 3 (cons 5 empty)))) ;; expected value true (contains-5? empty) ;; expected value false ;; Write a function contains? that takes a list of symbols ;; and a symbol x and returns true if x occurs in the ;; list, false otherwise ;; Examples: ;; (contains? (cons 'A (cons 'B empty)) 'B) -> true ;; (contains? (cons 'A (cons 'B empty)) 'C) -> false ;; Write a function count that takes a list of numbers ;; and a number n and returns the number of times ;; n occurs in the list