| | |
Scheme empty list help
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 16
Reputation:
Solved Threads: 1
I'm working on an implementation of Conway's Game of Life as a way to learn Scheme, and I'm having trouble with the updater. I keep getting the message "The object () is not applicable" when I try to use the following. I could be missing something simple, and any advice on my code is appreciated:
Thanks,
Cory
;;bitset and bitclear are just bit-string-set! and bit-string-clear! renamed
(define (update graph u d)
(define (upgraph lst)
(if (null? lst) lst
(begin
(bitset graph (car lst))
(upgraph graph cdr lst)
)
)
)
(define (downgraph lst)
(if (null? lst) lst
(begin
(bitclear graph (car lst))
(downgraph graph (cdr lst))
)
)
)
(upgraph u)
(downgraph d)
)Thanks,
Cory
•
•
Join Date: Feb 2008
Posts: 16
Reputation:
Solved Threads: 1
Ah, yes... I believe the word is "embarrassing"...
There are time when I feel rather blind and stupid. This is one of them.
I don't think I'd be calling bitset with '(), as a null list input would just return a null list. But that assumes I allowed it to take another argument....
Alright still having problems...
There are time when I feel rather blind and stupid. This is one of them.
I don't think I'd be calling bitset with '(), as a null list input would just return a null list. But that assumes I allowed it to take another argument....
Alright still having problems...
(define (update graph u d)
(define (upgraph lst)
(if (null? lst) lst
(begin
(bit-set graph (car lst))
(upgraph (cdr lst))
)
)
)
(define (downgraph lst)
(if (null? lst) lst
(begin
(bit-clear graph (car lst))
(downgraph (cdr lst))
)
)
)
(upgraph u)
(downgraph d)
) Last edited by cknapp; Feb 13th, 2008 at 9:36 pm.
•
•
Join Date: Feb 2008
Posts: 16
Reputation:
Solved Threads: 1
Yes, same problem.
I was thinking that may be the case... but if that's so, I'd really love to know why. It wouldn't make any sense: the empty list is just like any other piece of data.
I was also thinking it may have something to do with the (begin) statement. I'd elaborate more on this, but I've spent the last 7 hours learning LaTeX and using it to writing a lab report that's due tomorrow, and my brain is fried... Sleep time; I will return to this... probably Friday, as I have a lot to get done tomorrow.
I was thinking that may be the case... but if that's so, I'd really love to know why. It wouldn't make any sense: the empty list is just like any other piece of data.
I was also thinking it may have something to do with the (begin) statement. I'd elaborate more on this, but I've spent the last 7 hours learning LaTeX and using it to writing a lab report that's due tomorrow, and my brain is fried... Sleep time; I will return to this... probably Friday, as I have a lot to get done tomorrow.
A begin is implied in the body of a lambda.
The problem is that you have to be extra careful to keep data and code separate in scheme. You are mixing the two.
For example, the following is not a valid program:
but it is valid data:
Essentially, your update function body at some point evaluates to
...which is not a valid program.
Again, I have yet to re-install PLT so I can't be sure (it has been a few years since I've used scheme much), but you might fix it by simply changing your letrecs to use the following if inside:
That way, the result is nothing, rather than an empty list.
Hope this helps.
The problem is that you have to be extra careful to keep data and code separate in scheme. You are mixing the two.
For example, the following is not a valid program:
(1 2 3 4)but it is valid data:
'(1 2 3 4)Essentially, your update function body at some point evaluates to
('() '())...which is not a valid program.
Again, I have yet to re-install PLT so I can't be sure (it has been a few years since I've used scheme much), but you might fix it by simply changing your letrecs to use the following if inside:
(if (not (null? lst))That way, the result is nothing, rather than an empty list.
Hope this helps.
You are getting lost on syntactic sugar.
is sugar for:
Likewise, inside the body of a lambda, using
is sugar for:
What I suggest is replacing the lines:
with:
and see if that works. (I'm not sure if it will or not...)
(define (foo x y z) (+ (- x y) z))is sugar for:
(define foo (lambda (x y z) (+ (- x y) z)))Likewise, inside the body of a lambda, using
(define (bar q) (* q (+ q 1))) ...is sugar for:
(letrec ((bar (lambda (q) (* q (+ q 1))))) ...)What I suggest is replacing the lines:
(if (null? lst) lstwith:
(if (not (null? lst))and see if that works. (I'm not sure if it will or not...)
Last edited by Duoas; Feb 21st, 2008 at 12:20 am.
•
•
Join Date: Feb 2008
Posts: 16
Reputation:
Solved Threads: 1
Rock!
Syntactic sugar causes cancer of the semi-colon.
I actually had that at one point, but my function call was embarrassing, so I had changed it, hoping it had something to do with my error.
Anyway, yeah... Now I need to remember to filter through the syntactic sugar when I'm stuck.
Thanks again,
Syntactic sugar causes cancer of the semi-colon.
I actually had that at one point, but my function call was embarrassing, so I had changed it, hoping it had something to do with my error.
Anyway, yeah... Now I need to remember to filter through the syntactic sugar when I'm stuck.
Thanks again,
![]() |
Similar Threads
- scheme newbie here!!! (Computer Science)
- Another explorer.exe thread. (Viruses, Spyware and other Nasties)
- Family Tree scheme function (Legacy and Other Languages)
- Dr scheme help -> breaking strings into individual words (Computer Science)
- Scheme combine function help needed (Legacy and Other Languages)
- Dr Scheme Tutorial (Computer Science)
- Identifying Drivers needed (Windows 95 / 98 / Me)
- Lisp help - is this function right? (Legacy and Other Languages)
Other Threads in the Legacy and Other Languages Forum
- Previous Thread: solution to my matlab code
- Next Thread: GWBASIC and XP Pro
| Thread Tools | Search this Thread |






