From: Philip Haddad
Subject: stupid question
Date: 
Message-ID: <1131244744.169037.179140@o13g2000cwo.googlegroups.com>
ok, I have this

(defun mid (x y z)
   (and (> (min x y z)) (< (max x y z))))

and of course it returns "T" because of the "and" call. so how do I get
it to print out the middle of x y z (they are all ints)?

Thanks
Philip Haddad

From: Tayssir John Gabbour
Subject: Re: stupid question
Date: 
Message-ID: <1131250856.988750.94920@f14g2000cwb.googlegroups.com>
Pascal Bourguignon wrote:
> "Tayssir John Gabbour" <···········@yahoo.com> writes:
> > (defun mid (x y z)
> >   (min (max x y) z))
>
> (mid 2 3 1) --> 1

Ok, I meant something more like:

(defun mid (x y z)
  (min (max x y) (max x z) (max y z)))

Now I'm going to get some needed sleep.

Tayssir
From: Ron Garret
Subject: Re: stupid question
Date: 
Message-ID: <rNOSPAMon-889570.23225705112005@news.gha.chartermi.net>
In article <·······················@f14g2000cwb.googlegroups.com>,
 "Tayssir John Gabbour" <···········@yahoo.com> wrote:

> Pascal Bourguignon wrote:
> > "Tayssir John Gabbour" <···········@yahoo.com> writes:
> > > (defun mid (x y z)
> > >   (min (max x y) z))
> >
> > (mid 2 3 1) --> 1
> 
> Ok, I meant something more like:
> 
> (defun mid (x y z)
>   (min (max x y) (max x z) (max y z)))
> 
> Now I'm going to get some needed sleep.
> 
> Tayssir

