Re: Newbie without any idea!!!
- From: "Mauro Di Nuzzo" <picorna@xxxxxxxxx>
- Date: Wed, 29 Jun 2005 15:48:56 +0200
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: Mauro Di Nuzzo
- Re: Newbie without any idea!!!
- References:
- Newbie without any idea!!!
- From: leo
- Newbie without any idea!!!
- Prev by Date: Re: Free Prolog++ for Windows ...??
- Next by Date: Re: Newbie without any idea!!!
- Previous by thread: Newbie without any idea!!!
- Next by thread: Re: Newbie without any idea!!!
- Index(es):
Relevant Pages
|
|