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

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
  • Scheme also has the "character" datatype which is notated as #\a, #\b, #\c and so on. (list-string '(#\a #\b #\c)) gives "abc". Commented Oct 27, 2010 at 17:09

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.