Other Programming Languages
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreOther Programming Languages

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old October 13th, 2006, 03:59 PM
Laurentvw Laurentvw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2006
Posts: 1 Laurentvw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 29 sec
Reputation Power: 0
Scheme: calculating binary

Hey

I'm having a little problem while trying to make a piece of code that can turn a number into its binary code.

(define (displ-as-bin n)
(cond ((< n 1) (display " "))
((even? n) ((displ "0")(displ-as-bin (quotient n 2))))
(else ((display "1")(displ-as-bin (quotient n 2))))))

When trying it out, it gives me the correct answer, followed by an error, that I can't seem to fix!

For example:

(displ-as-bin 8 )
0001 . procedure application: expected procedure, given: #<void>; arguments were: #<void>

I can't figure out why I'm getting this error, so if anyone with a bit of Scheme knowledge could help me out here, it would be very much appreciated. Thanks =)

Reply With Quote
  #2  
Old October 13th, 2006, 07:31 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Hey,

You're trying to apply the return value of display which is #<void>. You can fix the code very simply by removing the brackets that are around this (to stop the application).

Here is the fixed code:

scheme Code:
Original - scheme Code
    (define (display-as-binary n)   (cond ((>= n 1)          (display (if (even? n)                       0                       1))          (display-as-binary              (quotient n 2)))))


Notice how display and display-as-binary are both grouped within the conditions body automatically, there is no need for extra brackets .

Take care,

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
  #3  
Old October 14th, 2006, 03:14 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,082 Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Schol-R-LEA User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 4 Days 10 h 16 m 50 sec
Reputation Power: 446
Conversely, you could use (begin) to allow mutiple lines of code on a single (if) statement:
scheme Code:
Original - scheme Code
    (define (display-as-binary n)   (if (>= n 1)       (begin         (display-as-binary          (quotient n 2))         (display (if (even? n)                      0                      1)))))


(Note that this prints the results in the correct order, too... however, it would make still more sense to generalize the code, by returning a string instead of simply displaying the value.)
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreOther Programming Languages > Scheme: calculating binary


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway