0

Getting an error for my binary search tree that I created in scheme.

$gosh main.sc
*** ERROR: list required, but got 5
Stack Trace:
_______________________________________

This is my code. I think the error has to do with how I am calling the functions, but I am not sure what exactly is wrong. I am calling the insert function with the two required parameters: tree, and a value of 5.

(define (member? t v)
    (cond 
        ((null? t)
            #f
        )
        ((< node (car t))
            (member? (cadr t) v))
        ((> node (car t))
            (member? (caddr t) v))
        (else
            #t
        )
    )
)

(define (insert t v)
    (cond
        ((null? t)
            (list v '() '())
        )
        ((< v (car t))
            (list (car t) (insert (cadr t) v) (caddr t))
        )
        ((>= v (car t)) 
                (list (car t) (cadr t) (insert (caddr t) v))
        )
        (else
            t
        )
    )
)


(define (fold func val lst)
  (if (null? lst) val (fold func (func val (car lst)) (cdr lst))))

(define (build lst)
  (fold (lambda (t v) (insert t v)) '() lst))

(define t (list 10 '() '()))
(insert t 5)

display  (member t 5)

display t
1
  • Can't reproduce in a more mature implementation It's possible that your interpreter chokes on the syntax errors at the end. (Gauche isn't even version 1.0 yet, so bugs should not be surprising.) Commented Nov 11, 2020 at 13:19

1 Answer 1

1

You care calling (member t 5) which is the same as (member '(10 '() '()) 5). Now member is not the same as your defined member? since it has a different name. member is the core library that looks like this:

(define (member obj lst)
  (cond ((null? lst) #f)
        ((equal? obj (car lst)) lst)
        (else (member obj (cdr lst)))))

Your member? has the two parameters swapped so when you miswrote the name and used the report version member 5 is not null, then it will do (car 5) and that will fail miserably. The error message that 5 is not of the required type list is pretty decent. It might spell the beans that it was member that failed though.

Another thing. If you replace the call to member with a call to member? you hit more problems. You use a variable node that is not defined anywhere.

The indentation and placing of parentheses is not goo lisp style. Your code should be written like this:

;; node doesn't exist in OPs code, but my implementation doesn't like member? without it
(define node 5)

;; possible typo by using the variable node ?
(define (member? t v)
  (cond
    ((null? t)
     #f)
    ((< node (car t))
     (member? (cadr t) v))
    ((> node (car t))
     (member? (caddr t) v))
    (else
     #t)))

(define (insert t v)
  (cond
    ((null? t)
     (list v '() '()))
    ((< v (car t))
     (list (car t) (insert (cadr t) v) (caddr t)))
    ((>= v (car t))
     (list (car t) (cadr t) (insert (caddr t) v)))
    (else 
     t)))


(define (fold func val lst)
  (if (null? lst) val (fold func (func val (car lst)) (cdr lst))))

(define (build lst)
  (fold (lambda (t v) (insert t v)) '() lst))

(define t (list 10 '() '()))
(insert t 5)

;; NB doesn't call a procedure, just evaluates it. 
display
;; Here the arguments are the wrong order and you don't use memeber?
(member t 5)
;; NB doesn't call a procedure, just evaluates it. 
display
t
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.