(defun cute-mid-1 (x y z)
  (second (sort (list x y z) '<))

(defun cute-mid-2 (x y z)
  (first (set-difference (list x y z) (list (min x y z) (max x y z)))))

(defun cute-mid-3 (x y z)
  (loop until (or (< x y z) (> x y z))
        do (rotatef x y z)
        finally return y))

:-)

rg
From: Tayssir John Gabbour
Subject: Re: stupid question
Date: 
Message-ID: <1131304182.561121.70820@g44g2000cwa.googlegroups.com>
Ron Garret wrote:
> In article <·······················@f14g2000cwb.googlegroups.com>,
>  "Tayssir John Gabbour" <···········@yahoo.com> wrote:
> > (defun mid (x y z)
> >   (min (max x y) (max x z) (max y z)))
> >
> > Now I'm going to get some needed sleep.
> >
> > Tayssir
>
> (defun cute-mid-1 (x y z)
>   (second (sort (list x y z) '<))
>
> (defun cute-mid-2 (x y z)
>   (first (set-difference (list x y z) (list (min x y z) (max x y z)))))
>
> (defun cute-mid-3 (x y z)
>   (loop until (or (< x y z) (> x y z))
>         do (rotatef x y z)
>         finally return y))

Cool. Does anyone know how to do this nicely with Screamer? Something
maybe like:

;; doesn't actually work due to DESTRUCTURING-BIND-V
(use-package :screamer)
(shadowing-import '(screamer::defun))

(defun mid (a b c)
  (destructuring-bind-v (x y z)
      (list a b c)
    (assert! (<=v x y z))
    (one-value (solution y (static-ordering #'linear-force)))))

I just used Screamer seriously today, for the first time..


Tayssir
From: Philip Haddad
Subject: Re: stupid question
Date: 
Message-ID: <1131311508.413489.221190@o13g2000cwo.googlegroups.com>
Screamer????
From: Tayssir John Gabbour
Subject: Re: stupid question
Date: 
Message-ID: <1131312953.992966.5190@z14g2000cwz.googlegroups.com>
Philip Haddad wrote:
> Screamer????

Yup. http://www.cliki.net/Screamer

It's pretty cool: tell Lisp what constraints a correct solution must
have, and it'll go and find a solution for you, if any. (Or you can
find all solutions, or just some, I suppose.)

So the single constraint I want to make is that...
(<= a b c) is true.

Given that (<= a b c) is true, I then want to return b.


Tayssir
From: Ron Garret
Subject: Re: stupid question
Date: 
Message-ID: <rNOSPAMon-DBEC74.22374706112005@news.gha.chartermi.net>
In article <·······················@g44g2000cwa.googlegroups.com>,
 "Tayssir John Gabbour" <···········@yahoo.com> wrote:

> Ron Garret wrote:
> > In article <·······················@f14g2000cwb.googlegroups.com>,
> >  "Tayssir John Gabbour" <···········@yahoo.com> wrote:
> > > (defun mid (x y z)
> > >   (min (max x y) (max x z) (max y z)))
> > >
> > > Now I'm going to get some needed sleep.
> > >
> > > Tayssir
> >
> > (defun cute-mid-1 (x y z)
> >   (second (sort (list x y z) '<))
> >
> > (defun cute-mid-2 (x y z)
> >   (first (set-difference (list x y z) (list (min x y z) (max x y z)))))
> >
> > (defun cute-mid-3 (x y z)
> >   (loop until (or (< x y z) (> x y z))
> >         do (rotatef x y z)
> >         finally return y))
> 
> Cool. Does anyone know how to do this nicely with Screamer? Something
> maybe like:
> 
> ;; doesn't actually work due to DESTRUCTURING-BIND-V
> (use-package :screamer)
> (shadowing-import '(screamer::defun))
> 
> (defun mid (a b c)
>   (destructuring-bind-v (x y z)
>       (list a b c)
>     (assert! (<=v x y z))
>     (one-value (solution y (static-ordering #'linear-force)))))
> 
> I just used Screamer seriously today, for the first time..

My guess is that you have to do something like:

(assert! (and (permutation-of (x y z) (a b c)) (<= x y z)))

or something like that.  I've not actually used screamer in a long time.

rg
From: Tayssir John Gabbour
Subject: Re: stupid question
Date: 
Message-ID: <1131364062.388341.101660@g44g2000cwa.googlegroups.com>
Ron Garret wrote:
> In article <·······················@g44g2000cwa.googlegroups.com>,
>  "Tayssir John Gabbour" <···········@yahoo.com> wrote:
> > Cool. Does anyone know how to do this nicely with Screamer? Something
> > maybe like:
> >
> > ;; doesn't actually work due to DESTRUCTURING-BIND-V
> > (use-package :screamer)
> > (shadowing-import '(screamer::defun))
> >
> > (defun mid (a b c)
> >   (destructuring-bind-v (x y z)
> >       (list a b c)
> >     (assert! (<=v x y z))
> >     (one-value (solution y (static-ordering #'linear-force)))))
> >
> > I just used Screamer seriously today, for the first time..
>
> My guess is that you have to do something like:
>
> (assert! (and (permutation-of (x y z) (a b c)) (<= x y z)))
>
> or something like that.  I've not actually used screamer in a long time.
>
> rg


Gotcha. What I got was something like:

(defun mid (n1 n2 n3)
  (let ((numbers (vector n1 n2 n3)))
    (with-indices (min mid max)
      (assert! (<=v (eltv numbers min)
                    (eltv numbers mid)
                    (eltv numbers max)))
      (elt numbers (first (solve-once mid min max))))))

I would've liked to use Screamer+, but I didn't have the time...

- - -

It depends on util functions like:

(screamer:define-screamer-package :blah
    (:export :solve-once
             :with-indices
             :eltv))
(in-package :blah)

(defun solve-once (&rest things)
  (one-value (solution things (static-ordering #'linear-force))))

;; we don't need no steenkin' docstrings
;; just binds vars to indices, and asserts they're all different
(defmacro with-indices (indices &body body)
  `(let* ,(loop with len = (1- (length indices))
                for i in indices
                collect `(,i (an-integer-betweenv 0 ,len)))
    (assert! (/=v ,@indices))
    ,@body))

(defun eltv (&rest rest)
  (applyv #'elt rest))


Tayssir
From: Philip Haddad
Subject: Re: stupid question
Date: 
Message-ID: <1131369639.340653.50350@o13g2000cwo.googlegroups.com>
ooooooo that's a nifty little trick.

Philip
From: Philip Haddad
Subject: Re: stupid question
Date: 
Message-ID: <1131253899.871505.54160@o13g2000cwo.googlegroups.com>
sigh. oh they teach the tables alright. I actually did a truth table
and came out with the same results as you did, however, it was my
memory of certain CL functions that was lacking.... it's been a few
months since I've written a functional Lisp program, Java has been
taking up a lot of my time in and out of school :(

Philip Haddad
From: Jack Unrue
Subject: Re: stupid question
Date: 
Message-ID: <i71rm1hlrhlkrdf92u661pcv1do3nqvglg@4ax.com>
On Sun, 06 Nov 2005 04:21:43 +0100, Pascal Bourguignon <····@mouse-potato.com> wrote:
>
> You could also build a truth table with some conditions and actions:
>
> +----------+----------+----------+-----+-----+-----+
> | (<= x y) | (<= x z) | (<= y z) |  x  |  y  |  z  |
> +----------+----------+----------+-----+-----+-----+
> |    YES   |    YES   |    YES   |  �  |  �  |  �  |
> |    YES   |    YES   |    NO    |  �  |  �  |  �  |
> |    YES   |    NO    |    YES   |  � impossible�  |
> |    YES   |    NO    |    NO    |  �  |  �  |  �  |
> |    NO    |    YES   |    YES   |  �  |  �  |  �  |
> |    NO    |    YES   |    NO    |  � impossible�  |
> |    NO    |    NO    |    YES   |  �  |  �  |  �  |
> |    NO    |    NO    |    NO    |  �  |  �  |  �  |
> +----------+----------+----------+-----+-----+-----+

That's the table for min.  This is for mid:

+----------+----------+----------+-----+-----+-----+
| (<= x y) | (<= x z) | (<= y z) |  x  |  y  |  z  |
+----------+----------+----------+-----+-----+-----+
|    YES   |    YES   |    YES   |  .  |  x  |  �  |
|    YES   |    YES   |    NO    |  .  |  .  |  x  |
|    YES   |    NO    |    YES   |  � impossible�  |
|    YES   |    NO    |    NO    |  x  |  �  |  .  |
|    NO    |    YES   |    YES   |  x  |  .  |  �  |
|    NO    |    YES   |    NO    |  � impossible�  |
|    NO    |    NO    |    YES   |  �  |  .  |  x  |
|    NO    |    NO    |    NO    |  �  |  x  |  .  |
+----------+----------+----------+-----+-----+-----+

A practical consideration is that we might also provide the
caller of mid with the means to specify what to do when not
all three parameters are distinct.

-- 
Jack Unrue
From: Jack Unrue
Subject: Re: stupid question
Date: 
Message-ID: <c56rm15pqnaggigjdgf8ceq27i5vk7ss7h@4ax.com>
On Sun, 06 Nov 2005 06:27:01 +0100, Pascal Bourguignon <····@mouse-potato.com> wrote:
>
> That'll teach me to cheat :-)  I should have built the table by hand
> instead of generating entirely programmatically with a wrong program...

D'oh!  :-)

-- 
Jack Unrue