943,882 Members | Top Members by Rank

Ad:
Feb 10th, 2008
0

Scheme empty list help

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cknapp is offline Offline
16 posts
since Feb 2008
Feb 13th, 2008
0

Re: Scheme empty list help

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Feb 13th, 2008
0

Re: Scheme empty list 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)
  )
Last edited by cknapp; Feb 13th, 2008 at 9:36 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cknapp is offline Offline
16 posts
since Feb 2008
Feb 14th, 2008
0

Re: Scheme empty list help

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 '() ?
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Feb 14th, 2008
0

Re: Scheme empty list help

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cknapp is offline Offline
16 posts
since Feb 2008
Feb 14th, 2008
0

Re: Scheme empty list help

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Feb 20th, 2008
0

Re: Scheme empty list help

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.
Last edited by cknapp; Feb 20th, 2008 at 9:04 pm.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cknapp is offline Offline
16 posts
since Feb 2008
Feb 21st, 2008
0

Re: Scheme empty list help

You are getting lost on syntactic sugar.
(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) lst
with:
(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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Feb 21st, 2008
0

Re: Scheme empty list help

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,
Reputation Points: 10
Solved Threads: 1
Newbie Poster
cknapp is offline Offline
16 posts
since Feb 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Legacy and Other Languages Forum Timeline: solution to my matlab code
Next Thread in Legacy and Other Languages Forum Timeline: GWBASIC and XP Pro





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC