Skip to content

Commit

Permalink
added up to filter-multiples
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinbui904 committed Jan 19, 2022
1 parent fc18ff6 commit 3f39520
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions assignment6-starter/collaboration.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://stackoverflow.com/questions/60801831/how-modulo-and-remainder-should-work-in-scheme
76 changes: 76 additions & 0 deletions assignment6-starter/main.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
;Written by Thien K. M. Bui
;Programming Languages Carleton College Winter '22
;Last updated 01/19/22

;seq
;param: two integers (first last)
;return: lazy list containing integer value from first to last
(define seq
(lambda (first last)
(if (> first last)
#f
;return a procedure in cdr that if invoked, calls seq
(cons first (lambda () (seq (+ first 1) last))))))

;inf-seq
;param: one integer (first)
;return: lazy list of integer from first to infinity
(define inf-seq
(lambda (first)
(cons first (lambda () (inf-seq (+ first 1))))))

;first-n
;param: lazy-list and an integer (n)
;return: Scheme list containing the first n values in the lazy list
;e.g. (first-n (seq 1 3) 2) --> (1 2)
(define first-n
(lambda (lazy n)
;if lazy is empty or n is 0
(if (or (null? lazy) (= n 0))
'()
(first-n-helper lazy n '()))))

;first-n-helper
;param: lazy-list lazy, integer n, scheme-list current_list
;return: Scheme list containing the first n values in the lazy list
;NOTE: use this helper to tail-optimize first-n
(define first-n-helper
(lambda (lazy n current_list)
(if (or (= n 0) (not lazy))
;base case, if n is 0 or if we're at the end of lazy (last member of a lazy list is #f)
current_list
(first-n-helper ((cdr lazy)) (- n 1) (append current_list (list (car lazy)))))))

;nth
;param: lazy-list lazy and integer n
;return: nth value of the lazy-list
(define nth
(lambda (lazy n)
(cond
((not lazy) #f)
;clause below must go after (not lazy) because otherwise we'd be trying to call car on a #f
((= n 1) (car lazy))
(else (nth ((cdr lazy)) (- n 1))))))

;filter-multiples
;param: lazy-list lazy and integer n
;return: lazy-list of all integers in lazy not-divisible by n

(define filter-multiples
(lambda (lazy n)
(filter-multiples-helper 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))))
)))
2 changes: 2 additions & 0 deletions assignment6-starter/scheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export GUILE_AUTO_COMPILE=0
rlwrap guile "$@"
1 change: 1 addition & 0 deletions assignment6-starter/test-m
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./scheme tester-script.scm "tests-m.scm"
39 changes: 39 additions & 0 deletions assignment6-starter/tester-script.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
;; Based on https://srfi.schemers.org/srfi-64/srfi-64.html Unit testing set up
;; code below. Ignore most of this; the important part to focus on is the tests
;; in the associated files.
(use-modules (ice-9 format))
(use-modules (srfi srfi-64))
(define (my-simple-runner)
(let ((runner (test-runner-null))
(num-passed 0)
(num-failed 0))
(test-runner-on-test-end! runner
(lambda (runner)
(case (test-result-kind runner)
((pass xpass) (set! num-passed (+ num-passed 1)))
((fail xfail)
(format #t "FAILED:\n ~s:line ~s\n ~s\n"
(test-result-ref runner 'source-file)
(test-result-ref runner 'source-line)
(test-result-ref runner 'source-form))

(set! num-failed (+ num-failed 1)))
(else #t))))
(test-runner-on-final! runner
(lambda (runner)
(format #t "Passing tests: ~d.~%Failing tests: ~d.~%"
num-passed num-failed)))
runner))

(load "main.scm")

(test-runner-factory
(lambda () (my-simple-runner)))

(define test-file-name (cadr (command-line)))

(test-begin "test suite")
(load test-file-name)
(test-end "m test suite")

(exit (test-runner-fail-count (test-runner-current)))
34 changes: 34 additions & 0 deletions assignment6-starter/tests-m.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
;; DO NOT CHANGE THESE TESTS.

;; Module to include testing code
(use-modules (srfi srfi-64))

;; Basic tests

(let ((x (seq 10 28)) (y (inf-seq 15)))
(test-equal "basics1" 10 (car x))
(test-equal "basics2" 11 (car ((cdr x))))
(test-equal "basics3" 15 (car y))
(test-equal "basics4" 16 (car ((cdr y))))
(test-equal "basics5" '(15 16 17 18 19 20) (first-n y 6))
(test-equal "basics6" 22 (nth y 8)))

(test-equal "filter-mult1" '(3 5) (first-n (filter-multiples (inf-seq 2) 2) 2) )
(test-equal "filter-mult2" '(4 5 7 8 10 11) (first-n (filter-multiples (inf-seq 3) 3) 6))
(test-equal "filter-mult3" '(5 7 8 9 10 11 13 14 15 16 17 19) (first-n (filter-multiples (inf-seq 5) 6) 12))
(test-equal "filter-mult4" '(5 6 8 9 10 11 12 13 15 16 17 18 19) (first-n (filter-multiples (seq 5 30) 7) 13))

;; Advanced tests (edge cases)

(test-equal "neg-inf-seq1" -3 (car (inf-seq -3)))
(test-equal "neg-inf-seq2" -2 (car ((cdr (inf-seq -3)))))
(test-equal "first-n-edge1" '(3 4) (first-n (seq 3 4) 20))
(test-equal "first-n-edge2" '() (first-n (seq 3 2) 20))

(test-assert "nth-edgh1" (not (nth (seq 3 2) 5)))
(test-assert "nth-edge2" (not (nth (seq 3 5) 25)))

(test-equal "filter-mult-first-n-edge" '(5 6 8 9 10 11 12 13 15 16 17 18 19 20 22 23 24 25 26 27 29 30) (first-n (filter-multiples (seq 5 30) 7) 500))

(test-equal "primes1" '(2 3 5 7 11 13 17 19 23 29) (first-n (primes) 10))
(test-equal "primes2" 71 (nth (primes) 20))

0 comments on commit 3f39520

Please sign in to comment.