using set! to change the value of a variable in drscheme

对着背影说爱祢 提交于 2019-12-11 13:57:46

问题


I'm trying to delete an occurrence of some value in a binary search tree. This is what I have so far:

(define removeBin (lambda (x t)
       (cond ((< x (car t)) (removeBin x (cadr t)))
             ((> x (car t)) (removeBin x (caddr t)))
             ((equal? x (car t)) 
                   (if(and (null? (cadr t)) (null? (caddr t))) '()
                (let ((r (replacement t))) ((set! (car t) r) (removeBin r t)))))))) 

It's giving me the following error: set!: not an identifier in: (car t) What does that mean? and how can I fix it so that set! would work?

thank you


回答1:


As the error message explains, (car t) is not a valid identifier, and thus its value cannot be changed.

You need to use set-car! like this:

(set-car! t r)

This changes the car of t to r.




回答2:


In Racket there are "mutable pairs" that you get with mcons, access with mcar and mcdr, and mutate with set-mcar! and set-mcdr!. You can get them using the conventional names if you're using one of the standard scheme languages, for example, by starting your code with #lang r5rs.



来源:https://stackoverflow.com/questions/4416862/using-set-to-change-the-value-of-a-variable-in-drscheme

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!