Re: How to get the terms of a compund term (predicate: simplfy)?

From: Emre Sevinc (emres_at_bilgi.edu.tr)
Date: 12/26/04


Date: Sun, 26 Dec 2004 14:43:32 +0200


"tmp123" <tmp123@menta.net> writes:

> Emre Sevinc wrote:
>> Following Bratko's "PROLOG: Programming for Artificial
>> Intelligence" I found an exercise:
>
> I insert some sugestions, I hope they useful:
>
>>
> a) in SWI exist a clause called "write_canonical". Is is very practical
> to show the real format of one expressions, because filters all infix
> expressions. By example, typing:
>
> ?- write_canonical(1+x)
> +(1,x)
>
> b) A clause can have any kind of term as input. Thus, not only it is
> posible write things like:
> foo(1) :- ...
> foo([H|Q]) :- ...
> foo(n(X,Y)) :- ...
>
> but also
> foo(+(X,Y)) :- ...
> foo(X+Y) :- ... <= interesting for your problem?

As far as I can see the problem is a little bit more
complex than I assumed it to be at the beginning ;-)

The things I've done until now just splits the original
expression into two lists and then recombining the lists
but losing the operations, so it simply is not the solution
to the simplification problem.

write_canonical looks like a nice function, but I guess
it is only for output, it doesn't create a list, am I wrong.

Anyway, the problem seems to be walk along the list and

  if the current element is a number
   then put it in the ResultingNum,
   move on to next element and keep on iterating
  if the current element is a + or -
   get the next element
    if it is a number then add (it to) or subtract it from
             the ResultingNum variable,
             storing the result in ResultingNum
    if it is NOT a number then put it in a SymList
          which have slots like Symbol, SymbolCounter
          such that SymbolCounter is incremented or decremented
          if the same symbol is encountered
          if a different symbol is encountered then put
            it in another slot and make its SymbolCounter 1.

Sort of a rough description in procedural style, but I just
can't imagine it in declarative style.

>> Any suggestions?
>>
>
> ! problem or ; problem?
>
> ?- write_canonical(
> ( Expression =.. [Head | Tail],
> number(Head), !,
> write('number')
> ;
> Head \== +,
> write('symbol'),
> L =.. Tail,
> simplify(L, E) ).
>
>
>
> ;(,(=..(_G1440, '.'(_G1437, _G1438)), ,(number(_G1437), ,(!,
> write(number)))), ,(\==(_G1437, +), ,(write(symbol), ,(=..(_G1461,
> _G1438), simplify(_G1461, _G1465)))))
>
> Expression = _G1440
> Head = _G1437
> Tail = _G1438
> L = _G1461
> E = _G1465
>
> ; is at the start. Is that what you expected?

What I want to do in these kind of situations is that

  first of all do: Expression =.. [Head | Tail]

and only after that

  if bla bla Head is something bla bla ...
       then bla bla...
  otherwise do
      bla bla Tail bla bla...

The problem is that the Prolog template I try
to use simply don't work that way, what I call
"first of all do:" part becomes a part of "if"
and worst of all if it is not satisfied it means
that Head and Tail variables is not instantiated and
this creates a problem in "otherwise do ..." part
because I assume that Head and Tail are initialized to some
value and then I check something and if it's ok
I do something with Head otherwise I do another thing
with Tail. But the Tail is not... you see? oh my! It just twists
my brain :)
     
  
>> Maybe some of the Prolog masters here would say that
>> I don't need that warm-up exercise, I must directly
>> code the solution itself but I find to convert "if
>> X is that then put it in that list, otherwise put it
>> in the other list" a little bit difficult, that's why
>> I started this way.
>>
>> Hope some exprerienced people can enlighten me.
>
> Maybe. In the while, I hope that a simple programmer can help ;-)

You certainly know much more than me ;-)
I finally tried something with DCGs but at the end
I've created a solution that does completely another
thing (I guess). :)

-- 
Emre Sevinc
eMBA Software Developer         Actively engaged in:
http:www.bilgi.edu.tr           http://ileriseviye.org
http://www.bilgi.edu.tr         http://fazlamesai.net
Cognitive Science Student       http://cazci.com
http://www.cogsci.boun.edu.tr


Relevant Pages

  • Re: How to remove redundant elements of a list
    ... I know the algorithm for remove the same elements,but I do not know ... list into head and tail components, consider head, and then recurse ... lists usually has the form: ...
    (comp.lang.ml)
  • Re: Linked List
    ... file_collate_2.c is interesting because it has a list of lists. ... ** with some same purchases as other files. ... head = tail = NULL; ... ** (head) list format: ...
    (comp.lang.c)
  • Re: Converting Scheme to Prolog
    ... element from the head of a list. ... The third case covers lists embedded within lists (this is the tricky ... and delete from the tail. ...
    (comp.lang.prolog)
  • Re: Linked list need help...
    ... > i need help with linked lists... ... static long unsigned list_count(list_type *head) ... list_type *tail; ... float literature; ...
    (comp.lang.c)
  • Re: EE Student, Edit, Proposal Masters, Help (concepts of functional programming, symbolic programmi
    ... > "No, expressions are not lists. ... This is a specialty of Matlab lists. ... > represent such a data structure is with an array of pointers and when ... This is referred to as lazy evaluation. ...
    (sci.math.symbolic)