0

I have this list

 (define masterList(list redApple chickenLeg porkLoin milkD baguetteBread orangeJuice beanCan))

I am trying to make a function called lookup that will take an integer and return the element from the list that matches that integer. The list contains elements from a structure in this format

(define-struct storeItem (id des cost))

So I will be passing an integer that represents id and I want storeItem to be retuned.

For instance -

(define redApple (make-storeItem 0 "red delicious apple" 1.99))

If I searched masterList and passed it 0, I would expect redApple to be returned.

Any help on the syntax?

(define (contains masterList x)
(cond 
    ((null? masterList) #f)
    ((eq? (car masterList) x) #t)
    (else (contains (cdr masterList) x))))

This is what I am trying to make work.

It returns true/false correctly based on what I pass it.

(contains materList redApple) returns true.

How can I modify this to return redApple if 0 is entered?

;; These are the constructors to make the elements in our structure
   (define redApple (make-storeItem 0 "red delicious apple" 1.99))
   (define chickenLeg (make-storeItem 1 "boned chicken" 2.99))
   (define porkLoin (make-storeItem 2 "processed pork" 4.99))
   (define milkD (make-storeItem 3 "vitamin d milk" 3.99))
   (define baguetteBread (make-storeItem 4 "french bread" 0.99))
   (define orangeJuice (make-storeItem 5 "fruit juice drink)" 1.49))
   (define beanCan (make-storeItem 6 "beans in a can" 2.49)


;; Creating a list that we will use as our master list which contains elements from our structure
(define masterList(list redApple chickenLeg porkLoin milkD baguetteBread orangeJuice beanCan))
Jonnyutah
  • 37
  • 4

1 Answers1

0

Your general structure is correct, but you're not testing the correct condition with

(eq? (car masterList) x)

You don't want to compare x with the whole element of the list, just with its id component. So that should be:

(= (storeItem-id (car masterList)) x)

Also, to compare numbers you should use = or eqv? rather than eq?. See What is the difference between eq?, eqv?, equal?, and = in Scheme?

If you want to get the object with the ID, rather than just #true or #false, return (car masterList) when the ID matches.

(define (find-storeItem masterList x)
  (cond ((null masterList) #false)
        ((= (storeItem-id (car masterList)) x) (car masterList))
        (else (find-storeItem (cdr masterList) x))))
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you for the link, it actually helped a lot. Whenever I run with your suggested code I get car: arity mismatch; the expected number of arguments does not match the given number expected: 1 given: 2 arguments...: – Jonnyutah Oct 15 '18 at 08:04
  • I had a missing parenthesis, I guess you fixed that the wrong way. – Barmar Oct 15 '18 at 08:10
  • Which parenthesis are missing? – Jonnyutah Oct 15 '18 at 17:47
  • The second `)` in `(storeItem-id (car masterList))` – Barmar Oct 15 '18 at 17:54
  • Thank you I do see where it was wrong. My code runs now but it seems my list is just 7 elements called #, any idea? I will edit my original code to show my constructors and list. – Jonnyutah Oct 15 '18 at 17:59
  • The question says it's a list of structures, not a list of item numbers. – Barmar Oct 15 '18 at 17:59
  • Basically it is a list of structure objects. So I want to perform functions like getid, gettotalcost, ect by navigating the list to find these desired attributes. In the function I am currentyl working on, it would be entering the id number and getting the item it goes to. So 0 would return redApple. – Jonnyutah Oct 15 '18 at 18:02
  • Your function just returns `#true` or `#false` depending on whether the ID is found. If you want to actually return the object, return `(car masterList)` instead of `#true`. – Barmar Oct 15 '18 at 18:04
  • That is correct, I was using #t and #f to see if it was working correctly, I apologize I did not make that clear originally. I want to input a number and return the corresponding grocery item that has the input as its id number. However at this point I think I may have created my list incorrectly since all the items in the list are # instead of redApple, chickenLeg...ect. – Jonnyutah Oct 15 '18 at 18:06