From: Ken Tilton
Subject: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <u85ih.2143$4R1.504@newsfe12.lga>
So I want 1 or -1 at random. This looks verbose:
(- 1 (* 2 (random 2)))
Thoughts?
ken
--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon
Ken Tilton wrote:
> So I want 1 or -1 at random. This looks verbose:
>
> (- 1 (* 2 (random 2)))
>
> Thoughts?
Yes. It does truly /look/ verbose. Good thing that it /isn't/.
You can save one byte right away like this:
(1- (* 2 (random 2)))
It doesn't matter whether you subtract one or subtract from one.
Case?
(ecase (random 2) (0 -1) (1 1))
Now, drop the sandbag I put in, since we know RANDOM works. Voila, one
byte saved:
(case (random 2) (0 -1) (1 1))
How about table lookup?
(aref #(-1 1) (random 2))
Obligatory one byte save (you can't say I don't try!):
(elt #(-1 1) (random 2))
Save 7 more bits, since quote /looks/ only about 1 bit wide (who cares
how it's implemented):
(nth (random 2) '(-1 1))
Go for Baroque:
(getf '(0 -1 1 1) (random 2))
Two byte save, by using optional arg of getf:
(getf '(0 -1) (random 2) 1)
Shakespearean:
;; I am eight times thrust through the doublet, four through the hose;
;; my buckler cut through and through; my sword HACKED like a
;; hand-saw---ecce SIGNUM! [emphasis mine]
(signum (- (random 2) 1/2))
From: Ken Tilton
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <bR6ih.592$3o1.113@newsfe10.lga>
Kaz Kylheku wrote:
> Ken Tilton wrote:
>
>>So I want 1 or -1 at random. This looks verbose:
>>
>> (- 1 (* 2 (random 2)))
>>
>>Thoughts?
>
>
> Yes. It does truly /look/ verbose. Good thing that it /isn't/.
>
> You can save one byte right away like this:
>
> (1- (* 2 (random 2)))
Ah. Indeed.
>
> It doesn't matter whether you subtract one or subtract from one.
>
> Case?
OK, but I have posthumously outlawed branching.
>
> (ecase (random 2) (0 -1) (1 1))
>
> Now, drop the sandbag I put in, since we know RANDOM works. Voila, one
> byte saved:
>
> (case (random 2) (0 -1) (1 1))
>
> How about table lookup?
>
> (aref #(-1 1) (random 2))
>
> Obligatory one byte save (you can't say I don't try!):
>
> (elt #(-1 1) (random 2))
Both feel "heavy".
>
> Save 7 more bits, since quote /looks/ only about 1 bit wide (who cares
> how it's implemented):
>
> (nth (random 2) '(-1 1))
>
> Go for Baroque:
>
> (getf '(0 -1 1 1) (random 2))
>
> Two byte save, by using optional arg of getf:
>
> (getf '(0 -1) (random 2) 1)
These are good, if I die I want my code to die with me.
>
> Shakespearean:
>
> ;; I am eight times thrust through the doublet, four through the hose;
> ;; my buckler cut through and through; my sword HACKED like a
> ;; hand-saw---ecce SIGNUM! [emphasis mine]
>
> (signum (- (random 2) 1/2))
>
Not bad. This is the one i was looking for, but I kept landing on zero.
But someone else offered this and a four-token solution by email (and
maybe still propagating its way here, so that is the standard to beat.*
kt
* yes, I am posthumously declaring token count to be the deciding
metric, probably obviating the need for the ban on branching.
--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon
In article <·················@newsfe10.lga>,
Ken Tilton <·········@gmail.com> wrote:
> * yes, I am posthumously declaring token count to be the deciding
> metric, probably obviating the need for the ban on branching.
(logior 1 (- (random 2)))
rg
Ron Garret <·········@flownet.com> wrote:
+---------------
| Ken Tilton <·········@gmail.com> wrote:
| > * yes, I am posthumously declaring token count to be the deciding
| > metric, probably obviating the need for the ban on branching.
|
| (logior 1 (- (random 2)))
+---------------
Shorter by one token [even more if you count the parens]:
(expt -1 (random 2))
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
In article <································@speakeasy.net>,
····@rpw3.org (Rob Warnock) wrote:
> Ron Garret <·········@flownet.com> wrote:
> +---------------
> | Ken Tilton <·········@gmail.com> wrote:
> | > * yes, I am posthumously declaring token count to be the deciding
> | > metric, probably obviating the need for the ban on branching.
> |
> | (logior 1 (- (random 2)))
> +---------------
>
> Shorter by one token [even more if you count the parens]:
>
> (expt -1 (random 2))
Damn! I knew there had to be a better way.
I doff my hat to you, sir. :-)
rg
Ron Garret <·········@flownet.com> wrote:
+---------------
| ····@rpw3.org (Rob Warnock) wrote:
| > Ron Garret <·········@flownet.com> wrote:
| > +---------------
| > | (logior 1 (- (random 2)))
| > +---------------
| >
| > Shorter by one token [even more if you count the parens]:
| > (expt -1 (random 2))
|
| Damn! I knew there had to be a better way.
| I doff my hat to you, sir. :-)
+---------------
Sadly, I must decline the praise, since had I but taken the trouble
to look through the entire thread before responding I would have
seen that the very same version had been proposed by Paul Griffioen
more than an hour earlier! (Oh, the shame...) ;-}
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
On Wed, 20 Dec 2006 06:26:58 -0600, <····@rpw3.org> wrote:
> Ron Garret <·········@flownet.com> wrote:
>| Ken Tilton <·········@gmail.com> wrote:
>| > * yes, I am posthumously declaring token count to be the deciding
>| > metric, probably obviating the need for the ban on branching.
>|
>| (logior 1 (- (random 2)))
> +---------------
>
> Shorter by one token [even more if you count the parens]:
>
> (expt -1 (random 2))
Ah, very nice!
--
There are no average Common Lisp programmers
Reply-To: email is ignored.
--
Posted via a free Usenet account from http://www.teranews.com
Ken Tilton <·········@gmail.com> writes:
>>
>> Shakespearean:
>>
>> ;; I am eight times thrust through the doublet, four through the hose;
>> ;; my buckler cut through and through; my sword HACKED like a
>> ;; hand-saw---ecce SIGNUM! [emphasis mine]
>>
>> (signum (- (random 2) 1/2))
>>
An alternative (always to not have the ugly -1 or 1 in the code or too
many constant ;-) ):
(signum (cos (random pi)))
--
Matley
Why Lisp? http://alu.cliki.net/RtL%20Highlight%20Film
Evaluate Lisp: http://lisp.tech.coop/Evaluate%20Lisp
Ken Tilton wrote:
> So I want 1 or -1 at random. This looks verbose:
>
> (- 1 (* 2 (random 2)))
Using MatzLisp:
[-1,1][rand(2)]
From: =?ISO-8859-15?Q?Andr=E9_Thieme?=
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <embehv$vsu$1@registered.motzarella.org>
William James schrieb:
> Ken Tilton wrote:
>> So I want 1 or -1 at random. This looks verbose:
>>
>> (- 1 (* 2 (random 2)))
>
> Using ruby:
>
> [-1,1][rand(2)]
(one-of -1 1)
in Lisp.
Andr�
--
William James wrote:
> Ken Tilton wrote:
> > So I want 1 or -1 at random. This looks verbose:
> >
> > (- 1 (* 2 (random 2)))
>
> Using MatzLisp:
>
> [-1,1][rand(2)]
Shorter:
0.5<=>rand(2)
Ken Tilton wrote:
> So I want 1 or -1 at random. This looks verbose:
>
> (- 1 (* 2 (random 2)))
>
> Thoughts?
>From a stupid-optimisation point of view this is probably really quite
good, because the processor will love you because of no branches and no
references to memory etc. Of course I am quietly ignoring the call to
RANDOM which will have a million branches, refer to data stored on
paper tape, on Mars and so on. But that's the whole
stupid-optimisation trick.
--tim
From: Robert Strandh
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <6wfybbkonh.fsf@serveur5.labri.fr>
Ken Tilton <·········@gmail.com> writes:
> So I want 1 or -1 at random. This looks verbose:
>
> (- 1 (* 2 (random 2)))
>
> Thoughts?
I would probably say something like :
(if (zerop (random 2)) -1 1)
--
Robert Strandh
From: Ken Tilton
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <lq6ih.590$3o1.446@newsfe10.lga>
Robert Strandh wrote:
> Ken Tilton <·········@gmail.com> writes:
>
>
>>So I want 1 or -1 at random. This looks verbose:
>>
>> (- 1 (* 2 (random 2)))
>>
>>Thoughts?
>
>
> I would probably say something like :
>
> (if (zerop (random 2)) -1 1)
>
Yep, but the zerop bothered me for some reason. Been helping someone
with C lately, almost tried (if (random 2)...) Doh!
Can I posthumously declare branching code off-limits? IF is such a clear
winner, takes all the fun out of it. This is one of those
competitiveness committee deals.*
Man law: no branching.
How do we decide? Character-count the source? Dissassemble? Count the
tokens? Parens?
kt
* If X is unbeatable, like the spit-ball**, it gets banned from the sport.
** Wetting ones fingers before pitching a baseball.
--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon
Maybe (expt -1 (random 2)) ?
Paul
"Ken Tilton" <·········@gmail.com> schreef in bericht
·······················@newsfe12.lga...
> So I want 1 or -1 at random. This looks verbose:
>
> (- 1 (* 2 (random 2)))
>
> Thoughts?
>
> ken
>
> --
> Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
>
> "Well, I've wrestled with reality for thirty-five
> years, Doctor, and I'm happy to state I finally
> won out over it." -- Elwood P. Dowd
>
> "I'll say I'm losing my grip, and it feels terrific."
> -- Smiling husband to scowling wife, New Yorker cartoon
From: Ken Tilton
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <iKbih.11$7r7.9@newsfe11.lga>
Paul Griffioen wrote:
> Maybe (expt -1 (random 2)) ?
Yes, that was the winner, the CPU be damned, received soon after the OP
by direct email so I will not blabber the chap's name in case he is in a
witness protection program. I love it, trying to get him to release it
under BSD or MIT.
kenny
--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon
I agree it's nice, but personally I don't think I would use it in my code,
altough it is a bordercase.
While your at it, why not also include (expt #C(0 1) (random 4)) in the
license? I like that one as well. If you square it you get the original one.
Paul
"Ken Tilton" <·········@gmail.com> schreef in bericht
···················@newsfe11.lga...
>
>
> Paul Griffioen wrote:
>> Maybe (expt -1 (random 2)) ?
>
> Yes, that was the winner, the CPU be damned, received soon after the OP by
> direct email so I will not blabber the chap's name in case he is in a
> witness protection program. I love it, trying to get him to release it
> under BSD or MIT.
>
> kenny
>
> --
> Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
>
> "Well, I've wrestled with reality for thirty-five
> years, Doctor, and I'm happy to state I finally
> won out over it." -- Elwood P. Dowd
>
> "I'll say I'm losing my grip, and it feels terrific."
> -- Smiling husband to scowling wife, New Yorker cartoon
From: Ken Tilton
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <cdeih.23$7r7.19@newsfe11.lga>
Paul Griffioen wrote:
> I agree it's nice, but personally I don't think I would use it in my code,
> altough it is a bordercase.
>
> While your at it, why not also include (expt #C(0 1) (random 4)) in the
> license? I like that one as well. If you square it you get the original one.
Bad luck, turned up in a patent search.
ken
--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon
From: Wade Humeniuk
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <gocih.82445$hn.74817@edtnps82>
Ken Tilton wrote:
> So I want 1 or -1 at random. This looks verbose:
>
> (- 1 (* 2 (random 2)))
>
> Thoughts?
>
I think you better revise the delivery date for your algebra system.
Its going to be late by agonizing over this. Or... here is a thought:
perhaps you are done and are filling up time before you unleash the hounds.
W
From: Ken Tilton
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <bEcih.8$Ry.0@newsfe10.lga>
Wade Humeniuk wrote:
> Ken Tilton wrote:
>
>> So I want 1 or -1 at random. This looks verbose:
>>
>> (- 1 (* 2 (random 2)))
>>
>> Thoughts?
>>
>
> I think you better revise the delivery date for your algebra system.
no,no,no -- always have a deadline, it is the only way to deal with
creep. NCTM 2007 is in March, they get what I have, period.
> Its going to be late by agonizing over this.
One man's agony is another's diversion. You'd rather I relax with
sudoku? (I do /not/ get that thing.)
> Or... here is a thought:
> perhaps you are done and are filling up time before you unleash the hounds.
I am working on cloning "3 * 2.4 = 7.2" for a program called "Tilton's
Algebra" and you think I might be done?
:)
kenny
--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon
On 2006-12-20, Ken Tilton <·········@gmail.com> wrote:
> I am working on cloning "3 * 2.4 = 7.2" for a program called
> "Tilton's Algebra" and you think I might be done?
But "3 * 2.4" is 7. You're obviously thinking of "3.0 * 2.4". ;-)
--
Neil Cerutti
From: Ken Tilton
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <RAdih.17$7r7.7@newsfe11.lga>
Neil Cerutti wrote:
> On 2006-12-20, Ken Tilton <·········@gmail.com> wrote:
>
>>I am working on cloning "3 * 2.4 = 7.2" for a program called
>>"Tilton's Algebra" and you think I might be done?
>
>
> But "3 * 2.4" is 7. You're obviously thinking of "3.0 * 2.4". ;-)
>
No, 3 * 2.4 = 7.20 and 3.0 * 2.4 = 7.200 if you want to do significant
digits. As noted elsewhere, that is future work and will be done only
when explicitly on the table pedagogically.
kt
--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon
From: Ken Tilton
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <XFdih.19$7r7.6@newsfe11.lga>
Ken Tilton wrote:
>
>
> Neil Cerutti wrote:
>
>> On 2006-12-20, Ken Tilton <·········@gmail.com> wrote:
>>
>>> I am working on cloning "3 * 2.4 = 7.2" for a program called
>>> "Tilton's Algebra" and you think I might be done?
>>
>>
>>
>> But "3 * 2.4" is 7. You're obviously thinking of "3.0 * 2.4". ;-)
>>
>
> No, 3 * 2.4 = 7.20 and 3.0 * 2.4 = 7.200 if you want to do significant
> digits. As noted elsewhere, that is future work and will be done only
> when explicitly on the table pedagogically.
You may also be thinking of the rules for adding and subtracting
decimals, vs multiplying/dividing.
kt
--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon
On 2006-12-20, Ken Tilton <·········@gmail.com> wrote:
>
>
> Ken Tilton wrote:
>>
>>
>> Neil Cerutti wrote:
>>
>>> On 2006-12-20, Ken Tilton <·········@gmail.com> wrote:
>>>
>>>> I am working on cloning "3 * 2.4 = 7.2" for a program called
>>>> "Tilton's Algebra" and you think I might be done?
>>>
>>>
>>>
>>> But "3 * 2.4" is 7. You're obviously thinking of "3.0 * 2.4". ;-)
>>>
>>
>> No, 3 * 2.4 = 7.20 and 3.0 * 2.4 = 7.200 if you want to do
>> significant digits. As noted elsewhere, that is future work
>> and will be done only when explicitly on the table
>> pedagogically.
>
> You may also be thinking of the rules for adding and
> subtracting decimals, vs multiplying/dividing.
I was remembering (I hoped) Physics 2 in college. If what I said
is wrong, I better review the material. Oops!
--
Neil Cerutti
If you throw at someone's head, it's very dangerous, because in the head is
the brain. --Pudge Rodriguez
From: Ken Tilton
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <7Mdih.20$7r7.9@newsfe11.lga>
Neil Cerutti wrote:
> On 2006-12-20, Ken Tilton <·········@gmail.com> wrote:
>
>>
>>Ken Tilton wrote:
>>
>>>
>>>Neil Cerutti wrote:
>>>
>>>
>>>>On 2006-12-20, Ken Tilton <·········@gmail.com> wrote:
>>>>
>>>>
>>>>>I am working on cloning "3 * 2.4 = 7.2" for a program called
>>>>>"Tilton's Algebra" and you think I might be done?
>>>>
>>>>
>>>>
>>>>But "3 * 2.4" is 7. You're obviously thinking of "3.0 * 2.4". ;-)
>>>>
>>>
>>>No, 3 * 2.4 = 7.20 and 3.0 * 2.4 = 7.200 if you want to do
>>>significant digits. As noted elsewhere, that is future work
>>>and will be done only when explicitly on the table
>>>pedagogically.
>>
>>You may also be thinking of the rules for adding and
>>subtracting decimals, vs multiplying/dividing.
>
>
> I was remembering (I hoped) Physics 2 in college. If what I said
> is wrong, I better review the material. Oops!
>
No, sorry, you were right. I was thinking of long multiplication, and
there one is adding total decimal places, not sigdigs.
ken
--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon
From: Alain Picard
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <87r6uv3srh.fsf@memetrics.com>
Ken Tilton <·········@gmail.com> writes:
> So I want 1 or -1 at random. This looks verbose:
>
> (- 1 (* 2 (random 2)))
>
> Thoughts?
I have a handy function called ONE-OF.
You'd write
(ONE-OF -1 1)
It's both more and less verbose than your version,
depending on your POV.
(defun random-element (set)
(nth (random (length set)) set))
(defun one-of (&rest objects)
"Return one of the objects, selected at random."
(funcall #'random-element objects))
From: Bill Atkins
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <m2lkl2wti9.fsf@bertrand.local>
Alain Picard <············@memetrics.com> writes:
> (defun one-of (&rest objects)
> "Return one of the objects, selected at random."
> (funcall #'random-element objects))
(defun one-of (&rest objects)
(random-element objects))
?
Ken Tilton wrote:
> So I want 1 or -1 at random. This looks verbose:
>
> (- 1 (* 2 (random 2)))
>
> Thoughts?
>
(defun -1|1 ()
(expt -1 (random 2)))
(* 5 (-1|1))
(defun +/- (x)
(* x (expt -1 (random 2))))
(+/- 5)
> ken
>
> --
> Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
>
> "Well, I've wrestled with reality for thirty-five
> years, Doctor, and I'm happy to state I finally
> won out over it." -- Elwood P. Dowd
>
> "I'll say I'm losing my grip, and it feels terrific."
> -- Smiling husband to scowling wife, New Yorker cartoon
From: Ken Tilton
Subject: Re: There's Gotta Be A Better Way #3: To One Or Not To One
Date:
Message-ID: <vvoih.1013$7r7.620@newsfe11.lga>
········@gmail.com wrote:
> Ken Tilton wrote:
>
>>So I want 1 or -1 at random. This looks verbose:
>>
>> (- 1 (* 2 (random 2)))
>>
>>Thoughts?
>>
>
>
> (defun -1|1 ()
> (expt -1 (random 2)))
>
> (* 5 (-1|1))
Not a symbol macro?
(define-symbol-macro -1?1 (expt -1 (random 2)))
(defun -1?1 (x) (* -1?1 x))
(-1?1 42)
:)
ken
--
Algebra: http://www.tilton-technology.com/LispNycAlgebra1.htm
Warnock's Explanation for why BLISS Programs are Big: They Just
Work(tm), so they never get revisited, so they never get refined.