Day 06: Move assocf into library.
This commit is contained in:
parent
cbbd3b60fe
commit
4f9e8ea253
11
lib.rkt
11
lib.rkt
|
@ -188,7 +188,7 @@
|
||||||
(cons (f v (first lst)) lst))
|
(cons (f v (first lst)) lst))
|
||||||
(list init) 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
|
;; Same as list-ref, except a default value is provided
|
||||||
;; if the index is beyond the length of the list.
|
;; if the index is beyond the length of the list.
|
||||||
(define (list-ref* lst pos [failure-result #f])
|
(define (list-ref* lst pos [failure-result #f])
|
||||||
|
@ -196,13 +196,20 @@
|
||||||
failure-result
|
failure-result
|
||||||
(list-ref lst pos)))
|
(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
|
;; Get first of list or default if empty
|
||||||
(define (first* lst [failure-result #f])
|
(define (first* lst [failure-result #f])
|
||||||
(match lst
|
(match lst
|
||||||
['() failure-result]
|
['() failure-result]
|
||||||
[`(,hd ,@_) hd]))
|
[`(,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)
|
;; repeat : number -> (listof any) -> (listof any)
|
||||||
(define (repeat m lst)
|
(define (repeat m lst)
|
||||||
(if (zero? m) '()
|
(if (zero? m) '()
|
||||||
|
|
|
@ -8,8 +8,6 @@
|
||||||
string-csv
|
string-csv
|
||||||
(map string->number _)))
|
(map string->number _)))
|
||||||
|
|
||||||
(define assocf (∘ cdr assoc))
|
|
||||||
|
|
||||||
(define fish
|
(define fish
|
||||||
(for/list ([age (range 0 9)])
|
(for/list ([age (range 0 9)])
|
||||||
(cons age (count #{= % age} input))))
|
(cons age (count #{= % age} input))))
|
||||||
|
|
Loading…
Reference in New Issue