LARGEST IN A LIST
Author Message
LARGEST IN A LIST

How do you get the largest integer in a list , the code beolow doesnt seem
to work
Any help would be much appreciated

larger(A,B):-
B < A.

largest([],X).

largest(Tail).

largest(Tail).

Sat, 29 May 2004 05:22:41 GMT
LARGEST IN A LIST
You can't call largest(tail) because there is no predicate called largest
that takes 1 argument.  What you need to do is the following:

larger(X,Y,X) :- X >= Y, !.
larger(X,Y,Y).

largest([X],X).

largest([H|T],X) :- largest(T,X2), larger(H, X2, X).

Quote:
> How do you get the largest integer in a list , the code beolow doesnt seem
> to work
> Any help would be much appreciated

> larger(A,B):-
>   B < A.

> largest([],X).

>   largest(Tail).

>   largest(Tail).

Sat, 29 May 2004 23:26:52 GMT
LARGEST IN A LIST

Quote:

> You can't call largest(tail) because there is no predicate called largest
> that takes 1 argument.  What you need to do is the following:
> larger(X,Y,X) :- X >= Y, !.
> larger(X,Y,Y).

huh?

?- larger(4,3,3).

yes

--
Gertjan van Noord Alfa-informatica, RUG,  Postbus 716, 9700 AS Groningen
vannoord at let dot rug dot nl            http://www.let.rug.nl/~vannoord

Sun, 30 May 2004 23:05:48 GMT
LARGEST IN A LIST

Quote:

> > You can't call largest(tail) because there is no predicate called largest
> > that takes 1 argument.  What you need to do is the following:

> > larger(X,Y,X) :- X >= Y, !.
> > larger(X,Y,Y).

> huh?

> ?- larger(4,3,3).

> yes

That should be

larger(X,Y,Z) :- X >= Y, !, Z = X.
larger(X,Y,Y).

--
Andrew Eremin
IC Parc, William Penney Lab.,        Tel: +44 (0)20 7594 8299
Imperial College                     Fax: +44 (0)20 7594 8432

Sun, 30 May 2004 23:32:00 GMT
LARGEST IN A LIST
The Z = X is not needed.  The way I had it was correct, Prolog unifies the
instantiated X if X >= Y.

This is correct!

larger(X,Y,X) :- X >= Y, !.
larger(X,Y,Y) :- X =< Y.

Quote:

> > > You can't call largest(tail) because there is no predicate called
largest
> > > that takes 1 argument.  What you need to do is the following:

> > > larger(X,Y,X) :- X >= Y, !.
> > > larger(X,Y,Y).

> > huh?

> > ?- larger(4,3,3).

> > yes

> That should be

> larger(X,Y,Z) :- X >= Y, !, Z = X.
> larger(X,Y,Y).

> --
> Andrew Eremin
> IC Parc, William Penney Lab.,        Tel: +44 (0)20 7594 8299
> Imperial College                     Fax: +44 (0)20 7594 8432

Mon, 31 May 2004 02:36:23 GMT
LARGEST IN A LIST

Quote:

> The Z = X is not needed.  The way I had it was correct, Prolog unifies the
> instantiated X if X >= Y.

> This is correct!

> larger(X,Y,X) :- X >= Y, !.
> larger(X,Y,Y) :- X =< Y.

Now that you've added the test to the second clause it's correct, but
for what you originally wrote the unification with the head of the first
would fail, the cut would not be reached and unification with the second
clause would succeed.

Quote:

> > > > You can't call largest(tail) because there is no predicate called
> largest
> > > > that takes 1 argument.  What you need to do is the following:

> > > > larger(X,Y,X) :- X >= Y, !.
> > > > larger(X,Y,Y).

> > > huh?

> > > ?- larger(4,3,3).

> > > yes

> > That should be

> > larger(X,Y,Z) :- X >= Y, !, Z = X.
> > larger(X,Y,Y).

--
Andrew Eremin
IC Parc, William Penney Lab.,        Tel: +44 (0)20 7594 8299
Imperial College                     Fax: +44 (0)20 7594 8432

Mon, 31 May 2004 04:59:35 GMT
LARGEST IN A LIST

Quote:

> The Z = X is not needed.  The way I had it was correct, Prolog unifies the
> instantiated X if X >= Y.

the way yioyou had it was incorrect. Andrew's variant is correct.

Quote:
> This is correct!
> larger(X,Y,X) :- X >= Y, !.
> larger(X,Y,Y) :- X =< Y.

and this one is correct too, but is less efficient (not that you'd ever
notice) in that in some cases you check whether X is smaller than Y if
you already established that X is not larger or equal to Y. Seems silly
to me.

of course my prefered solution would use if-then-else construct - but
we've been through that here on this list too often already.

Gj

Quote:

>> > > You can't call largest(tail) because there is no predicate called
> largest
>> > > that takes 1 argument.  What you need to do is the following:

>> > > larger(X,Y,X) :- X >= Y, !.
>> > > larger(X,Y,Y).

>> > huh?

>> > ?- larger(4,3,3).

>> > yes

>> That should be

>> larger(X,Y,Z) :- X >= Y, !, Z = X.
>> larger(X,Y,Y).

--
Gertjan van Noord Alfa-informatica, RUG,  Postbus 716, 9700 AS Groningen
vannoord at let dot rug dot nl            http://www.let.rug.nl/~vannoord

Mon, 31 May 2004 15:25:23 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages