Hi!
I’m attempting to write a relation that maps between a string and the parsed version of that string. The string looks like
//4/////2/1//3///1/4///2///1/3//2
And should be interpreted as a list delimited by /
, where an empty element means something (denotes a free variable), and an integer (0-9) denotes a variable with such integer value. But I haven’t gotten that far yet.
After taking some code from online and modifying it, I arrived at the following:
nonmember(Arg,[Arg|_]) :-
!,
fail.
nonmember(Arg,[_|Tail]) :-
!,
nonmember(Arg,Tail).
nonmember(_,[]).
split(In, Sep, [Left|Rest]) :-
append(Left, [Sep|Right], In), nonmember(Sep, Left), split(Right, Sep, Rest).
split(In, Sep, [In]) :- nonmember(Sep, In).
This seems to work:
?- split(S, 47, [[], [50], []]).
S = [47,50,47]
?- split("2//3", 47, C).
C = [[50],[],[51]] ?
But I also want to “chunk” them into 4 equal pieces:
chunk_n(N, [], []).
chunk_n(N, L, [Chunk | Chunked]) :-
append(Chunk, L1, L),
length(Chunk, N),
chunk_n(N, L1, Chunked).
urlcounts(N, UrlCounts, Top, Bottom, Left, Right) :-
chunk_n(N, L, [Top, Bottom, Left, Right]),
split(UrlCounts, 47, L).
urlcounts
is my overall function.
However, urlcounts
behaves as the following:
- if the
chunk_n(...)
condition is put before thesplit(...)
one, then it only works whenTop
Bottom
Left
Right
are instantiated and stack overflows ifUrlCounts
is instantiated but notTop
Bottom
Left
Right
- if
split(...)
beforechunk_n(...)
, then it only works ifUrlCounts
is instantiated, and stack overflows ifTop
Bottom
Left
Right
are given but notUrlCounts
.
Any suggested fixes?