2010-10-27 29 views
0
(define-struct binding 
    (
    let ; a string 
    num ; a number 
) 
) 
(define Bind-A (make-binding empty 1)) 
(define Bind-B (make-binding "A" 2)) 
(define Bind-C (make-binding "F" 1)) 
(define Bind-D (make-binding "A" 1)) 
(define Bind-E (make-binding "C" 1)) 
(define Bind-F (make-binding "E" 3)) 
(define Bind-All (list Bind-A Bind-B Bind-C Bind-D Bind-E Bind-F)) 

所以我有一個結構體,我將調用「綁定」和一個列表,它包含了我創建的所有「綁定」。現在提出這個問題:可以說我想創建一個列表,它在每個綁定中都有一個與我調用函數相同的數字。例如:計劃幫助。構造列表和遞歸

;;----------------------------------------------------------------------------- 
;; Return a string containing the letters (in alphabetical order, separated by a 
;; space) of all bindings with the same number in pool of "bindings". 
;; If either letter is unknown or if no bindings have the same number 
;; Return the null string (""). 
;;----------------------------------------------------------------------------- 
(define (same-num ; string 
    which-binding) ; binding to check 
    pool    ; list of all bindings 
    ) 
    (cond 
     [(empty? (binding-let which-binding)) ""] 
     [(equal? (binding-let which-binding) (binding-let (first pool)) ... ] 
     [else ... ] 
    ) 
) 
(check-expect (same-num Bind-E Bind-all) "A F") 
(check-expect (same-num Bind-F Bind-all) "") 
(check-expect (same-num Bind-A Bind-all) "") 

我希望這是有道理的,我解釋的方式。我一直是這樣努力了幾個小時,我覺得這是非常簡單的我只是不明白的語言不夠。

+0

Scheme還具有「字符」數據類型,它被標記爲#\ a,#\ b,#\ c等等。 (list-string'(#\ a#\ b#\ c))給出「abc」。 – erjiang 2010-10-27 17:09:39

回答

1

類似的東西(我現在不能測試它,但這個想法應該清楚):

(define (same-num which-binding pool) 
    (define (iter which lst result) 
    (cond 
     ((null? (binding-let which-binding)) result) 
     ((null? lst) result) 
     ((equal? (binding-let which-binding) 
       (binding-let (car lst))) 
        (iter which (cdr lst) (cons (binding-let (car lst)) result))) 
     (else (iter which (cdr lst) result)))) 

    (iter which-binding pool null)) 

這一次將返回字母列表。您必須對它們進行排序並加入以便自己編寫字符串:)