From: unknown
Subject: Simplify list
Date: 
Message-ID: <3e82e85e.222b.16838@cs.rmit.edu.au>
Can anyone show me the general way to simplify the list in
lisp or any functional language.
For example how can i simplify this code below:
(IFLTEF (MIN (MAX 300.87200000000001 7.7235300000000002)
             (MAX 302.36599999999999 176.67699999999999))
             (MIN (MAX 357.21699999999998
72.892099999999999)
                  (IFLTEF 285.65300000000002
                          357.75900000000001
                          304.02800000000002
                          25.8766))
             (- (- 287.774 329.45699999999999)
                (GETDIST MYGOALPOS))

             (MAX (IFLTEF 251.75299999999999
                          117.008
                          73.049099999999996
                          342.52100000000002)
                  (IFLTEF 259.44099999999997
                          340.25599999999997
                          205.233
                          263.12900000000002)))

From: Kent M Pitman
Subject: Re: Simplify list
Date: 
Message-ID: <sfwr88t578p.fsf@shell01.TheWorld.com>
unknown writes:

> Can anyone show me the general way to simplify the list in
> lisp or any functional language.
> For example how can i simplify this code below: [...]

Is this homework?  If so, you should clearly identify that whenever
asking a question.  Not doing so amounts to tricking others into doing
your homework and is very frowned upon here.  We don't mind helping
with homework, but we need to answer differently in that case.
From: james anderson
Subject: Re: Simplify list
Date: 
Message-ID: <3E82FAA0.38C01C3F@setf.de>
Kent M Pitman wrote:
> 
> unknown writes:
> 
> > Can anyone show me the general way to simplify the list in
> > lisp or any functional language.
> > For example how can i simplify this code below: [...]
> 
> Is this homework?  If so, you should clearly identify that whenever
> asking a question.  Not doing so amounts to tricking others into doing
> your homework and is very frowned upon here.  We don't mind helping
> with homework, but we need to answer differently in that case.

one should also be more forthcoming when providing background to a question.
otherwise one may well get a response like:

? (eval '(IFLTEF (MIN (MAX 300.87200000000001 7.7235300000000002)
                    (MAX 302.36599999999999 176.67699999999999))
        (MIN (MAX 357.21699999999998
                  72.892099999999999)
             (IFLTEF 285.65300000000002
                     357.75900000000001
                     304.02800000000002
                     25.8766))
        (- (- 287.774 329.45699999999999)
           (GETDIST MYGOALPOS))
        
        (MAX (IFLTEF 251.75299999999999
                     117.008
                     73.049099999999996
                     342.52100000000002)
             (IFLTEF 259.44099999999997
                     340.25599999999997
                     205.233
                     263.12900000000002))))
25.8766
From: unknown
Subject: Re: Simplify list
Date: 
Message-ID: <3e8302ca.4bac.16838@cs.rmit.edu.au>
It's a homework but i didn't expect to solve the problem of
this homework cause it's too long and too many requirements.
I am just asking what the method that can use to simplify
some problem like the below example in general.
>
>
> Kent M Pitman wrote:
> >
> > unknown writes:
> >
> > > Can anyone show me the general way to simplify the
> > > list in lisp or any functional language.
> > > For example how can i simplify this code below: [...]
> >
> > Is this homework?  If so, you should clearly identify
> > that whenever asking a question.  Not doing so amounts
> > to tricking others into doing your homework and is very
> > frowned upon here.  We don't mind helping with homework,
> but we need to answer differently in that case.
> one should also be more forthcoming when providing
> background to a question. otherwise one may well get a
> response like:
> ? (eval '(IFLTEF (MIN (MAX 300.87200000000001
> 7.7235300000000002)
>                     (MAX 302.36599999999999
> 176.67699999999999))
>         (MIN (MAX 357.21699999999998
>                   72.892099999999999)
>              (IFLTEF 285.65300000000002
>                      357.75900000000001
>                      304.02800000000002
>                      25.8766))
>         (- (- 287.774 329.45699999999999)
>            (GETDIST MYGOALPOS))
>
>         (MAX (IFLTEF 251.75299999999999
>                      117.008
>                      73.049099999999996
>                      342.52100000000002)
>              (IFLTEF 259.44099999999997
>                      340.25599999999997
>                      205.233
>                      263.12900000000002))))
> 25.8766
From: Kaz Kylheku
Subject: Re: Simplify list
Date: 
Message-ID: <cf333042.0303271049.3c2a4bad@posting.google.com>
unknown wrote in message news:<···················@cs.rmit.edu.au>...
> It's a homework but i didn't expect to solve the problem of
> this homework cause it's too long and too many requirements.
> I am just asking what the method that can use to simplify
> some problem like the below example in general.

