;; The first three lines of this file were inserted by DrRacket. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-intermediate-reader.ss" "lang")((modname foldr) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
; lon -> num
; sums all the nums in lon
;(define (sum lon)
; (cond
; [(empty? lon) 0] ; <-- difference 1
; [(cons? lon) (+ (first lon) ; <-- difference 2
; (sum (rest lon)))]))
(check-expect (sum '()) 0)
(check-expect (sum (list 1 2 3 4)) 10)
; lon -> num
; multiplies all the nums in lon
;(define (product lon)
; (cond
; [(empty? lon) 1] ; <-- difference 1
; [(cons? lon) (* (first lon) ; <-- difference 2
; (product (rest lon)))]))
(check-expect (product '()) 1)
(check-expect (product (list 1 2 3 4)) 24)
; abstracting over sum and product...
; list-of-num num (num num -> num) -> num
; combines numbers in lon using COMB function and identity value base-n
(define (combine-nums lon base-n COMB)
(cond
[(empty? lon) base-n]
[(cons? lon) (COMB (first lon)
(combine-nums (rest lon) base-n COMB))]))
; lon -> num
; sums all the nums in lon
(define (sum lon)
(foldr + 0 lon))
; (combine-nums lon 0 +))
; lon -> num
; multiplies all the nums in lon
(define (product lon)
(foldr * 1 lon))
; (combine-nums lon 1 *))