I’ve been wracking my brain on a scheme problem for the last couple days that involves creating a procedure with message passing that keeps track of a list of friends, and can then manipulate that list to change the value of a specific friend to online or offline. If the person that is put in is a friend, it returns true. If they haven’t been added yet, it returns false. It can also show the list of friends that are online, and another list of the ones that you have. Basically, the idea is to make something of a changeable Facebook tracker. It responds to 4 inputs: ‘add, ‘toggle-status, ‘get-online-friends, and ‘get-friends.
This is what I’ve managed to get so far:
(define (sort-strings lst)
(sort lst string<?))
(define (make-facebook-list)
(let ((T '()))
(define (dispatch x)
(define (add-person name)
(set! T (cons (cons name #f) T))
'done)
(define (online? friend)
(eq? friend #t))
(define (toggle-status! name)
(begin ;(if () (set! T (cons (cons name #t) T)))
(if (member name T)
(set-cdr! T (cons (cons name (not (cdr name)) T))))
(if (member name T) #t #f)))
(define (get-online-friends!)
(sort-strings (filter online? T)))
(define (get-friends!)
(sort-strings T))
(cond ((eq? x 'add) add-person)
((eq? x 'toggle-status) toggle-status!)
((eq? x 'get-online-friends) (get-online-friends!))
((eq? x 'get-friends) (get-friends!))
(else (error "Unknown Request" x))))
dispatch))
And these are the test cases that I’m using:
(define my-lst (make-facebook-list))
(display ((my-lst 'add) "Francis"))(newline) ; should return 'done
(display ((my-lst 'add) "Adrian"))(newline) ; should return 'done
(display ((my-lst 'add) "Zule"))(newline) ; should return 'done
(display ((my-lst 'add) "Geralt"))(newline) ; should return 'done
(display ((my-lst 'add) "Dexter"))(newline) ; should return 'done
(display ((my-lst 'add) "Leonidas"))(newline) ; should return 'done
(display ((my-lst 'toggle-status) "Leonidas"))(newline) ; should return #t
(display ((my-lst 'toggle-status) "Francis"))(newline) ; should return #t
(display ((my-lst 'toggle-status) "Zule"))(newline) ; should return #t
(display ((my-lst 'toggle-status) "Xavier"))(newline) ; should return #f
(display (my-lst 'get-online-friends))(newline) ; should return ("Francis" "Leonidas" "Zule")
(display (my-lst 'get-friends)) ; should return ("Adrian" "Dexter" "Francis" "Geralt" "Leonidas" "Zule")
I know that they won’t all work right now, which is why I commented out some of the procedures. The one I’m trying to get to run correctly is the the procedure that toggles the status to online or offline. I’ve been led to believe that “member” is used to check if a certain value is a part of a list.A m I going about this the wrong way?
NOTE: the sort-strings procedure is meant to be used with the procedures that return a list of friends, so as to keep them in alphabetical order.
Your friend is
begin, which forces the following expressions to be evaluated sequentially:Edit