Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. #lang typed/racket
  2.  
  3. (define sample "0-306-40615-2")
  4. (define sample2 "978-0-306-40615-7")
  5.  
  6. (: bind-values (-> String (Listof Integer)))
  7. (define (bind-values x)
  8. (cast (filter complex?
  9. (map (lambda ([s : String])
  10. (if (string=? s "X")
  11. 10
  12. (string->number s)))
  13. (regexp-match* #rx"." x)))
  14. (Listof Integer)))
  15.  
  16. (: isbn10? (-> String Boolean))
  17. (define (isbn10? x)
  18. (let ([z (foldl + 0 (map * (bind-values x) (range 1 11)))])
  19. (cond
  20. [(zero? (modulo z 11)) true]
  21. [else false])))
  22.  
  23. (: isbn13? (-> String Boolean))
  24. (define (isbn13? x)
  25. (let ([z (foldl + 0 (for/list: : (Listof Integer)
  26. ([i (in-naturals)]
  27. [j (bind-values x)])
  28. (if (even? i)
  29. j
  30. (* 3 j))))])
  31. (cond
  32. [(zero? (modulo z 10)) true]
  33. [else false])))
  34.  
  35. (: isbn? (-> String Boolean))
  36. (define (isbn? x)
  37. (let ([z : String (regexp-replace* #px"[^\\dX]" x "")])
  38. (match (string-length z)
  39. [10 (isbn10? z)]
  40. [13 (isbn13? z)]
  41. [_ false])))
  42.  
  43. (isbn? sample)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement