TDWTF: Russian Peasant Multiplication

Started by Kirill, July 22, 2009, 05:34:53 AM

Previous topic - Next topic

Kirill

Please see this post: http://thedailywtf.com/Articles/Programming-Praxis-Russian-Peasant-Multiplication.aspx">Russian Peasant Multiplication



It ends with:


Quote
Your challenge: write a function that multiplies two numbers using the Russian peasant algorithm. There is no language restriction, though anything on the esoteric language list will probably be ignored. Spoiler alert: the solution(s) will undoubtedly appear in the comments.


There is no LISP solution there yet...

cormullion

#1
You can find Lutz' (I assume!) typically elegant solution http://thedailywtf.com/Comments/Programming-Praxis-Russian-Peasant-Multiplication.aspx?pg=2#277861">there now.

xytroxon

#2
Quote from: "cormullion"You can find Lutz' (I assume!) typically elegant solution http://thedailywtf.com/Comments/Programming-Praxis-Russian-Peasant-Multiplication.aspx?pg=2#277861">there now.


Re: Programming Praxis: Russian Peasant Multiplication

2009-07-22 14:51 • by newlisp.org (unregistered)

   

278283 in reply to 277861

Reply Quote

improvement for some problems starting with even number:



(define (rmul x y , (s 0))

  (until (= x 1)

    (unless (zero? (% x 2))

      (inc s y))

    (setq x (>> x) y (<< y)))

  (+ y s)

)


I think I found it... 9 pages of code entries posted so far? Al Gore won't be very happy over all the CO2 generated ;)



-- xytroxon
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

TedWalther

#3
Quote from: "cormullion"You can find Lutz' (I assume!) typically elegant solution http://thedailywtf.com/Comments/Programming-Praxis-Russian-Peasant-Multiplication.aspx?pg=2#277861">there now.


Thanks for the link.  I was so inspired, I dashed off this one-liner:



(define (rmul x y) (if (= x 1) y (+ (if (zero? (% x 2)) 0 y) (rmul (>> x) (<< y)))))


Ted
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

newdep

#4
QuoteI think I found it... 9 pages of code entries posted so far? Al Gore won't be very happy over all the CO2 generated ;)


I think I all people on this planet would stop talking for 1 day we solved the CO2 problem already..thats including Al.. ;-)





I Like these brain trainers.. But Cormullion posted already the trick...<<iekss>>..now I need to wait for another one to come by.. I was too quick in clicking..;-)
-- (define? (Cornflakes))

TedWalther

#5
Here is the same solution, but with more normal indenting.  Still ends up being 6 line, like Lutz' solution, but uses tail recursion and maybe does fewer assignments?



(define (rmul x y)
  (if (= x 1)
      y
    (+
     (if (zero? (% x 2)) 0 y)
     (rmul (>> x) (<< y)))))
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

TedWalther

#6
Even better, the even? predicate could be written like this:



(= (& x 1) 0)


I really wish that 0 could be included as a value for "false" in numeric contexts.  That is the one thing from C and other languages that I miss.



I often find myself wishing that "" and 0 as well as nil were counted as false values.



Ted
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

TedWalther

#7
Desired False values:

0 (integer or float)

"" (empty string)



Current False values:

false (symbol)

nil (symbol)

() (empty list)



I do know that there are reasons for not allowing "" or 0 as false values.  Can someone fill me in?  As a former C programmer, I find them very comfortable idioms.



Ted
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

TedWalther

#8
Even shorter version of the russian peasant:



(define (rmul x y)
  (if (= x 1)
      y
    (+
     (* (& x 1) y)
     (rmul (>> x) (<<y)))))
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

TedWalther

#9
accidentical duplicate, please delete this post.
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.

Lutz

#10
QuoteI do know that there are reasons for not allowing "" or 0 as false values. Can someone fill me in? As a former C programmer, I find them very comfortable idioms.




try 'null?' which combines nil?, empty? and zero?


(map null? '(() nil "" 0 0.0)) => (true true true true true)



ps: note that 'false' is not a built-in symbol, it only works, if nobody has set it.



ps2: maintenance release 10.1.1 is on its way

TedWalther

#11
Thanks Lutz.  Maybe I'll start using (if-not-null ...) in my code now.  Not as satisfying as a straight (if) statement, not as impressive in a code contest like the russian peasant one, but it will work.  Are there reasons somewhere for not having (not (null? ..)) as the default for (if, or, and, case) and relatives?  Just LISP tradition?  I did read a JavaScript book by a LISP guy where he was really negative about that very feature of JavaScript, but I couldn't quite make out why he thought it was a bad feature.



Ted


Quote from: "Lutz"
QuoteI do know that there are reasons for not allowing "" or 0 as false values. Can someone fill me in? As a former C programmer, I find them very comfortable idioms.




try 'null?' which combines nil?, empty? and zero?


(map null? '(() nil "" 0 0.0)) => (true true true true true)



ps: note that 'false' is not a built-in symbol, it only works, if nobody has set it.



ps2: maintenance release 10.1.1 is on its way
Cavemen in bearskins invaded the ivory towers of Artificial Intelligence.  Nine months later, they left with a baby named newLISP.  The women of the ivory towers wept and wailed.  \"Abomination!\" they cried.