From 8addf5798a226aec71ffaae0c26cd0197417977b Mon Sep 17 00:00:00 2001 From: Isaac Shoebottom Date: Thu, 29 Sep 2022 16:37:45 -0300 Subject: [PATCH] Add most of lab 6 content --- journal/_src/posts/2022-09-28-lab-six.md | 6 +++++ labs/L06/Lab6.rkt | 30 ++++++++++++++++++++++++ labs/L06/errors.json | 20 ++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 labs/L06/Lab6.rkt create mode 100644 labs/L06/errors.json diff --git a/journal/_src/posts/2022-09-28-lab-six.md b/journal/_src/posts/2022-09-28-lab-six.md index 6dd231f..019b013 100644 --- a/journal/_src/posts/2022-09-28-lab-six.md +++ b/journal/_src/posts/2022-09-28-lab-six.md @@ -5,3 +5,9 @@ In this lab I learned about using hash sets and json parsing in racket. +## Hash Sets +I learned about how to create, access and how to perform operations around hash sets, such as retrieving a color when accessing the hash set with the key of a fruit. You can use them to keep track of counts of a given string, such as in the test module in the lab. + +## JSON Parsing + +To be filled \ No newline at end of file diff --git a/labs/L06/Lab6.rkt b/labs/L06/Lab6.rkt new file mode 100644 index 0000000..b6bbb69 --- /dev/null +++ b/labs/L06/Lab6.rkt @@ -0,0 +1,30 @@ +#lang racket + +(define ht (hash "apple" 'red "banana" 'yellow)) + +(module+ test + (require rackunit) + (check-equal? (hash-ref ht "apple") 'red)) + +(define ht2 (hash-set ht "coconut" 'brown)) + +(module+ test + (check-equal? (hash-ref ht2 "coconut") 'brown) + (check-exn exn:fail? (lambda () (hash-ref ht "coconut")))) + +(define (census . lst) + (define (helper lst acc-hash) + (cond + [(empty? lst) (hash->list acc-hash)] + [else + (let* ([key (first lst)] + [current (hash-ref acc-hash key 0)]) + (helper (rest lst) (hash-set acc-hash key (add1 current))))])) + (helper lst (hash))) + +(module+ test + (check-equal? + (sort (census 1 2 3 1 2) #:key car <) '((1 . 2) (2 . 2) (3 . 1))) + (check-equal? + (sort (census "aardvark" "dingo" "buffalo" "buffalo" "bear") #:key car string