Skip to content

Commit

Permalink
added collaboration.txt and all other functions needed for assignment 6
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbui904 committed Jan 21, 2022
1 parent 3f39520 commit 2e61329
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
2 changes: 1 addition & 1 deletion assignment6-starter/collaboration.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
https://stackoverflow.com/questions/60801831/how-modulo-and-remainder-should-work-in-scheme
ALl code in main.scm is written by me, Thien K. M. Bui aside from the modulo function whcih I had to look up https://stackoverflow.com/questions/60801831/how-modulo-and-remainder-should-work-in-scheme
45 changes: 29 additions & 16 deletions assignment6-starter/main.scm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;Written by Thien K. M. Bui
;Programming Languages Carleton College Winter '22
;Last updated 01/19/22
;Last updated 01/20/22

;seq
;param: two integers (first last)
Expand Down Expand Up @@ -58,19 +58,32 @@

(define filter-multiples
(lambda (lazy n)
(filter-multiples-helper lazy n '())
))
(cond
((not lazy) #f)
((= (modulo (car lazy) n) 0) (filter-multiples ((cdr lazy)) n))
(else (cons (car lazy) (lambda () (filter-multiples ((cdr lazy)) n)))))))

; (if (or (not lazy) (= (modulo (car lazy) n) 0))
; (cons '() (filter-multiples ((cdr lazy)) n))
; (cons (car lazy) (filter-multiples ((cdr lazy)) n)))
;filter-multiples-helper (for tail optimization)
;param: lazy-list lazy, integer n, return-lazy-list return_lazy
;return: lazy-list of all integers in lazy not-divisible by n
(define filter-multiples-helper
(lambda (lazy n return_lazy)
(cond
((not lazy) '())
((= (modulo (car lazy) n) 0) (filter-multiples-helper ((cdr lazy)) n (append '() return_lazy)))
(else (filter-multiples-helper ((cdr lazy)) n (append return_lazy (car lazy))))
)))
;almost-eratosthenes
;param: lazy-list lazy
;return: lazy list containing the first element of lazy-list and all other elements in list that are not divisible by the first element
(define almost-eratosthenes
(lambda (lazy)
(filter-multiples lazy (car lazy))))

;eratosthenes
;param: lazy-list
;return: lazy-list containing all prime numbers (Sieve of Eratosthenes)
(define eratosthenes
(lambda (lazy)
(if (null? lazy)
#f
(cons (car lazy)
(lambda ()
(eratosthenes (filter-multiples lazy (car lazy))))))))

;prime
;param: none
;return: lazy-list of all prime numbers
(define primes
(lambda ()
(eratosthenes (inf-seq 2))))

0 comments on commit 2e61329

Please sign in to comment.