Scheme empty list help

Please support our Legacy and Other Languages advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Feb 2008
Posts: 16
Reputation: cknapp is an unknown quantity at this point 
Solved Threads: 1
cknapp cknapp is offline Offline
Newbie Poster

Scheme empty list help

 
0
  #1
Feb 10th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Scheme empty list help

 
0
  #2
Feb 13th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: cknapp is an unknown quantity at this point 
Solved Threads: 1
cknapp cknapp is offline Offline
Newbie Poster

Re: Scheme empty list help

 
0
  #3
Feb 13th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Scheme empty list help

 
0
  #4
Feb 14th, 2008
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 '() ?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: cknapp is an unknown quantity at this point 
Solved Threads: 1
cknapp cknapp is offline Offline
Newbie Poster

Re: Scheme empty list help

 
0
  #5
Feb 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Scheme empty list help

 
0
  #6
Feb 14th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: cknapp is an unknown quantity at this point 
Solved Threads: 1
cknapp cknapp is offline Offline
Newbie Poster

Re: Scheme empty list help

 
0
  #7
Feb 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Scheme empty list help

 
0
  #8
Feb 21st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 16
Reputation: cknapp is an unknown quantity at this point 
Solved Threads: 1
cknapp cknapp is offline Offline
Newbie Poster

Re: Scheme empty list help

 
0
  #9
Feb 21st, 2008
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,
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Legacy and Other Languages Forum


Views: 3646 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Legacy and Other Languages
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC