diff options
Diffstat (limited to 'js/games/nluqo.github.io/~bh/61a-pages/Lectures/3.5/prime1.scm')
-rw-r--r-- | js/games/nluqo.github.io/~bh/61a-pages/Lectures/3.5/prime1.scm | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/js/games/nluqo.github.io/~bh/61a-pages/Lectures/3.5/prime1.scm b/js/games/nluqo.github.io/~bh/61a-pages/Lectures/3.5/prime1.scm new file mode 100644 index 0000000..35f7a70 --- /dev/null +++ b/js/games/nluqo.github.io/~bh/61a-pages/Lectures/3.5/prime1.scm @@ -0,0 +1,22 @@ + + +(define (prime? n) + (null? (filter (lambda (x) (= (remainder n x) 0)) + (range 2 (- n 1))))) + +(define (range from to) + (if (> from to) + '() + (cons from (range (+ from 1) to)))) + + +(define (filter predicate data) + (cond ((null? data) '()) + ((predicate (car data)) + (cons (car data) (filter predicate (cdr data)))) + (else (filter predicate (cdr data))))) + + + + +;; try (filter prime? (range 2 100)) |