From: Jacob Lyles
Subject: Simple Function Problem
Date: 
Message-ID: <1151365684.871479.261100@b68g2000cwa.googlegroups.com>
I'm using Scheme and trying to make a function that will take three
numbers and return the greatest 2 of them. So far I have this:

(define (compare a b)
    (if (> a b)
        a
        b))

(define (2-of-3 a b c)
  ((compare a b) (compare b c)))


But when I run (2-of-3 3 5 6) I get the following error:

"The object 5 is not applicable"

I'm using MIT's Edwin editor on a windows machine working through
chapter 1.1 of Structure and Interpretation of Computer Programming, if
it makes a difference. 

Thanks,
Jacob

From: bradb
Subject: Re: Simple Function Problem
Date: 
Message-ID: <1151366088.913030.304670@m73g2000cwd.googlegroups.com>
Jacob Lyles wrote:
> I'm using Scheme and trying to make a function that will take three
> numbers and return the greatest 2 of them. So far I have this:
>
> (define (compare a b)
>     (if (> a b)
>         a
>         b))
>
> (define (2-of-3 a b c)
>   ((compare a b) (compare b c)))
>
>
> But when I run (2-of-3 3 5 6) I get the following error:
>
> "The object 5 is not applicable"
>
> I'm using MIT's Edwin editor on a windows machine working through
> chapter 1.1 of Structure and Interpretation of Computer Programming, if
> it makes a difference.
>
> Thanks,
> Jacob

The line "((compare a b) (compare b c)))" is wrong.  Scheme (and Lisp)
evaluate left to right, so this line becomes
((compare 3 5) (compare 5 6))
(5 6) -> tries to call "5"

You may want to change the line to
(list (compare a b) (compare b c))
which will return a 2 element list.

Cheers
Brad
From: vanekl
Subject: Re: Simple Function Problem
Date: 
Message-ID: <1151374978.297595.232760@y41g2000cwy.googlegroups.com>
bradb wrote:
> Jacob Lyles wrote:
> > I'm using Scheme and trying to make a function that will take three
> > numbers and return the greatest 2 of them. So far I have this:
> >
> > (define (compare a b)
> >     (if (> a b)
> >         a
> >         b))
> >
> > (define (2-of-3 a b c)
> >   ((compare a b) (compare b c)))
> >
> >
> > But when I run (2-of-3 3 5 6) I get the following error:
> >
> > "The object 5 is not applicable"
> >
> > I'm using MIT's Edwin editor on a windows machine working through
> > chapter 1.1 of Structure and Interpretation of Computer Programming, if
> > it makes a difference.
> >
> > Thanks,
> > Jacob
>
> The line "((compare a b) (compare b c)))" is wrong.  Scheme (and Lisp)
> evaluate left to right, so this line becomes
> ((compare 3 5) (compare 5 6))
> (5 6) -> tries to call "5"
>
> You may want to change the line to
> (list (compare a b) (compare b c))
> which will return a 2 element list.
> 
> Cheers
> Brad

But not necessarily a correct answer.
Lou
From: bradb
Subject: Re: Simple Function Problem
Date: 
Message-ID: <1151378141.331176.122190@p79g2000cwp.googlegroups.com>
vanekl wrote:
> bradb wrote:
> > Jacob Lyles wrote:
> > > I'm using Scheme and trying to make a function that will take three
> > > numbers and return the greatest 2 of them. So far I have this:
> > >
> > > (define (compare a b)
> > >     (if (> a b)
> > >         a
> > >         b))
> > >
> > > (define (2-of-3 a b c)
> > >   ((compare a b) (compare b c)))
> > >
> > >
> > > But when I run (2-of-3 3 5 6) I get the following error:
> > >
> > > "The object 5 is not applicable"
> > >
> > > I'm using MIT's Edwin editor on a windows machine working through
> > > chapter 1.1 of Structure and Interpretation of Computer Programming, if
> > > it makes a difference.
> > >
> > > Thanks,
> > > Jacob
> >
> > The line "((compare a b) (compare b c)))" is wrong.  Scheme (and Lisp)
> > evaluate left to right, so this line becomes
> > ((compare 3 5) (compare 5 6))
> > (5 6) -> tries to call "5"
> >
> > You may want to change the line to
> > (list (compare a b) (compare b c))
> > which will return a 2 element list.
> >
> > Cheers
> > Brad
>
> But not necessarily a correct answer.
> Lou

Yes, (2-of-3 1 5 2) will return (5 5)

Brad
From: Hrvoje Blazevic
Subject: Re: Simple Function Problem
Date: 
Message-ID: <e7qn84$op$1@ss408.t-com.hr>
bradb wrote:
> 
> The line "((compare a b) (compare b c)))" is wrong.  Scheme (and Lisp)
> evaluate left to right, so this line becomes
> ((compare 3 5) (compare 5 6))
> (5 6) -> tries to call "5"

The end result is correct, but the statement that Scheme (and Lisp)
evaluate left to right is not. I do not know about CL, but Scheme
standard does not specify left to right evaluation.

