Skip to content

Commit 4275140

Browse files
authored
Merge pull request #51 from dvanhorn/next
Next
2 parents 80f6521 + da0e476 commit 4275140

File tree

5 files changed

+166
-3
lines changed

5 files changed

+166
-3
lines changed

www/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ $(course):
1515
++xref-in setup/xref load-collections-xref \
1616
--redirect-main http://docs.racket-lang.org/ \
1717
$(course).scrbl
18-
mkdir -p $(course)/code/
18+
mkdir -p $(course)/code/
1919
cd notes ; \
20-
tar -c `git ls-files -X .gitignore intro abscond blackmail con dupe extort fraud hustle iniquity jig knock loot mug` | \
20+
tar -c `git ls-files -X .gitignore intro abscond blackmail con dupe extort fraud hustle iniquity jig knock loot mug fp project.pdf` | \
2121
(cd ../main/code ; tar -x) ; cd ../..
2222

2323
clean:

www/info.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#lang info
2+
(define test-omit-paths '("notes/fp/sieve.rkt"))

www/notes/fp/sieve.rkt

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#lang racket
2+
3+
(begin
4+
;; sieve for computing primes, based on GT Benchmarks
5+
6+
;;--------------------------------------------------------------------------------------------------
7+
;; streams
8+
9+
(define (stream hd thunk)
10+
(cons hd thunk))
11+
12+
(define (stream-first s)
13+
(car s))
14+
15+
(define (stream-rest s)
16+
(cdr s))
17+
18+
;(: make-stream (-> Natural (-> stream) stream))
19+
(define (make-stream hd thunk)
20+
(stream hd thunk))
21+
22+
;; Destruct a stream into its first value and the new stream produced by de-thunking the tail
23+
;(: stream-unfold (-> stream (cons Natural stream)))
24+
(define (stream-unfold st)
25+
(cons (stream-first st) ((stream-rest st))))
26+
27+
;; [stream-get st i] Get the [i]-th element from the stream [st]
28+
;(: stream-get (-> stream Natural Natural))
29+
(define (stream-get st i)
30+
(match (stream-unfold st)
31+
[(cons hd tl)
32+
(cond [(= i 0) hd]
33+
[else (stream-get tl (sub1 i))])]))
34+
35+
;; [stream-take st n] Collect the first [n] elements of the stream [st].
36+
;(: stream-take (-> stream Natural (Listof Natural)))
37+
(define (stream-take st n)
38+
(cond [(= n 0) '()]
39+
[else (match (stream-unfold st)
40+
[(cons hd tl)
41+
(cons hd (stream-take tl (sub1 n)))])]))
42+
43+
;;--------------------------------------------------------------------------------------------------
44+
;; sieve
45+
46+
;; `count-from n` Build a stream of integers starting from `n` and iteratively adding 1
47+
;(: count-from (-> Natural stream))
48+
(define (count-from n)
49+
(make-stream n (λ () (count-from (add1 n)))))
50+
51+
;; `sift n st` Filter all elements in `st` that are equal to `n`.
52+
;; Return a new stream.
53+
;(: sift (-> Natural stream stream))
54+
(define (sift n st)
55+
(match (stream-unfold st)
56+
[(cons hd tl)
57+
(cond [(= 0 (modulo hd n)) (sift n tl)]
58+
[else (make-stream hd (λ () (sift n tl)))])]))
59+
60+
;; `sieve st` Sieve of Eratosthenes
61+
;(: sieve (-> stream stream))
62+
(define (sieve st)
63+
(match (stream-unfold st)
64+
[(cons hd tl)
65+
(make-stream hd (λ () (sieve (sift hd tl))))]))
66+
67+
;; stream of prime numbers
68+
;(: primes (-> stream)
69+
(define (primes) (sieve (count-from 2)))
70+
71+
;;--------------------------------------------------------------------------------------------------
72+
;; reading numbers
73+
74+
(define (* n m)
75+
(match n
76+
[0 0]
77+
[1 m]
78+
[n (+ m (* (sub1 n) m))]))
79+
80+
(define (expt n m)
81+
(match m
82+
[0 1]
83+
[1 n]
84+
[m (* n (expt n (sub1 m)))]))
85+
86+
(define (read-number)
87+
(digits->number (read-digits)))
88+
89+
(define (read-digits)
90+
(let ((c (read-char)))
91+
(if (eof-object? c)
92+
'()
93+
(if (eq? #\newline c)
94+
'()
95+
(cons c (read-digits))))))
96+
97+
(define (digits->number ds)
98+
(match ds
99+
['() 0]
100+
[(cons d ds)
101+
(+ (* (digit->number d)
102+
(expt 10 (length ds)))
103+
(digits->number ds))]))
104+
105+
(define (digit->number d)
106+
(match d
107+
[#\0 0]
108+
[#\1 1]
109+
[#\2 2]
110+
[#\3 3]
111+
[#\4 4]
112+
[#\5 5]
113+
[#\6 6]
114+
[#\7 7]
115+
[#\8 8]
116+
[#\9 9]))
117+
118+
;;--------------------------------------------------------------------------------------------------
119+
(define (modulo n m)
120+
(if (< n m)
121+
n
122+
(modulo (- n m) m)))
123+
124+
;;--------------------------------------------------------------------------------------------------
125+
;; computes nth prim
126+
127+
(stream-get (primes) (sub1 (read-number))))

www/notes/project.pdf

935 KB
Binary file not shown.

www/project.scrbl

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#lang scribble/manual
22
@(require "defns.rkt")
3-
@title[#:style '(toc unnumbered)]{Project}
3+
@title[#:style '(unnumbered)]{Project}
44

55
There will be a final course project to be completed over the last
66
several weeks of the course. The project will involve extending the
@@ -19,6 +19,8 @@ learned over the semester to complete a full-featured compiler.
1919
Project repository:
2020
@centered{@link[repo repo]}
2121

22+
@link["code/project.pdf"]{Slides} from lecture on the project.
23+
2224
You are given a working compiler for an extension of the language we
2325
have been developing all semester.
2426

@@ -62,6 +64,38 @@ in the compiler.
6264
There will be a garbage collector provided by the second round of
6365
benchmarks which you will need to incorporate in to your compiler.
6466

67+
@section[#:tag-prefix "fp-" #:style 'unnumbered]{Measuring run-times}
68+
69+
Let's look at an example of how to measure the run-time performance of
70+
the code your compiler generates.
71+
72+
First, let's start with fairly computationally intensive program.
73+
Here is a @link["code/fp/sieve.rkt"]{program} that computes the
74+
@emph{n}th prime number using the ancient
75+
@link["https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes"]{Sieve of
76+
Eratosthenes} method.
77+
78+
Save it to the directory where your compiler lives and run @tt{make
79+
sieve.run}. This will run the compiler to generate the @tt{sieve.run}
80+
executable. This program expects to read a number from the standard
81+
input port.
82+
83+
Run:
84+
85+
@centered{@tt{echo -n 100 | ./sieve.run}}
86+
87+
to compute the 100th prime number.
88+
89+
To measure the time it takes, add the time command:
90+
91+
@centered{@tt{echo -n 100 | time ./sieve.run}}
92+
93+
This will run the program and show the result @emph{and} timing
94+
information. We will be concerned with improving the real time it
95+
takes to run the program.
96+
97+
98+
@section[#:tag-prefix "fp-" #:style 'unnumbered]{Testing}
6599

66100
@bold{There is separate a repository for tests.} When you push your
67101
code, Travis will automatically run your code against the tests. If

0 commit comments

Comments
 (0)