Re: Newbie without any idea!!!



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
> >
>
>


.



Relevant Pages

  • Re: Python Doc Problem Example: os.path.split
    ... I was working on a program where i needed to split a path into dirname, ... > Split the pathname path into a pair, (head, tail) where tail is the ... > last pathname component and head is everything leading up to that. ... > tail part will never contain a slash; if path ends in a slash, ...
    (comp.unix.programmer)
  • Re: Using a link list over an array.
    ... compile (p->data is a void *) so you have not shown us some key ... int cmp ... head = list_sort; ... list_type *tail; ...
    (comp.lang.c)
  • Re: Linked List
    ... pete wrote: ... int main ... head = tail = NULL; ... head = list_sort; ...
    (comp.lang.c)
  • HELP for doubly linked list
    ... forward and one pointing backwards. ... You must be able to insert a new node at the head of the ... You must be able to insert a new node at the tail of the ... tail, F – display contents forward, B – display contents backwards, ...
    (microsoft.public.dotnet.languages.vc)
  • HELP for doubly linked list
    ... forward and one pointing backwards. ... You must be able to insert a new node at the head of the ... You must be able to insert a new node at the tail of the ... tail, F – display contents forward, B – display contents backwards, ...
    (microsoft.public.dotnet.languages.vc)