POV-Ray : Newsgroups : povray.off-topic : Haskell humour : Re: Haskell humour Server Time
7 Sep 2024 05:11:36 EDT (-0400)
  Re: Haskell humour  
From: nemesis
Date: 1 Oct 2008 20:06:38
Message: <48e4108e$1@news.povray.org>
Orchid XP v8 wrote:
> Well, if you already know how many parameters each function takes, that 
> could work.

In the example I gave, all functions are known, because they are locally 
defined.  Other than that:

*lambda* creates a new function by taking a list of parameters and 
several subexpressions as body => (lambda (p1 p2 ...) body ...)

*let* takes a list of local bindings and then may have several 
subexpressions as body => (let ((b1 v1) (b2 v2) ...) body ...)

let is just a macro for ((lambda (b1 b2 ...) body ...) v1 v2 ...)
That is, the application of values to the function. :)

*if* takes a condition and has at least one branch => (if foo this) or 
(if foo this that)

*or* takes several subexpressions and evaluates them with logical or
=> (or foo bar ...)

> (Also, the example you mosted has some fairly unhelpful indentation in it.)

BTW, it's problem 1 from Euler, except twisted a bit.  I didn't know you 
already knew of it. :P

(let ((%? (lambda (a b) (zero? (modulo a b)))))
   (let sum ((i 5) (r 0))
     (if (= i 100000) r
         (sum (+ 1 i) (if (or (%? i 5) (%? i 7))
                          (+ r i)
                          r)))))

I don't think it's particularly bad indented.  In named lets, I tend to 
call it like a function, all parameters in the same line.  Lisp 
indenting generally puts only the first parameter in the same line, the 
others go below, indented at the same line as the first parameter.

Anyway, it reads like this:

 > (let ((%? (lambda (a b) (zero? (modulo a b)))))
let %? be a function taking a b as arguments and saying if a is 
divisible by b

 >  (let sum ((i 5) (r 0))
then let sum be a function taking (i initially 5) and (r initially 0)

 >    (if (= i 100000) r
and returning r if i == 100000

 >        (sum (+ 1 i) (if (or (%? i 5) (%? i 7))
 >                         (+ r i)
 >                         r)))))
or sum of (incremented i) and ((r+i) -- if i divisible by 5 or by 7 -- 
(or r otherwise))

Is it really that hard?  Perhaps I've lost my mind in parentheses and 
can now see clearly through the Matrix code stream... :D


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.