views:

30

answers:

1
(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))

So I have a struct for something I will call "binding" and a list which holds all the "bindings" I created. Now for the question: lets say I wanted to create a list which held the letter in each Binding that has the same number as I call a function with. For example:

;;-----------------------------------------------------------------------------
;; 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) "")

I hope this makes sense the way I explained it.. I've been struggling with this for hours and I feel like it is really simple I just don't understand the language enough.

+1  A: 

Something like that (I cannot test it right now, but the idea should be clear):

(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))

This one will return a list of letters. You will have to sort them and join to string yourself :)

Draco Ater