School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,215 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,567 people online right now. Registration is fast and FREE... Join Now!




Finding minimum using scheme

 

Finding minimum using scheme

adfergu2

2 Nov, 2009 - 10:31 PM
Post #1

New D.I.C Head
*

Joined: 26 Oct, 2009
Posts: 4

INSTRUCTIONS!
Create a Scheme procedure find_min2 that reads 2 numbers from the keyboard, and returns the minimum of two. Do not use the "min" primitive, use "if" instead to do this
For examples, if user enters:
12 -34
then your procedure should return
-34
Include the line:
(display (find_min2 (read) (read)))
at the end of your scheme file so that it will read two numbers and display the output.

Here is my code, it compiles and then prints the inputed values in order disreguarding the "if" statement. Im new to this so please help.

CODE

(define (find_min2 x y)  
  (if (> x y)
         ( y
          x
          )
  (display(find_min2(read)(read)))        
) )


User is offlineProfile CardPM
+Quote Post


William_Wilson

RE: Finding Minimum Using Scheme

3 Nov, 2009 - 04:21 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,668



Thanked: 97 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
It has been a while since I've worked with scheme, but I think it should look more like this:
CODE

(define (find_min2 x y)  
  (if (> x y)
         ( y
          x
          )        
) )
(display(find_min2(read)(read)))

with the display call outside of the find_min2 definition.
User is offlineProfile CardPM
+Quote Post

adfergu2

RE: Finding Minimum Using Scheme

3 Nov, 2009 - 06:11 PM
Post #3

New D.I.C Head
*

Joined: 26 Oct, 2009
Posts: 4

when I try it your way, it does not compile correctly, and ends up saying "procedure application: expected procedure, given: 21; arguments were: 39" when I run it inputting 21 and 39.



User is offlineProfile CardPM
+Quote Post

Kiriran

RE: Finding Minimum Using Scheme

4 Nov, 2009 - 07:28 AM
Post #4

D.I.C Head
**

Joined: 11 Apr, 2007
Posts: 54


My Contributions
CODE
(define (find_min2 x y)
  (if (> x y)
      y; true
      x; false
      ))
(display (find_min2 (read) (read)))

you don't need the (). In fact if you use (y x) you return (y x) if true and nothing for false
User is offlineProfile CardPM
+Quote Post

adfergu2

RE: Finding Minimum Using Scheme

4 Nov, 2009 - 10:29 AM
Post #5

New D.I.C Head
*

Joined: 26 Oct, 2009
Posts: 4

wow thanks that did work.
So if i wanted to extend this program to take in numbers from the keyboard until 0 was read, and display the minimum of all of those, I have this so far, but it just prints everything out.
CODE

(define (find_min x min)
                  (if(number? x)
                
                     (if( = x 0)
                       min; true
                       x;false
                       ))
  )                          
                                
  (display(find_min(read)))


thanks for your help!

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 10:03PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month