diff --git a/journal/_src/posts/2022-09-26-lab-five.md b/journal/_src/posts/2022-09-26-lab-five.md new file mode 100644 index 0000000..fc0e5f6 --- /dev/null +++ b/journal/_src/posts/2022-09-26-lab-five.md @@ -0,0 +1,14 @@ + Title: lab-five + Date: 2022-09-26T08:30:00 + Tags: cs2613, lab, racket, recursion, pattern-matching + +In this lab I learned about tail end recursion and attempted pattern matching + + + +## Tail end recursion + +I learned about tail end recursion and how this is way more efficient on the stack, and that racket has certain guarantees about the efficiency of tail end recursion + +## Pattern matching +It was hard for me to see how to utilize pattern matching. I am sure after spending more time with it on the assignment I will be able to figure it out. \ No newline at end of file diff --git a/labs/L05/count-odds.rkt b/labs/L05/count-odds.rkt new file mode 100644 index 0000000..c89a15e --- /dev/null +++ b/labs/L05/count-odds.rkt @@ -0,0 +1,64 @@ +#lang racket + +(module+ test + (require rackunit)) + +(define (count-odds lst) + (cond + [(empty? lst) 0] + [(odd? (first lst)) (add1 (count-odds (rest lst)))] + [else (count-odds (rest lst))])) + +(module+ test + (check-equal? (count-odds (list 3 2 1 1 2 3 4 5 5 6)) 6)) + +(define (count-odds2 lst) + (define (helper lst odds) + (cond + [(empty? lst) odds] + [(odd? (first lst)) (helper (rest lst) (add1 odds))] + [else (helper (rest lst) odds)])) + (helper lst 0)) + +(module+ test + (check-equal? (count-odds2 (list 3 2 1 1 2 3 4 5 5 6)) 6) + (define random-list (build-list 100 (lambda (x) (random 1 100)))) + (check-equal? (count-odds random-list) (count-odds2 random-list))) + +(define (count-odds3 lst) + (for/fold + ([odds 0]) + ([n lst]) + (cond + [(odd? n) (add1 odds)] + [else odds]))) + +(module+ test + (check-equal? (count-odds3 (list 3 2 1 1 2 3 4 5 5 6)) 6) + (check-equal? (count-odds random-list) (count-odds3 random-list)) + (check-equal? (count-odds3 (list 3 2 1 1 2 3 4 5 5 6)) 6)) + +(define big-list (range 50000000)) +(for* ([fun (list count-odds count-odds2 count-odds3)] + [rep '(1 2 3)]) + (printf "~a ~a\n" fun rep) + (time (fun big-list))) + +;# 1 +;cpu time: 22109 real time: 22390 gc time: 15828 +;# 2 +;cpu time: 19578 real time: 19679 gc time: 16250 +;# 3 +;cpu time: 18109 real time: 18231 gc time: 14859 +;# 1 +;cpu time: 3125 real time: 3178 gc time: 93 +;# 2 +;cpu time: 2765 real time: 2822 gc time: 31 +;# 3 +;cpu time: 2765 real time: 2798 gc time: 31 +;# 1 +;cpu time: 1000 real time: 1002 gc time: 0 +;# 2 +;cpu time: 1187 real time: 1252 gc time: 15 +;# 3 +;cpu time: 984 real time: 1002 gc time: 0 \ No newline at end of file diff --git a/labs/L05/match-ex.rkt b/labs/L05/match-ex.rkt new file mode 100644 index 0000000..e8dffcf --- /dev/null +++ b/labs/L05/match-ex.rkt @@ -0,0 +1,13 @@ +#lang racket + + +(define (list-length2 rst) +(match lst + ['() 0] + [(list fst rst ...) (add1 (list-length2 rst))])) + +(define (list-length3 lst) +(match lst + ['() 0] + [(cons fst rst) (add1 list-length3 rst)])) + diff --git a/labs/L05/match.rkt b/labs/L05/match.rkt new file mode 100644 index 0000000..9d6897f --- /dev/null +++ b/labs/L05/match.rkt @@ -0,0 +1,18 @@ +#lang racket + +(define (list-length list) + (match list + [`() 0] + [(cons head tail) (cons (f head) + (list-length f tail))])) + +(module+ test + (require rackunit) + (check-equal? (list-length '(1 2 3)) 3) + (check-equal? (list-length '()) 0)) + +(define (my-map2 f lst) + (match lst + ['() '()] + [(list head tail ...) (cons (f head) + (my-map2 f tail))]))