Egg on my face, I guess I still have trouble with declarative thinking at times.
From: Robert Oschler (no_replies_at_fake_email_address.invalid)
Date: 02/15/04
- Previous message: Arun Hallan: "Re: Help with project"
- Next in thread: Bart Demoen: "Re: Egg on my face, I guess I still have trouble with declarative thinking at times."
- Reply: Bart Demoen: "Re: Egg on my face, I guess I still have trouble with declarative thinking at times."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 15 Feb 2004 13:12:35 -0500
Hello,
After claiming that I am not irretrievably brain mangled by procedural
programming, in the previous thread titled "Newbie, The secret with Prolog:
How to to transform old "traditionally programming" thinking into Prolog
style?", I just had this unfortunate experience.
I wanted to create, from scratch without looking it up, a predicate that
finds all the combinations of a set.
After stating the problem declaratively, I originally came up with:
------------------------------------------
diffset(_, [], []).
diffset(A, [B | T], [B | Res]):-
A \= B,
diffset(A, T, Res).
diffset(A, [A | T], Res):-
diffset(A, T, Res).
all_member([], _).
all_member([H1 | T1], L):-
member(H1, L),
diffset(H1, L, L2),
all_member(T1, L2).
findcomb(L, Comb):-
all_member(Comb, L).
-------------------------------------------
In conjunction with the use of findall(), this does indeed find all the
combinations of a given list. Unfortunately they are not unique
combinations. If the elements in each of the solutions sets were sorted,
there would be duplicate solutions. This of course is due to the use of
member() in conjunction with backtracking to find all successive solutions.
So I tried to think declaratively about the problem of finding only *unique*
combinations of a given list. After an hour or two, my mind got tired so I
gave up. As soon as I lay down and stopped "verbalizing" the problem, I
could immediately see visually how I wanted the predicate to operate; to
piece the elements of the target list together to create the list of
solutions. I quickly got up and without talking to myself mentally, banged
out the very simple predicate below.
--------------------------------------------
findcomb_unique([], []).
findcomb_unique([H | T], [H | T2]):-
findcomb_unique(T, T2).
findcomb_unique([_ | T], T2):-
findcomb_unique(T, T2).
findallcomb_unique(L, AllComb):-
findall(X, findcomb_unique(L, X), AllComb).
----------------------------------------------
This is what I saw, not heard, in my head.
I guess I will have to face up to the fact that for me, Prolog will always
be this strange and elegant language that another part of my brain loves,
and that part isn't talking to the rest of me.
So be it.
BTW, if anyone would like to state declaratively what each clause does in
the predicate findcomb_unique, I'd be grateful. I still have trouble trying
describe it declaratively.
-ro
- Previous message: Arun Hallan: "Re: Help with project"
- Next in thread: Bart Demoen: "Re: Egg on my face, I guess I still have trouble with declarative thinking at times."
- Reply: Bart Demoen: "Re: Egg on my face, I guess I still have trouble with declarative thinking at times."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|