diff --git a/lib.rkt b/lib.rkt index 88b06fe..aa3dfe6 100644 --- a/lib.rkt +++ b/lib.rkt @@ -188,7 +188,7 @@ (cons (f v (first lst)) lst)) (list init) lst))) -;; list-ref* : (listof a) -> number -> a -> (or a #f) +;; list-ref* : (listof a) -> number -> a -> (or/c a #f) ;; Same as list-ref, except a default value is provided ;; if the index is beyond the length of the list. (define (list-ref* lst pos [failure-result #f]) @@ -196,13 +196,20 @@ failure-result (list-ref lst pos))) -;; first* : (listof a) -> (or a #f) +;; first* : (listof a) -> (or/c a #f) ;; Get first of list or default if empty (define (first* lst [failure-result #f]) (match lst ['() failure-result] [`(,hd ,@_) hd])) +;; assocf : a -> (listof (cons a b)) -> (or/c b #f) +;; Returns cdr if assoc succeeds, otherwise #f +(define (assocf a lst) + (match (assoc a lst) + [`(,_ . ,b) b] + [else #f])) + ;; repeat : number -> (listof any) -> (listof any) (define (repeat m lst) (if (zero? m) '() diff --git a/src/06.rkt b/src/06.rkt index 444ff6e..956ec54 100644 --- a/src/06.rkt +++ b/src/06.rkt @@ -8,8 +8,6 @@ string-csv (map string->number _))) -(define assocf (∘ cdr assoc)) - (define fish (for/list ([age (range 0 9)]) (cons age (count #{= % age} input))))