[erlang-questions] Local namespaces in case/try

Dmitry Groshev <>
Sun Jul 15 12:40:23 CEST 2012


Richard,

don't you think about case as some sort of imperative construction? Because 
I can't see why "case" can't be that "keyword to introduce new variable" — 
case is isolated in some sense. It was already done with list 
comprehensions, after all. Moreover, this is the case in Haskell that 
you've mentioned:

Prelude> let a = [1, 2, 3]
Prelude> case a of [b, c, d] -> "ok"
"ok"
Prelude> b
<interactive>:6:1: Not in scope: `b'

So does OCaml:

# let a = [1; 2; 3] ;;
val a : int list = [1; 2; 3]
# match a with
    [b; c; d] -> b
  | _ -> 0 ;;
- : int = 1
# b ;;
Error: Unbound value b


So it's more like some obscure "feature" of Erlang for newcomers, which is 
no good.
Speaking about better example:

test(Input) ->
    Result = case test1(Input) of
                 {ok, BetterInput} ->
                     case test2(BetterInput) of
                         {ok, BestInput} ->
                             do_something(BestInput);
                         {error, _Reason}=Error ->
                             Error
                     end;
                 {error, _Reson}=Error ->
                     Error
             end,
    case Result of
        {ok, _}=Ok -> Ok;
        {error, _Reason}=Error -> %% OOPS!
            do_something_on_error(Error)
    end.

I can come up with a few solutions to this compile-time error, but I can't 
understand why I should. Correct indentation solves a problem of reasoning 
about nested constructions.

On Wednesday, July 11, 2012 5:35:07 AM UTC+4, Richard O'Keefe wrote:
>
>
> On 10/07/2012, at 10:00 PM, Dmitry Groshev wrote: 
>
> > Exactly what I was talking about. I can't see any sense in bloating the 
> code with nonsensical functions 
>
> It isn't the functions that are nonsensical, it is 
> using the same variable with two different meanings 
> which is in the strict literal sense of the word nonsensical. 
>
> Give us a serious example! 
> Show us some code that is easier to read if you do this! 
>
> Yes, other functional languages have nested variable scopes within a 
> single function (although the semantics is often defined in terms of 
> nested functions), BUT those languages require you to introduce new 
> bindings with keywords 
>
>     (define (f X) 
>       (let ((R) (E))              ;;; HERE! 
>         (let ((V (do-something))) 
>           (case (car V) 
>             ((result) (set! R (cadr V))) 
>             ((error)  (set! E (cadr V)))))) 
>       (let ((R) (E))              ;;; HERE! 
>           (case (car V) 
>             ((result) (set! R (cadr V))) 
>             ((error)  (set! E (cadr V))))))) 
>
> Lisp and Scheme have LET and LET*. 
> ML has let. 
> Haskell has let and where. 
>
> If you want Lisp-Flavoured Erlang, it exists. 
>
> _______________________________________________ 
> erlang-questions mailing list 
>  
> http://erlang.org/mailman/listinfo/erlang-questions 
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120715/7ea6da75/attachment.html>


More information about the erlang-questions mailing list