Guest User

Untitled

a guest
Dec 10th, 2017
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. ;; The first three lines of this file were inserted by DrRacket. They record metadata
  2. ;; about the language level of this file in a form that our tools can easily process.
  3. #reader(lib "htdp-advanced-reader.ss" "lang")((modname blabla) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ())))
  4. ;; email represents an email
  5. ;; from: String - email address of sender
  6. ;; to: String - email address of recipient
  7. ;; subject: String - subject line
  8. ;; contents: String - the contents of the email
  9. (define-struct email (from to subject contents))
  10.  
  11. ;; example data
  12. (define email1 (make-email "karl@klammer.com" "me@me.com" "hi" "blablabla"))
  13. (define email2 (make-email "karl@klammer.com" "me@me.com" "[SPAM]hi" "blablabla"))
  14. (define email3 (make-email "eva@longoria.com" "me@gmx.net" "hi" "blablabla"))
  15. (define emailx (make-email "eva@lon" "me@gmx.net" "hi" "blablabla"))
  16.  
  17.  
  18. (define incoming-mails (list email1 email2 email3 emailx))
  19. (define noincoming-mails '())
  20.  
  21. ;; contact represents a single contact
  22. ;; name: String - the name of the contact
  23. ;; address: String - the email address
  24. (define-struct contact (name address))
  25.  
  26. ;; mail-folders represents the folders for an email program
  27. ;; friends: (listof email) - emails from friends
  28. ;; indirect: (listof email) - emails received but not sent via "To:"
  29. ;; unknown: (listof email) - emails from persons not in the contacts
  30. ;; spam: (listof email) - emails classified as spam or possible spam
  31. (define-struct mail-folders (friends indirect unknown spam))
  32.  
  33. ;; example data
  34. (define my-mails-empty (make-mail-folders '() '() '() '()))
  35. (define my-mails (make-mail-folders (list email1) '() (list emailx) '()))
  36.  
  37. ;; example data
  38. (define karl (make-contact "Karl Klammer" "karl@klammer.com"))
  39. (define eva (make-contact "Eva Longoria" "eva@longoria.com"))
  40. (define my-contacts (list karl eva))
  41.  
  42. (define my-emails (list "me@me.com" "me@acm.org" "me@gmx.net"))
  43.  
  44. ;; valid-recipient?
  45. ;;
  46. ;;
  47. ;;
  48. (define (valid-recipient? add mycon)
  49. (known-sender? add mycon))
  50.  
  51. (check-expect (valid-recipient? "karl@klammer.com" my-contacts) true)
  52.  
  53.  
  54. ;; Tests
  55.  
  56. ;; known-sender?
  57. ;;
  58. ;;
  59. ;;
  60. (define (known-sender? add con)
  61. (cond [(empty? con) false]
  62. [(string=? add (contact-address (first con))) true]
  63. [else (known-sender? add (rest con))]))
  64.  
  65. (check-expect (known-sender? "karl@klammer.com" my-contacts) true)
  66. (check-expect (known-sender? "karl@klammer.co" my-contacts) false)
  67.  
  68. ;; Tests
  69.  
  70. ;; is-spam?
  71. ;;
  72. ;;
  73. ;;
  74. (define (is-spam? sub)
  75. (cond [(< (string-length sub) 6) false]
  76. [(string=? "[SPAM]" (substring sub 0 6)) true]
  77. [(> (string-length sub) 7) (if (string=? "[SPAM?]" (substring sub 0 7)) true false)]
  78. [else false]))
  79.  
  80. (check-expect (is-spam? "[Spam]") false)
  81. (check-expect (is-spam? "[sp hallo") false)
  82. (check-expect (is-spam? "[SPAM?] hi") true)
  83. (check-expect (is-spam? "[SPAM]") true)
  84. ;; Tests
  85.  
  86. ;; sort-emails
  87. ;; sort-emails: (listof email) mailfolders (listof contact)(listof String) -> mailfolders
  88. ;;
  89. ;;
  90. (define (sort-emails inc off lcon ladd)
  91. (local ((define (fill-mailfolder mail off1)
  92. (cond [(empty? off1) (cons mail empty)]
  93. [else (fill-mailfolder mail (rest off1))]))
  94.  
  95. (define (sort-mailtheme num mail)
  96. (cond [(= num 1) (fill-mailfolder mail (mail-folders-spam))]
  97. [(= num 2) (fill-mailfolder mail (mail-folders-unknown))]
  98. [(= num 3) (fill-mailfolder mail (mail-folders-indirect))]
  99. [(= num 4) (fill-mailfolder mail (mail-folders-friends))]))
  100.  
  101. (define (check-email mail off lcon ladd)
  102. (if (is-spam? (email-subject mail)) (sort-mailtheme 1 mail)
  103. (if (known-sender? (email-from mail) lcon) (sort-mailtheme 2 mail)
  104. (if (not (valid-recipient? (email-to mail) ladd)) (sort-mailtheme 3 mail)
  105. (sort-mailtheme 4 mail))))))
  106.  
  107. (map check-email inc off lcon ladd)))
  108.  
  109. (sort-emails incoming-mails my-mails my-contacts my-emails)
  110.  
  111. ;;(check-expect (sort-emails incoming-mails my-mails my-contacts my-emails) (list email1 email1 email3) '() (list emailx) (list email2)))
  112.  
  113.  
  114.  
  115.  
  116. ;; Tests
Add Comment
Please, Sign In to add comment