Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;; The first three lines of this file were inserted by DrRacket. They record metadata
- ;; about the language level of this file in a form that our tools can easily process.
- #reader(lib "htdp-advanced-reader.ss" "lang")((modname blabla) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ())))
- ;; email represents an email
- ;; from: String - email address of sender
- ;; to: String - email address of recipient
- ;; subject: String - subject line
- ;; contents: String - the contents of the email
- (define-struct email (from to subject contents))
- ;; example data
- (define email1 (make-email "karl@klammer.com" "me@me.com" "hi" "blablabla"))
- (define email2 (make-email "karl@klammer.com" "me@me.com" "[SPAM]hi" "blablabla"))
- (define email3 (make-email "eva@longoria.com" "me@gmx.net" "hi" "blablabla"))
- (define emailx (make-email "eva@lon" "me@gmx.net" "hi" "blablabla"))
- (define incoming-mails (list email1 email2 email3 emailx))
- (define noincoming-mails '())
- ;; contact represents a single contact
- ;; name: String - the name of the contact
- ;; address: String - the email address
- (define-struct contact (name address))
- ;; mail-folders represents the folders for an email program
- ;; friends: (listof email) - emails from friends
- ;; indirect: (listof email) - emails received but not sent via "To:"
- ;; unknown: (listof email) - emails from persons not in the contacts
- ;; spam: (listof email) - emails classified as spam or possible spam
- (define-struct mail-folders (friends indirect unknown spam))
- ;; example data
- (define my-mails-empty (make-mail-folders '() '() '() '()))
- (define my-mails (make-mail-folders (list email1) '() (list emailx) '()))
- ;; example data
- (define karl (make-contact "Karl Klammer" "karl@klammer.com"))
- (define eva (make-contact "Eva Longoria" "eva@longoria.com"))
- (define my-contacts (list karl eva))
- (define my-emails (list "me@me.com" "me@acm.org" "me@gmx.net"))
- ;; valid-recipient?
- ;;
- ;;
- ;;
- (define (valid-recipient? add mycon)
- (known-sender? add mycon))
- (check-expect (valid-recipient? "karl@klammer.com" my-contacts) true)
- ;; Tests
- ;; known-sender?
- ;;
- ;;
- ;;
- (define (known-sender? add con)
- (cond [(empty? con) false]
- [(string=? add (contact-address (first con))) true]
- [else (known-sender? add (rest con))]))
- (check-expect (known-sender? "karl@klammer.com" my-contacts) true)
- (check-expect (known-sender? "karl@klammer.co" my-contacts) false)
- ;; Tests
- ;; is-spam?
- ;;
- ;;
- ;;
- (define (is-spam? sub)
- (cond [(< (string-length sub) 6) false]
- [(string=? "[SPAM]" (substring sub 0 6)) true]
- [(> (string-length sub) 7) (if (string=? "[SPAM?]" (substring sub 0 7)) true false)]
- [else false]))
- (check-expect (is-spam? "[Spam]") false)
- (check-expect (is-spam? "[sp hallo") false)
- (check-expect (is-spam? "[SPAM?] hi") true)
- (check-expect (is-spam? "[SPAM]") true)
- ;; Tests
- ;; sort-emails
- ;; sort-emails: (listof email) mailfolders (listof contact)(listof String) -> mailfolders
- ;;
- ;;
- (define (sort-emails inc off lcon ladd)
- (local ((define (fill-mailfolder mail off1)
- (cond [(empty? off1) (cons mail empty)]
- [else (fill-mailfolder mail (rest off1))]))
- (define (sort-mailtheme num mail)
- (cond [(= num 1) (fill-mailfolder mail (mail-folders-spam))]
- [(= num 2) (fill-mailfolder mail (mail-folders-unknown))]
- [(= num 3) (fill-mailfolder mail (mail-folders-indirect))]
- [(= num 4) (fill-mailfolder mail (mail-folders-friends))]))
- (define (check-email mail off lcon ladd)
- (if (is-spam? (email-subject mail)) (sort-mailtheme 1 mail)
- (if (known-sender? (email-from mail) lcon) (sort-mailtheme 2 mail)
- (if (not (valid-recipient? (email-to mail) ladd)) (sort-mailtheme 3 mail)
- (sort-mailtheme 4 mail))))))
- (map check-email inc off lcon ladd)))
- (sort-emails incoming-mails my-mails my-contacts my-emails)
- ;;(check-expect (sort-emails incoming-mails my-mails my-contacts my-emails) (list email1 email1 email3) '() (list emailx) (list email2)))
- ;; Tests
Add Comment
Please, Sign In to add comment