Evan Martin (evan) wrote in evan_tech,
Evan Martin
evan
evan_tech

all i have to talk about is schoolwork these days


Man, this is ugly. Multiplication is implemented recursively as repeated addition, then exponentiation on top of that as repeated multiplication. But it works! And type-checks! Give it two integers and it gives you n^m.

Sadly, recursive functions mean that it's no longer guaranteed to terminate.
(fn n:int.
  (rec pwr:int->int m.
    (if m > 0 then
      (rec mul:int->int y.
        (if y > 0 then
          n+(mul (y + (-1)))
        else
          0)
      ) (pwr (m + (-1)))
    else
      1
    )
  )
)
I was about to rewrite in in Python just for fun, but then I realized you can't (?) make recursive functions with Python lambdas.
Subscribe

  • livejournal kids

    Neat image from Jack Dorsey. Every so often someone will ask me about Twitter and I'll dig up a a random day from Brad's LJ in 1999 and talk about…

  • megaupload captcha

    Someone make a Javascript-based captcha cracker for megaupload. It's strange to see those captchas again because I idly myself wrote a…

  • zombie ghosd

    I was tickled to discover another IBM developerworks article on one of my abandoned hacks and that both it and its predecessor have been translated…

  • Post a new comment

    Error

    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 5 comments