Home > Archive > Prolog > May 2004 > generating numbers 1..N
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
generating numbers 1..N
|
|
| vertigo 2004-05-15, 5:31 am |
| Hello
i use SWI-Prolog version 5.2.8.
I wanted to receive list with numbers 1..N.
I tried:
findall(I,for(I,1,N),List)
but there is no 'for' in this prolog.
I tried to do it by hand:
create(N,Numbers):-
create2(N,[],Numbers).
create2(N,IPath,OPath):-
conc(N,IPath,OPath),
Max is N-1,
((Max >0, create2(Max,IPath,OPath)) ; (Max=<0)).
conc([],[],[]).
conc([],L,L).
conc(L,[],L).
conc([X,L1],L2,[X,L3]):-
conc(L1,L2,L3).
but i always receive:
?- create(4,X).
No
Why ?
How can i solve this problem ?
Thanx
Michal
| |
| bart demoen 2004-05-15, 7:31 am |
| vertigo wrote:
>
> create(N,Numbers):-
> create2(N,[],Numbers).
> create2(N,IPath,OPath):-
> conc(N,IPath,OPath),
> Max is N-1,
> ((Max >0, create2(Max,IPath,OPath)) ; (Max=<0)).
>
> conc([],[],[]).
> conc([],L,L).
> conc(L,[],L).
> conc([X,L1],L2,[X,L3]):-
> conc(L1,L2,L3).
Several things:
1) conc expects a list as first argument, and you call it with N - a
number - that is bound to fail
2) you seem to want to use an accumilating parameter - than compute new
accumulating parameters until you are done and then bind the output
argument to the accu
as in
create(N,Numbers):-
create2(N,[],Numbers).
create2(N,IPath,OPath):-
conc([N],IPath,NewIPath),
Max is N-1,
(
(Max >0, create2(Max,NewIPath,OPath))
;
(Max=<0, OPath = NewIPath)
).
3) the last clause of conc should be
conc([X|L1],L2,[X|L3]):-
conc(L1,L2,L3).
Note the | instead of the ,
[X,L1] means a list with the two elements X and L1 - does NOT unify with
[a,b,c]
[X|L1] means a list with first element X and tail L1 - does unify with
[a,b,c]
Finally, clause 1 and 3 of conc are redundant - and they lead to
duplicate answers - nothing really wrong, but probably unwanted
behaviour.
Cheers
Bart Demoen
| |
| Nameless 2004-05-16, 7:31 am |
| "vertigo" wrote in message
news:c84m6p$aq8$1@nemesis.news.tpi.pl...
....
> i use SWI-Prolog version 5.2.8.
> I wanted to receive list with numbers 1..N.
> I tried:
> findall(I,for(I,1,N),List)
>
> but there is no 'for' in this prolog.
....
SWI-Prolog version 5.3.2 has the built-in predicate
between/3, which is comparable to for/3:
?- findall(I,between(3,8,I),List).
I = _G347
List = [3, 4, 5, 6, 7, 8]
Yes
Take note of the order of the arguments in between/3.
Look up between/3 in your SWI-Prolog reference manual.
--
Mail sent to this email address is automatically deleted
(unread) on the server. Send replies to the newsgroup.
| |
| Algernon 2004-05-22, 4:34 am |
| create(N,Ls) :- create(1,N,Ls).
create(M,N,[M|Ns]) :-
M < N,
M1 is M + 1,
create(M1,N,Ns).
create(N,N,[N]).
| |
| flapper 2004-05-22, 4:31 pm |
| vertigo wrote:
> Hello
> i use SWI-Prolog version 5.2.8.
> I wanted to receive list with numbers 1..N.
> I tried:
> findall(I,for(I,1,N),List)
>
> but there is no 'for' in this prolog.
> I tried to do it by hand:
> create(N,Numbers):-
> create2(N,[],Numbers).
> create2(N,IPath,OPath):-
> conc(N,IPath,OPath),
> Max is N-1,
> ((Max >0, create2(Max,IPath,OPath)) ; (Max=<0)).
>
> conc([],[],[]).
> conc([],L,L).
> conc(L,[],L).
> conc([X,L1],L2,[X,L3]):-
> conc(L1,L2,L3).
>
> but i always receive:
> ?- create(4,X).
>
> No
>
> Why ?
> How can i solve this problem ?
>
>
> Thanx
> Michal
>
>
/*
Here's how I derive 'from'.
Let from(X1,Xn,L) iff L is a list of consecutive integers from X1 to Xn.
(a) if X1 = Xn then the list must be of the form
[X1]
i.e.,
*/
from(X1,X1,[X1]).
/*
(b) if X1 < Xn then the list must be of the form
[X1,X2|L]
where X2 is X1 + 1, and [X2|L] is a list of consecutive integers from X2
to Xn, i.e.,
*/
from(X1,Xn,[X1,X2|L]) :-
X1 < Xn,
X2 is X1 + 1,
from(X2,Xn,[X2|L]).
/*
There are no other cases to consider.
*/
?- from(-5,1,L).
L = [-5, -4, -3, -2, -1, 0, 1] ;
No more answers.
--
sequitur AT sonic DOT net
| |
| citizen 2004-05-22, 7:35 pm |
| vertigo wrote:
> Hello
> i use SWI-Prolog version 5.2.8.
> I wanted to receive list with numbers 1..N.
> I tried:
> findall(I,for(I,1,N),List)
>
> but there is no 'for' in this prolog.
> I tried to do it by hand:
> create(N,Numbers):-
> create2(N,[],Numbers).
> create2(N,IPath,OPath):-
> conc(N,IPath,OPath),
> Max is N-1,
> ((Max >0, create2(Max,IPath,OPath)) ; (Max=<0)).
>
> conc([],[],[]).
> conc([],L,L).
> conc(L,[],L).
> conc([X,L1],L2,[X,L3]):-
> conc(L1,L2,L3).
>
> but i always receive:
> ?- create(4,X).
>
> No
>
> Why ?
> How can i solve this problem ?
>
>
> Thanx
> Michal
>
>
/*
Here's how I derive 'from'.
Let from(X1,Xn,L) iff L is a list of consecutive integers from X1 to
Xn. For instance,
from(-5,1,[-5,-4,-3,-2,-1,0,1]).
(a) if X1 = Xn, the list must be of the form
[X1]
i.e.,
*/
from(X1,X1,[X1]).
/*
(b) if X1 < Xn, the list must be of the form
[X1,X2|L]
where X2 is X1 + 1 and [X2|L] is a list of consecutive integers from X2
to Xn, i.e.,
*/
from(X1,Xn,[X1,X2|L]) :-
X1 < Xn,
X2 is X1 + 1,
from(X2,Xn,[X2|L]).
/*
(c) If X1 > Xn, there is no list of consecutive integers /from/ X1
/to/ Xn.
There are no other cases to consider.
*/
?- from(-5,1,L).
L = [-5, -4, -3, -2, -1, 0, 1] ;
No more answers.
--
sequitur AT sonic DOT net
|
|
|
|
|