That method is called ``semantics-preserving algebraic manipulation''.
You learn how to do this in school for ordinary algebra, but it
extends into computing, over operations that involve objects other
than numbers.

The general idea is to arm yourself with a set of reduction patterns,
and then look for places in the expression which these patterns fit;
and then perform the associated tree transformation.

One useful tool is constant folding. When you see a function  call
whose inputs are constants, then replace that function call with the
result of evaluating over those constants. This idae finds immediate
application in your example expression:

>  (IFLTEF (MIN (MAX 300.87200000000001 7.7235300000000002)
>               (MAX 302.36599999999999 176.67699999999999)) ...)

(MAX CONSTANT CONSTANT) can be replaced by (EVAL '(MAX CONSTANT
CONSTANT)),
and similarly for min for MIN. Consequently (assuming that IFLTEF is
just a function, rather than a macro which changes the meaning of MIN
and MAX!) you can replace the whole (MIN ...) expression by the
constant 300.872.

I have no idea what IFLTEF is; but I'm sure it has useful properties
that you could exploit, and probably is a pure function that can be
folded to a constant if applied over constant arguments.

> >         (MIN (MAX 357.21699999999998
> >                   72.892099999999999)
> >              (IFLTEF 285.65300000000002
> >                      357.75900000000001
> >                      304.02800000000002
> >                      25.8766))
> >         (- (- 287.774 329.45699999999999)

Here is an alebraic pattern, with an associated rewrite rule:

    (- (- $1 $2)) --rewrite-->  (- $2 $1)

the pattern finds a match matches in the above expression, giving you
the rewrite:

   (- 329.45699999999999 287.774)

And of course you have constants here again, so you can replace the
call of the - function by the constant result.

You have to be a little careful, however, because floating point
arithmetic does not have all the properties of abstract real number
arithmetic. So you cannot apply the associative law, for instance:
it's not the case that

  (+ (+ a b) c)

is the same as

  (+ a (+ b c))

if a, b and c are floating point quantities. You can still apply such
transformations, but then you have to understand that you are changing
the semantics of the computation, and accept all the risks and
responsibilities of ensuring that it still yields acceptable results.
From: Raymond Wiker
Subject: Re: Simplify list
Date: 
Message-ID: <86u1dohn53.fsf@raw.grenland.fast.no>
unknown writes:

> It's a homework but i didn't expect to solve the problem of
> this homework cause it's too long and too many requirements.
> I am just asking what the method that can use to simplify
> some problem like the below example in general.

        Here's a simple function for simplifying numeric code:

(defun simplify (item)
  (if (atom item)
      item
      (let ((op (first item))
            (vals (mapcar #'simplify (rest item))))
        (if (every #'numberp vals)
            (apply op vals)
            (cons op vals)))))

(format t "~a~%" (simplify '(+ a b 0.0)))
(format t "~a~%" (simplify '(+ a b (min 1 3 5) (* 7 8 9 0.0))))

which gives:

(+ A B 0.0)
(+ A B 1 0.0)

        This can be extended to also do some obvious simplifications
depending on the actual operation:

(defun simplify-2 (item)
  (if (atom item)
      item
      (let ((op (first item))
            (vals (mapcar #'simplify (rest item))))
        (if (every #'numberp vals)
            (apply op vals)
            (cond ((member op '(+ -))
                   (cons op (remove 0 vals :test #'equalp)))
                  ((and (eq op '*)
                        (member 0 vals :test #'equalp))
                   0)
                  ;;; etc
                  (t (cons op vals)))))))

(format t "~a~%" (simplify-2 '(+ a b 0.0)))
(format t "~a~%" (simplify-2 '(+ a b (min 1 3 5) (* 7 8 9 0.0))))

which gives

(+ A B)
(+ A B 1)

-- 
Raymond Wiker                        Mail:  ·············@fast.no
Senior Software Engineer             Web:   http://www.fast.no/
Fast Search & Transfer ASA           Phone: +47 23 01 11 60
P.O. Box 1677 Vika                   Fax:   +47 35 54 87 99
NO-0120 Oslo, NORWAY                 Mob:   +47 48 01 11 60

Try FAST Search: http://alltheweb.com/
From: james anderson
Subject: Re: Simplify list
Date: 
Message-ID: <3E83121E.FF0C870C@setf.de>
unknown wrote:
> 
> It's a homework but i didn't expect to solve the problem of
> this homework cause it's too long and too many requirements.
> I am just asking what the method that can use to simplify
> some problem like the below example in general.

one cannot "simplify" without a metric. "in general" is not sufficient. my
facetious answer was the result of a concrete, well-known, general
"simplification" method. it was not the one you seem to be thinking of. what
do you mean by "simplify"?

> >
> >
> > Kent M Pitman wrote:
> > >
> > > unknown writes:
> > >
> > > > Can anyone show me the general way to simplify the
> > > > list in lisp or any functional language.
> > > > For example how can i simplify this code below: [...]
> > >
> > > Is this homework?  If so, you should clearly identify
> > > that whenever asking a question.  Not doing so amounts
> > > to tricking others into doing your homework and is very
> > > frowned upon here.  We don't mind helping with homework,
> > but we need to answer differently in that case.
> > one should also be more forthcoming when providing
> > background to a question. otherwise one may well get a
> > response like:
> > ? (eval '(IFLTEF (MIN (MAX 300.87200000000001
> > 7.7235300000000002)
> >                     (MAX 302.36599999999999
> > 176.67699999999999))
> >         (MIN (MAX 357.21699999999998
> >                   72.892099999999999)
> >              (IFLTEF 285.65300000000002
> >                      357.75900000000001
> >                      304.02800000000002
> >                      25.8766))
> >         (- (- 287.774 329.45699999999999)
> >            (GETDIST MYGOALPOS))
> >
> >         (MAX (IFLTEF 251.75299999999999
> >                      117.008
> >                      73.049099999999996
> >                      342.52100000000002)
> >              (IFLTEF 259.44099999999997
> >                      340.25599999999997
> >                      205.233
> >                      263.12900000000002))))
> > 25.8766
From: unknown
Subject: Re: Simplify list
Date: 
Message-ID: <3e830ff2.5ffc.16838@cs.rmit.edu.au>
> It's a homework but i didn't expect to solve the problem
> of this homework cause it's too long and too many
> requirements. I am just asking what the method that can
> use to simplify some problem like the below example in
> general. >
Sorry i forgot, simplify mean make it shorter e.g
simplify (X) --> shorter version of input program or
expression

> >
> > Kent M Pitman wrote:
> > >
> > > unknown writes:
> > >
> > > > Can anyone show me the general way to simplify the
> > > > list in lisp or any functional language.
> > > > For example how can i simplify this code below:
> > [...] >
> > > Is this homework?  If so, you should clearly identify
> > > that whenever asking a question.  Not doing so amounts
> > > to tricking others into doing your homework and is
> > > very frowned upon here.  We don't mind helping with
> > homework, but we need to answer differently in that
> > case. one should also be more forthcoming when providing
> > background to a question. otherwise one may well get a
> > response like:
> > ? (eval '(IFLTEF (MIN (MAX 300.87200000000001
> > 7.7235300000000002)
> >                     (MAX 302.36599999999999
> > 176.67699999999999))
> >         (MIN (MAX 357.21699999999998
> >                   72.892099999999999)
> >              (IFLTEF 285.65300000000002
> >                      357.75900000000001
> >                      304.02800000000002
> >                      25.8766))
> >         (- (- 287.774 329.45699999999999)
> >            (GETDIST MYGOALPOS))
> >
> >         (MAX (IFLTEF 251.75299999999999
> >                      117.008
> >                      73.049099999999996
> >                      342.52100000000002)
> >              (IFLTEF 259.44099999999997
> >                      340.25599999999997
> >                      205.233
> >                      263.12900000000002))))
> > 25.8766