0

在过去的几天里,我一直在为一个计划问题绞尽脑汁,该问题涉及创建一个带有消息传递的过程来跟踪朋友列表,然后可以操纵该列表以将特定朋友的值更改为在线或离线。如果放入的人是朋友,则返回 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 过程旨在与返回朋友列表的过程一起使用,以保持它们按字母顺序排列。

4

1 回答 1

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))
于 2012-12-09T19:49:15.217 回答