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:

;;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

Recommended Answers

All 8 Replies

That shouldn't work at all. Both upgraph and downgraph are defined to take a single argument, but you are passing multiple...

That could be the problem. Make sure also that you aren't calling bitset with '().

Sorry I can't be of more help.

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...

(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)
  )

The same problem?

I see nothing wrong with your code... (I can't test it myself ATM, as I'm just recovering from a system wipe and have yet to reinstall scheme.)

Do you think it might be barfing because upgraph evaluates to '() ?

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.

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: (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.

Sorry, I'm not fully following you. I understand what you're saying about ('() '()) being unintelligible for the interpreter, but how do I rectify this?

Thanks again.

You are getting lost on syntactic sugar. ([B]define[/B] (foo x y z) (+ (- x y) z)) is sugar for: ([B]define[/B] foo ([B]lambda[/B] (x y z) (+ (- x y) z))) Likewise, inside the body of a lambda, using ([B]define[/B] (bar q) (* q (+ q 1))) ... is sugar for: ([B]letrec[/B] ((bar ([B]lambda[/B] (q) (* q (+ q 1))))) ...) What I suggest is replacing the lines: ([B]if[/B] ([B]null?[/B] lst) lst with: ([B]if[/B] ([B]not[/B] ([B]null?[/B] lst)) and see if that works. (I'm not sure if it will or not...)

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,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.