Skip to content

Commit

Permalink
add dir
Browse files Browse the repository at this point in the history
  • Loading branch information
lyf6lyf committed Mar 3, 2015
1 parent 11ebadb commit 07b36cf
Show file tree
Hide file tree
Showing 18 changed files with 1,983 additions and 83 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.jpg
*.pdf
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
80 changes: 80 additions & 0 deletions hw4/hw4.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

#lang racket

(provide (all-defined-out)) ;; so we can put tests in a second file

;; put your code below
(define (sequence low high stride)
(if (> low high)
null
(cons low (sequence (+ low stride) high stride))))

(define (string-append-map xs suffix)
(map (lambda (s) (string-append s suffix)) xs))

(define (list-nth-mod xs n)
(if (< n 0)
(error "list-nth-mod: negative number")
(if (null? xs)
(error "list-nth-mod: empty list")
(car (list-tail xs (remainder n (length xs)))))))

(define (stream-for-n-steps s n)
(if (> n 0)
(cons (car (s)) (stream-for-n-steps (cdr (s)) (- n 1)))
null))

(define funny-number-stream
(letrec ([f
(lambda (x) (cons
(if (= 0 (remainder x 5)) (- 0 x) x)
(lambda () (f (+ x 1)))))])
(lambda () (f 1))))

(define dan-then-dog
(lambda () (cons "dan.jpg" (lambda () (cons "dog.jpg" dan-then-dog)))))

(define (stream-add-zero s)
(lambda () (cons (cons 0 (car (s)))
(stream-add-zero (cdr (s))))))

(define (cycle-lists xs ys)
(define (f n)
(lambda () (cons
(cons (list-nth-mod xs n) (list-nth-mod ys n))
(f (+ 1 n)))))
(f 0))

(define (vector-assoc v vec)
(letrec ([l (vector-length vec)]
[f (lambda (n)
(if (= l n) #f
(let ([ref (vector-ref vec n)])
(if (pair? ref)
(if (equal? v (car ref)) ref (f (+ 1 n)))
(f (+ 1 n))))))])
(f 0)))

(define (cached-assoc xs n)
(let ([cache (make-vector n #f)]
[pos 0])
(lambda (v)
(let ([cr (vector-assoc v cache)])
(if cr cr
(let ([xr (assoc v xs)])
(if xr
(begin (vector-set! cache pos xr)
(set! pos (if (= (+ 1 pos) n) 0 (+ 1 pos)))
xr)
#f)))))))

(define-syntax while-less
(syntax-rules (do)
[(while-less e1 do e2)
(letrec ([x e1]
[thunk (lambda () (cons e2 thunk))]
[f (lambda (t)
(if (< (car (t)) x)
(f (cdr (t)))
#t))])
(f thunk))]))
59 changes: 59 additions & 0 deletions hw4/hw4test.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#lang racket
;; Programming Languages Homework4 Simple Test
;; Save this file to the same directory as your homework file
;; These are basic tests. Passing these tests does not guarantee that your code will pass the actual homework grader

;; Be sure to put your homework file in the same folder as this test file.
;; Uncomment the line below and change HOMEWORK_FILE to the name of your homework file.
(require "hw4.rkt")
(require rackunit)

;; Helper functions
(define ones (lambda () (cons 1 ones)))
(define a 2)

(define tests
(test-suite
"Sample tests for Assignment 4"

; sequence test
(check-equal? (sequence 0 5 1) (list 0 1 2 3 4 5) "Sequence test")

; string-append-map test
(check-equal? (string-append-map
(list "dan" "dog" "curry" "dog2")
".jpg") '("dan.jpg" "dog.jpg" "curry.jpg" "dog2.jpg") "string-append-map test")

; list-nth-mod test
(check-equal? (list-nth-mod (list 0 1 2 3 4) 2) 2 "list-nth-mod test")

; stream-for-n-steps test
(check-equal? (stream-for-n-steps (lambda () (cons 1 ones)) 1) (list 1) "stream-for-n-steps test")

; funny-number-stream test
(check-equal? (stream-for-n-steps funny-number-stream 16) (list 1 2 3 4 -5 6 7 8 9 -10 11 12 13 14 -15 16) "funny-number-stream test")

; dan-then-dog test
(check-equal? (stream-for-n-steps dan-then-dog 1) (list "dan.jpg") "dan-then-dog test")

; stream-add-zero test
(check-equal? (stream-for-n-steps (stream-add-zero ones) 1) (list (cons 0 1)) "stream-add-zero test")

; cycle-lists test
(check-equal? (stream-for-n-steps (cycle-lists (list 1 2 3) (list "a" "b")) 3) (list (cons 1 "a") (cons 2 "b") (cons 3 "a"))
"cycle-lists test")

; vector-assoc test
(check-equal? (vector-assoc 4 (vector (cons 2 1) (cons 3 1) (cons 4 1) (cons 5 1))) (cons 4 1) "vector-assoc test")

; cached-assoc tests
(check-equal? ((cached-assoc (list (cons 1 2) (cons 3 4)) 3) 3) (cons 3 4) "cached-assoc test")

; while-less test
(check-equal? (while-less 7 do (begin (set! a (+ a 1)) a)) #t "while-less test")

))

(require rackunit/text-ui)
;; runs the test
(run-tests tests)
Loading

0 comments on commit 07b36cf

Please sign in to comment.