Re: Newbie without any idea!!!
- From: "Mauro Di Nuzzo" <picorna@xxxxxxxxx>
- Date: Wed, 29 Jun 2005 16:54:40 +0200
This is a combination of the two. I think this serve all your needs.
sum(Result, List) :-
sum(Result, 0, List).
sum(Result, Result, []) :- !.
sum(Result, Temp, [Head|Tail]) :-
sum(NewTemp, Temp, Head),
sum(Result, NewTemp, Tail).
sum(Result, Temp, Head) :-
Result is Temp + Head.
Cheers, M
"Mauro Di Nuzzo" <picorna@xxxxxxxxx> ha scritto nel messaggio
news:Qcywe.6967$hV3.2285@xxxxxxxxxxxxxxxxxxxxxx
> Here is the standard swi prolog flatten/2.
>
> swi_flatten(List, FlatList) :-
> swi_flatten(List, [], FlatList0), !,
> FlatList = FlatList0.
>
> swi_flatten(Var, Tl, [Var|Tl]) :-
> var(Var), !.
> swi_flatten([], Tl, Tl) :- !.
> swi_flatten([Hd|Tl], Tail, List) :-
> swi_flatten(Hd, FlatHeadTail, List),
> swi_flatten(Tl, Tail, FlatHeadTail).
> swi_flatten(Atom, Tl, [Atom|Tl]).
>
> sum(X, L) :-
> swi_flatten(L, F),
> sum1(X, F, 0).
> sum1(X, [], X).
> sum1(X, [H|T], R) :-
> R1 is R + H,
> sum1(X, T, R1).
>
> bye
> M
>
> <leo@xxxxxxxxxxxxxx> ha scritto nel messaggio
> news:1120053965.668110.5220@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> > How can I do if I do not use swi prolog flatten/2? Thx!
> >
> > Ming
> >
>
>
.
- Follow-Ups:
- Re: Newbie without any idea!!!
- From: leo
- Re: Newbie without any idea!!!
- References:
- Newbie without any idea!!!
- From: leo
- Re: Newbie without any idea!!!
- From: Mauro Di Nuzzo
- Re: Newbie without any idea!!!
- From: Mauro Di Nuzzo
- Re: Newbie without any idea!!!
- From: leo
- Re: Newbie without any idea!!!
- From: Mauro Di Nuzzo
- Newbie without any idea!!!
- Prev by Date: Re: Newbie without any idea!!!
- Next by Date: Re: After Clocksin & Mellish?
- Previous by thread: Re: Newbie without any idea!!!
- Next by thread: Re: Newbie without any idea!!!
- Index(es):
Relevant Pages
|
|