neomang
neomang

Reputation: 149

Scheme object is not applicable - 2 different ways

I am writing some code for a roster management program in scheme, but encounter an issue when I try to add a student. I am using a roster parameter which is a list of lists, each sublist being a student's record. The first student I add works without issue, however when I attempt to add a second student, I receive one of two instances of the same error.

If I try to enter a student who should be added, I get the error :

The object ("21" "Anon Ymous" "89") is not applicable.

If the student I add has information that conflicts with the existing student, I get the error :

The object (("23" "Anon Ymous" "11")) is not applicable.

The code for this section is as follows :

(define addifnotcontains
    (lambda (roster item)
        (cond ((null? roster) (list item))
            ((equal? (car (car roster)) (car item))
                (begin
                    (display "\tID Already Exists\n")
                    (roster)
                ))
            ((equal? (cadr (car roster)) (cadr item)) 
                (begin
                    (display "\tName Already Exists\n")
                    (roster)
                ))
            (else (cons ((car roster) addifnotcontains (cdr roster))))
        )
    )
)

and the call for this function is (menu (addifnotcontains roster (buildobject 0 '()))) where menu is a lambda function that simply takes in a roster

I know that this issue has been caused by mismatched or misplaced parentheses in my code previously, but I cannot tell why it is occurring this time. I suspect it has something to do with calling (roster) at the end of the begin block, but as I understand it, the begin block returns the last value called within the block. What am I doing wrong?

Upvotes: 2

Views: 95

Answers (1)

molbdnilo
molbdnilo

Reputation: 66459

In the first case, you're trying to apply (car roster) to addifnotcontains and (cdr roster):

(cons ((car roster) addifnotcontains (cdr roster)))
      ^   <--    This is one expression   -->    ^ 

You're also only passing one argument to cons and forgetting to pass along item.

This should be

(cons (car roster) (addifnotcontains (cdr roster) item))

In the second case, you're trying to apply roster as a function.

You're correct in that the value of a begin block is the value of the last expression in the block, but in your case this expression should be roster, not (roster).
(Remember that you can't add parentheses willy-nilly in Scheme like you can in some other languages; parentheses are always significant.)

Upvotes: 1

Related Questions