在过去的几天里,我一直在讨论计划问题,包括创建一个消息传递过程,跟踪朋友列表,然后可以操纵该列表来更改特定朋友的价值在线或离线。如果放入的人是朋友,则返回true。如果它们尚未添加,则返回false。它还可以显示在线朋友列表,以及您拥有的另一个列表。基本上,这个想法是制作一个可变的Facebook追踪器。它响应4个输入:'add',toggle-status,'get-online-friends,'get-friends。
这是我迄今为止所做的:
(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))
这些是我正在使用的测试用例:
(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")
我知道他们现在不会全部工作,这就是为什么我评论了一些程序。我试图正确运行的是将状态切换为联机或脱机的过程。我被引导相信“成员”用于检查某个值是否属于列表的一部分。我是否以错误的方式进行此操作?
注意:sort-strings过程旨在与返回朋友列表的过程一起使用,以便按字母顺序保留它们。
答案 0 :(得分:2)
您的朋友是begin
,它会强制按顺序评估以下表达式:
(define (make-facebook-list)
(define T '())
(define (dispatch x)
(lambda (fl)
(cond ((eq? x 'add)
(begin
(set! T (append T (list fl)))
'done))
(else 'invalid))))
dispatch)
scheme@(guile-user)> (define my-lst (make-facebook-list))
scheme@(guile-user)> ((my-lst 'add) "Francis")
$1 = done
修改强>
(define (make-facebook-account)
(let ((*facebook-friends* '()))
(define (sort-alphabetically sequence)
(sort-list sequence string<?))
(define (friend name)
(assoc name *facebook-friends*))
(define (friends-name friend)
(car friend))
(define (friends-status friend)
(cdr friend))
(define (online? friend)
(eq? (friends-status friend) #t))
(define (friend-on-facebook? name)
(if (member (friend name)
*facebook-friends*)
#t #f))
(define (add-person! name)
(begin
(set! *facebook-friends*
(cons (cons name #f)
*facebook-friends*))
'done))
(define (toggle-status! name)
(if (friend-on-facebook? name)
(begin
(set-cdr! (friend name)
(if (online? (friend name))
#f #t))
'done)
(error
"Person no friend on facebook -- TOGGLE-STATUS!" name)))
(define (get-friends table)
(sort-alphabetically
(map friends-name table)))
(define (get-online-friends table)
(get-friends
(filter online? table)))
(define (dispatch m)
(cond ((eq? m 'add!) add-person!)
((eq? m 'toggle-status!) toggle-status!)
((eq? m 'get-friends)
(get-friends *facebook-friends*))
((eq? m 'get-online-friends)
(get-online-friends *facebook-friends*))
((eq? m 'friend-on-facebook?) friend-on-facebook?)
(else
(error "Unknown request -- DISPATCH" m))))
dispatch))