-- Hrvoje
From: Pascal Bourguignon
Subject: Re: Simple Function Problem
Date: 
Message-ID: <87k6733uaw.fsf@thalassa.informatimago.com>
Hrvoje Blazevic <······@despammed.com> writes:

> bradb wrote:
>> 
>> The line "((compare a b) (compare b c)))" is wrong.  Scheme (and Lisp)
>> evaluate left to right, so this line becomes
>> ((compare 3 5) (compare 5 6))
>> (5 6) -> tries to call "5"
>
> The end result is correct, but the statement that Scheme (and Lisp)
> evaluate left to right is not. I do not know about CL, but Scheme
> standard does not specify left to right evaluation.

Common Lisp specifies the evaluation from left to right, in most cases.

   (let ((i 0)) (list (incf i) (incf i) (incf i)))

returns always the same list in Common Lisp.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
From: bradb
Subject: Re: Simple Function Problem
Date: 
Message-ID: <1151427615.939651.208220@u72g2000cwu.googlegroups.com>
Hrvoje Blazevic wrote:
> bradb wrote:
> >
> > The line "((compare a b) (compare b c)))" is wrong.  Scheme (and Lisp)
> > evaluate left to right, so this line becomes
> > ((compare 3 5) (compare 5 6))
> > (5 6) -> tries to call "5"
>
> The end result is correct, but the statement that Scheme (and Lisp)
> evaluate left to right is not. I do not know about CL, but Scheme
> standard does not specify left to right evaluation.
>
> -- Hrvoje

You're right.  CL does specify that a conforming implementation will
evaluate left to right.  And now that I think about it Scheme does not
specify it either way.  I think I read a rant by somebody who said
"Most Schemes do eval left to right, but it's not in the spec - so you
can't rely on it."

Cheers
Brad
From: Karstens Rage
Subject: Re: Simple Function Problem
Date: 
Message-ID: <6qCdnZEIYMFlAT3ZnZ2dnUVZ_rqdnZ2d@comcast.com>
Jacob Lyles wrote:
> I'm using Scheme and trying to make a function that will take three
> numbers and return the greatest 2 of them. So far I have this:
> 
> (define (compare a b)
>     (if (> a b)
>         a
>         b))
> 
> (define (2-of-3 a b c)
>   ((compare a b) (compare b c)))
> 
> 
> But when I run (2-of-3 3 5 6) I get the following error:
> 
> "The object 5 is not applicable"
> 
> I'm using MIT's Edwin editor on a windows machine working through
> chapter 1.1 of Structure and Interpretation of Computer Programming, if
> it makes a difference. 
> 
> Thanks,
> Jacob
> 

I learn by looking at the original post and seeing if I can figure out
the answer before the experts chime in. This is an amazing resource if
you ask the right questions.

