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

  • Next message: Bart Demoen: "Re: Egg on my face, I guess I still have trouble with declarative thinking at times."
    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


  • Next message: Bart Demoen: "Re: Egg on my face, I guess I still have trouble with declarative thinking at times."

    Relevant Pages

    • Re: Sublists question
      ... I do not really get what the predicate means or does. ... >>helps just to write the predicate out clearly in natural language, ... >>then translate into Prolog. ... more as a functional programming language than as a logic ...
      (comp.lang.prolog)
    • newbie question,built-in predicate get
      ... I am learning clocksin's "programming in prolog". ... for predicate 'get', the book says that if X is instantiated, the ...
      (comp.lang.prolog)
    • Psuedo_Newbie: Data Storage?
      ... Prolog and Oz are giving me a good workover in the area of Logic ... programming. ... with a perpetually free tail variable. ... It seems like there should be a way to create a predicate, ...
      (comp.lang.prolog)
    • Re: Is Prolog good for AI? (was: Minsky still posting)
      ... > than a programming language suitable for large scale direct human use. ... but it was Prolog nor AI. ... >> than most other languages. ... PHP because it solves a very specific problem. ...
      (comp.lang.prolog)
    • Re: Prolog module system
      ... I think prolog offers so much freedom that one can build its own ... Both modules define a member/2 predicate, ... this leads to a name conflict. ... shaky ground as current module systems (look at the issues with e.g. ...
      (comp.lang.prolog)