[erlang-questions] Style wars: junk comments

Fred Hebert <>
Thu Sep 13 03:38:57 CEST 2012


Some Schemes also allow things like:

(define (fib x)
    (define (fib-rec n a b)
       (cond [(= n 0) b]
             [true (fib-rec (- n 1) b (+ a b))]))
    (fib-rec x 0 1))

which can be called as(fib 4). This allows to define private or helper 
functions within the context of their main function, without needing 
destructive assignment or whatever, and then later on executing it. 
Python allows something similar:

def fib(x):
     def fib_rec(x, a, b):
         if x == 0:
             return b
         else:
             return fib_rec(x-1, b, a+b)
     return fib_rec(x, 0, 1)

though it would be nicer if Python indeed had tail recursion ;) Whether 
the functions come before or after the final higher-level call doesn't 
especially matter to me, but code becomes way nicer to read to me when 
described that way.

You can separate one-use helpers from module-wide helpers that way, too.

Nothing would keep us from doing something similar in Erlang iff 
anonymous functions were not so anonymous (and if self-recursion in 
anonymous function wasn't that annoying to read).

On 12-09-12 5:59 PM, Richard O'Keefe wrote:
> On 12/09/2012, at 11:48 PM, Fred Hebert wrote:
>
>> This is the approach I like the most.
>>
>> It's something I picked up when working with Scheme, where functions can contain the definitions of their helpers within them, which made the code much clearer to me.
> Scheme and ML have features that make this rather more useful
> than it is in languages like Clean and Haskell.  In ML you can
> write
>     local
>         <private definitions>
>     in
>         <public definitions>
>     end
>
> and this has the effect that the things defined in
> <private definitions> are visible in <public
> definitions>, but not outside, while the things
> defined in <public definitions> are visible outside.
> For example, you might have
>
>     local
>        fun rebalance t = ...
>     in
>        fun add k v t = ...
>        fun del k t = ...
>     end
>
> Scheme lets you hack this by doing
>     (define add)
>     (define del)
>     (letrec ((rebalance (lambda (t) ...))
>       (set! add (lambda (k v t) ...))
>       (set! del (lambda (k t) ...))
>       'ok)
>
> I see this quite a lot in Scheme.
> If Joe's still thinking about Erlang 2, I hope he'll consider
> the functionality of local-in-end.
>
>




More information about the erlang-questions mailing list