I wrote:
(defun greatest (&rest args) (car (sort args #'>)))

The only Scheme I have is LispMe on the PalmOS. I don't know if it has
'sort'

k

-- 
http://karstensrage.blogspot.com/
Rants and raves about pretty much anything I happen to be thinking about
From: Rainer Joswig
Subject: Re: Simple Function Problem
Date: 
Message-ID: <C0C63FED.434ED%joswig@lisp.de>
Am 27.06.2006 1:48 Uhr schrieb "Jacob Lyles" unter <···········@gmail.com>
in ························@b68g2000cwa.googlegroups.com:

> I'm using Scheme and trying to make a function that will take three
> numbers and return the greatest 2 of them. So far I have this:
> 
> (define (compare a b)
>     (if (> a b)
>         a
>         b))
> 
> (define (2-of-3 a b c)
>   ((compare a b) (compare b c)))
> 
> 
> But when I run (2-of-3 3 5 6) I get the following error:
> 
> "The object 5 is not applicable"
> 
> I'm using MIT's Edwin editor on a windows machine working through
> chapter 1.1 of Structure and Interpretation of Computer Programming, if
> it makes a difference.
> 
> Thanks,
> Jacob
> 

Hmm, I think that you are in the wrong newsgroup. But then, I think
there is no newsgroup in the comp.lang.* hierarchy for homework.

But, what was your question btw.?
From: Jacob Lyles
Subject: Re: Simple Function Problem
Date: 
Message-ID: <1151371239.968927.87400@u72g2000cwu.googlegroups.com>
My apologies. I've been learning Lisp and Sceme concurrently and I need
to learn to keep them separate.

And it's not homework, though I understand how you might think that
since I asked the first difficult question out of a popular Scheme
text. However, it is merely my own pathetic attempt at self-teaching. I
know I'm asking a lot of dumb questions, but by doing so I've been
directed to some great resources so I won't have to ask as many in the
future.


Rainer Joswig wrote:
> Am 27.06.2006 1:48 Uhr schrieb "Jacob Lyles" unter <···········@gmail.com>
> in ························@b68g2000cwa.googlegroups.com:
>
> > I'm using Scheme and trying to make a function that will take three
> > numbers and return the greatest 2 of them. So far I have this:
> >
> > (define (compare a b)
> >     (if (> a b)
> >         a
> >         b))
> >
> > (define (2-of-3 a b c)
> >   ((compare a b) (compare b c)))
> >
> >
> > But when I run (2-of-3 3 5 6) I get the following error:
> >
> > "The object 5 is not applicable"
> >
> > I'm using MIT's Edwin editor on a windows machine working through
> > chapter 1.1 of Structure and Interpretation of Computer Programming, if
> > it makes a difference.
> >
> > Thanks,
> > Jacob
> >
>
> Hmm, I think that you are in the wrong newsgroup. But then, I think
> there is no newsgroup in the comp.lang.* hierarchy for homework.
> 
> But, what was your question btw.?
From: Pascal Bourguignon
Subject: Re: Simple Function Problem
Date: 
Message-ID: <87zmfz499o.fsf@thalassa.informatimago.com>
"Jacob Lyles" <···········@gmail.com> writes:

> My apologies. I've been learning Lisp and Sceme concurrently and I need
> to learn to keep them separate.
>
> And it's not homework, though I understand how you might think that
> since I asked the first difficult question out of a popular Scheme
> text. However, it is merely my own pathetic attempt at self-teaching. I
> know I'm asking a lot of dumb questions, but by doing so I've been
> directed to some great resources so I won't have to ask as many in the
> future.

That said, this is not a lisp question.
This is an algorithm question.

You need to build an algorithm that takes three values, and return the
two highest values in the three.

For example, I give you three equal-sized boxes, and you must give me
back the two heaviest boxes.  How do you proceed?

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
From: Tim X
Subject: Re: Simple Function Problem
Date: 
Message-ID: <87sllo2vq5.fsf@tiger.rapttech.com.au>
"Jacob Lyles" <···········@gmail.com> writes:

> I'm using Scheme and trying to make a function that will take three
> numbers and return the greatest 2 of them. So far I have this:
>
> (define (compare a b)
>     (if (> a b)
>         a
>         b))
>
> (define (2-of-3 a b c)
>   ((compare a b) (compare b c)))
>
>
> But when I run (2-of-3 3 5 6) I get the following error:
>
> "The object 5 is not applicable"
>
> I'm using MIT's Edwin editor on a windows machine working through
> chapter 1.1 of Structure and Interpretation of Computer Programming, if
> it makes a difference. 
>

Warning - not that familiar with scheme. However, I think you need to
look at the definition of 2-of-3. What are yu expecting to get
returned from that function? As a hint, what would happen if you
replaced the two calls to compare with their results? Is that a valid
scheme statement? (try executing it and I think you will see the
problem).

Tim

-- 
tcross (at) rapttech dot com dot au
From: Stefan Mandl
Subject: Re: Simple Function Problem
Date: 
Message-ID: <4gjadrF1mp2nbU1@news.dfncis.de>
Ok - guess deadline is over now, here's version:

(define (2-of-n . args)
   (let ((sorted (sort args >)))
     (list (car sorted) (cadr sorted))))
From: William James
Subject: Re: Simple Function Problem
Date: 
Message-ID: <1151645183.909129.121430@d56g2000cwd.googlegroups.com>
Jacob Lyles wrote:
> I'm using Scheme and trying to make a function that will take three
> numbers and return the greatest 2 of them. So far I have this:
>
> (define (compare a b)
>     (if (> a b)
>         a
>         b))
>
> (define (2-of-3 a b c)
>   ((compare a b) (compare b c)))
>
>
> But when I run (2-of-3 3 5 6) I get the following error:
>
> "The object 5 is not applicable"
>
> I'm using MIT's Edwin editor on a windows machine working through
> chapter 1.1 of Structure and Interpretation of Computer Programming, if
> it makes a difference.
>
> Thanks,
> Jacob

In newLISP:

(define (two-of-3)
  (rest (sort (args))))

Without sorting:

(define (two-of-3 a b c)
  (list (max a b) (max b c)))
From: Pascal Bourguignon
Subject: Re: Simple Function Problem
Date: 
Message-ID: <87zmfvxk5n.fsf@thalassa.informatimago.com>
"William James" <·········@yahoo.com> writes:
> In newLISP:
> Without sorting:
>
> (define (two-of-3 a b c)
>   (list (max a b) (max b c)))

Whouah!  newLISP is really magical!

(two-of-3 1 3 2) --> (3 2) ; or (2 3) ; from the above code!!! Fantastic!


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
From: Thomas A. Russ
Subject: Re: Simple Function Problem
Date: 
Message-ID: <ymifyhm4h9m.fsf@sevak.isi.edu>
"William James" <·········@yahoo.com> writes:

> Without sorting:
> 
> (define (two-of-3 a b c)
>   (list (max a b) (max b c)))

How about something like this ;-)

  (define (two-of-3 a b c)
    (list (max a b c) 
          (min (max a b c) (max a b) (max a c) (max b c))))


-- 
Thomas A. Russ,  USC/Information Sciences Institute