dividing list
From: Pere Montolio (tmp123_at_menta.net)
Date: 12/14/04
- Previous message: A.L.: "Re: Where can i find <<assignment problem>> example an prolog"
- Next in thread: Bart Demoen: "Re: dividing list"
- Reply: Bart Demoen: "Re: dividing list"
- Reply: Jan Bernard van Doorn: "Re: dividing list"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 14 Dec 2004 11:28:03 -0800
In order to add one more posibility to the previous solutions:
dividelist(A,B,C) :-
length(A,L),
L2 is L/2,
length(B,L2),
length(C,L2),
append(B,C,A).
OK, in this version it is valid only for even length, but it is
easy to expand it to odd length, if necessary.
In my opinion, (I'm not teacher, so it is only the opinion of
a programmer), it has some interesting characteristics, and it
seems no erroneous (until now):
It is nice, because it is a single rule, not recursive, and matchs
the human idea of the split: two list of same length that its
append gives the parameter.
It is 100% prolog, because it uses things like call to length with
first parameter free (=unbound=variable, I do not know the word).
It is fast, at least in SWI-prolog: 1000 executions for a list of 1000
elements, for the solution of the previous post, takes 1.3 Minferences
and 0.73 seconds CPU time. This one takes 0.45 Minferences and 0.57
seconds (34% inferences and 78% time).
(sorry if a post that several times, but I've some problems with
my navigator and google).
- Previous message: A.L.: "Re: Where can i find <<assignment problem>> example an prolog"
- Next in thread: Bart Demoen: "Re: dividing list"
- Reply: Bart Demoen: "Re: dividing list"
- Reply: Jan Bernard van Doorn: "Re: dividing list"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|