Re: Newbie without any idea!!!
- From: "Mauro Di Nuzzo" <picorna@xxxxxxxxx>
- Date: Wed, 29 Jun 2005 15:53:06 +0200
Forget it! This is a "trick" using swi prolog flatten/2.
Ah, the first clause (ie. sum(0,[]) is not needed).
M
"Mauro Di Nuzzo" <picorna@xxxxxxxxx> ha scritto nel messaggio
news:OExwe.6951$hV3.2078@xxxxxxxxxxxxxxxxxxxxxx
> This could be a solution:
>
> sum(0, []).
> sum(X, L) :-
> flatten(L, F),
> sum1(X, F, 0).
> sum1(X, [], X).
> sum1(X, [H|T], R) :-
> R1 is R + H,
> sum1(X, T, R1).
>
> Note that also the following (for example) are permitted:
>
> ?- sum(X, [[]]).
> ?- sum(X, [[2,2],2]).
>
> I dont know if this is undesirable.
> Of course, the list must be an integer list.
>
> Cheers, M
>
>
> <leo@xxxxxxxxxxxxxx> ha scritto nel messaggio
> news:1120030257.119295.55300@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> > Hi! The following is the requirement of the program
> >
> > sum(X, 4) : X would be evaluated to 4.
> > sum(X, [2,4]): X would be evaluated to 6.
> > sum(X, [2,[2,2]]): X would be evaluated to 6.
> >
> > How can I implement by Prolog?
> > I have the following idea.
> > sum(X,X).
> > sum(X,[H|T]):-..............
> >
> > Thanks!
> > 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
- Newbie without any idea!!!
- Prev by Date: Re: Newbie without any idea!!!
- Next by Date: Re: SWI Prolog qsave_program/2 win/unix trouble
- Previous by thread: Re: Newbie without any idea!!!
- Next by thread: Re: Newbie without any idea!!!
- Index(es):
Relevant Pages
|
|