Created
June 6, 2011 22:45
-
-
Save ncweinhold/1011280 to your computer and use it in GitHub Desktop.
different ways of summing lists of numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
(define (my-sum-1 lst) | |
(cond | |
((null? lst) 0) | |
(else (+ (car lst) (my-sum-1 (cdr lst)))))) | |
(define (my-sum-2 lst) | |
(define (my-sum-iter res lst) | |
(cond | |
((null? lst) res) | |
(else (my-sum-iter (+ (car lst) res) (cdr lst))))) | |
(my-sum-iter 0 lst)) | |
(define (my-sum-3 lst) | |
(apply + lst)) | |
(define (my-range start end) | |
(cond | |
((= start end) '()) | |
(else (cons start (my-range (+ 1 start) end))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment