Reputation: 107
I am receiving two errors and im clearly not understanding something here. The first comes when the inputs are both sets. This is the error it gives me:
Error: #t is not a function [subset?, (anon)]
The second comes when A or S is not a set and it his the else statement. I get this:
Error: execute: unbound symbol: "else" [subset?, is-set?]
My code is as follows:
(define (is-set? L)
(if (list? L)
(cond
((null? L) #t)
((member (car L)(cdr L)) #f)
(else (is-set? (cdr L)))
)
(else #f)
)
)
(define (subset? A S)
(if ((is-set? A) and (is-set? S))
(cond
((null? A) #t)
((member (car A) S)
(subset? (cdr A) S))
(else #f))
(else #f))
)
(subset? '(1 3 5) '(1 2 3 5 6))
;;(subset? '(1 3 5) 1)
The is-set? function works and has the exact same syntax "((null? L) #t)" as "((null? A) #t)" Does it have something to do with the fact that subset? takes two parameters?
I am testing this on https://repl.it/languages/scheme im not sure if that would make a difference either.
thanks
Upvotes: 0
Views: 538
Reputation: 370132
((is-set? A) and (is-set? S))
Scheme doesn't have infix operators. (exp exp*)
is the syntax for a function call where the first expression evaluates to a function, which is then called with the values of the other expressions as arguments.
So the above is interpreted as a function call where (is-set? A)
is the function and and
and (is-set? S)
are the arguments. The first problem with that is that (is-set? A)
isn't actually a function (it's a Boolean), which is what the error message is telling you.
(else #f)
if
does not use an else
keyword. The else case of an if
is simply given as the second expression to if
. That is, instead of (if condition exp1 (else exp2))
, you should write (if condition exp1 exp2)
.
The reason that you don't run into the same error with your first function is that you never call is-set?
with a non-list, so it never reaches the else-case and never finds the error.
Upvotes: 1