Created
February 8, 2018 01:30
-
-
Save lagenorhynque/aed14f309a1c5808953f2ba4058d35aa to your computer and use it in GitHub Desktop.
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
| (defn sum [xs] | |
| (let [f (fn f [acc xs] | |
| (if (empty? xs) | |
| acc | |
| (recur (+ acc (first xs)) (rest xs))))] | |
| (f 0 xs))) | |
| (sum '(1 2 3)) ; => 6 | |
| (defn sum* [xs] | |
| (letfn [(f [acc xs] | |
| (if (empty? xs) | |
| acc | |
| (recur (+ acc (first xs)) (rest xs))))] | |
| (f 0 xs))) | |
| (sum* '(1 2 3)) ; => 6 | |
| (defn sum** [xs] | |
| (loop [acc 0 | |
| xs xs] | |
| (if (empty? xs) | |
| acc | |
| (recur (+ acc (first xs)) (rest xs))))) | |
| (sum** '(1 2 3)) ; => 6 |
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
| (define (sum xs) | |
| (letrec ((f (lambda (acc xs) | |
| (if (null? xs) | |
| acc | |
| (f (+ acc (car xs)) (cdr xs)))))) | |
| (f 0 xs))) | |
| (sum '(1 2 3)) ; => 6 | |
| (define (sum* xs) | |
| (define (f acc xs) | |
| (if (null? xs) | |
| acc | |
| (f (+ acc (car xs)) (cdr xs)))) | |
| (f 0 xs)) | |
| (sum* '(1 2 3)) ; => 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment