Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang typed/racket
- (define sample "0-306-40615-2")
- (define sample2 "978-0-306-40615-7")
- (: bind-values (-> String (Listof Integer)))
- (define (bind-values x)
- (cast (filter complex?
- (map (lambda ([s : String])
- (if (string=? s "X")
- 10
- (string->number s)))
- (regexp-match* #rx"." x)))
- (Listof Integer)))
- (: isbn10? (-> String Boolean))
- (define (isbn10? x)
- (let ([z (foldl + 0 (map * (bind-values x) (range 1 11)))])
- (cond
- [(zero? (modulo z 11)) true]
- [else false])))
- (: isbn13? (-> String Boolean))
- (define (isbn13? x)
- (let ([z (foldl + 0 (for/list: : (Listof Integer)
- ([i (in-naturals)]
- [j (bind-values x)])
- (if (even? i)
- j
- (* 3 j))))])
- (cond
- [(zero? (modulo z 10)) true]
- [else false])))
- (: isbn? (-> String Boolean))
- (define (isbn? x)
- (let ([z : String (regexp-replace* #px"[^\\dX]" x "")])
- (match (string-length z)
- [10 (isbn10? z)]
- [13 (isbn13? z)]
- [_ false])))
- (isbn? sample)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement