(load "asmt-helper.scm") ;; FROM-TO ;; ------- ;; Inputs: LOW and HIGH, positive integers ;; Output: a list of every number counting up between ;; LOW and HIGH, inclusive (define from-to (lambda (low high) (if (> low high) ;; Base case: No way to count up from low to high '() ;; Recursive case: Add LOW to output and consider ;; the next smallest range going to HIGH (cons low (from-to (+ 1 low) high))))) (tester '(from-to 1 1)) ;; (1) (tester '(from-to 1 5)) ;; (1 2 3 4 5) (tester '(from-to 1 0)) ;; () ;; SUM-TO ;; ------ ;; Inputs: N, a positive integer ;; Output: The sum of all numbers from 1 to N (define sum-to (lambda (n) (apply + (from-to 1 n)))) (tester '(sum-to 5)) ;; 15 (tester '(sum-to 1)) ;; 1 (tester '(sum-to 0)) ;; 0 (define our-plus (lambda lst (apply + lst))) ;; DOUBLE ;; ------ ;; Inputs: X, an integer ;; Output: twice X (define double (lambda (x) (* 2 x))) ;; DOUBLE-ALL ;; ---------- ;; Inputs: LST, a list of integers ;; Output: A list with every number in LST doubled! (define double-all (lambda (lst) (map double lst))) (tester '(double-all '(1 2 3))) ;; SUM-SQUARES-TO ;; -------------- ;; Inputs: N, a positive integer ;; Output: the sum of the squares of each number from 1 to N (define sum-squares-to (lambda (n) (apply + (map (lambda (x) (* x x)) (from-to 1 n))))) (tester '(sum-squares-to 10))