Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
conditional expressions
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  24 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
damien morton  
View profile  
 More options Nov 14 2008, 7:38 pm
From: "damien morton" <dmor...@bitfurnace.com>
Date: Sat, 15 Nov 2008 11:38:47 +1100
Local: Fri, Nov 14 2008 7:38 pm
Subject: [erlang-questions] conditional expressions

I keep wanting to have python-like boolean expression evaluation, in which a
value of nil, 0, an empty list or an empty tuple are considered to be false.
It would be great to have a consise way of saying "the value of this
expression is expr1 if expr1 evaluates to a non-false value, or expr2
otherwise".

In python, you would say "X = foo() or bar()", which would evaluate foo(),
and if that evaluates to one of None,False,0,[],(),{}, then evaluate bar()

I cant for the life of me figure out what the most concise way of stating
that is in erlang.

perhaps
 X = if (T1=foo()) =/= [] -> T1, false -> bar() end

it would nice to be able to say something like
 X = foo() otherwise bar().

or somesuch

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Roger Critchlow  
View profile  
 More options Nov 14 2008, 8:13 pm
From: "Roger Critchlow" <r...@elf.org>
Date: Fri, 14 Nov 2008 18:13:40 -0700
Local: Fri, Nov 14 2008 8:13 pm
Subject: Re: [erlang-questions] conditional expressions

I think you're looking for the andalso and orelse operators.  You say:

  X = foo() orelse bar(),

and bar() only gets called if the value of foo() is false.

-- rec --

2008/11/14 damien morton <dmor...@bitfurnace.com>

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
damien morton  
View profile  
 More options Nov 15 2008, 1:17 pm
From: "damien morton" <dmor...@bitfurnace.com>
Date: Sun, 16 Nov 2008 05:17:52 +1100
Local: Sat, Nov 15 2008 1:17 pm
Subject: Re: [erlang-questions] conditional expressions

1> case [] of [] -> X = aaa; X -> ok end.
aaa
2> case bbb of [] -> X = aaa; X -> ok end.
** exception error: no case clause matching bbb

am I missing something?

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mats cronqvist  
View profile  
 More options Nov 15 2008, 12:42 pm
From: mats cronqvist <ma...@kreditor.se>
Date: Sat, 15 Nov 2008 18:42:53 +0100
Local: Sat, Nov 15 2008 12:42 pm
Subject: Re: [erlang-questions] conditional expressions

Richard Carlsson <richa...@it.uu.se> writes:

> X = case foo() of
>        [] -> bar();
>        X1 -> X1
>      end

  strangely, this also works;

case foo() of
  [] -> X = bar();
  X -> ok
end

  as long as X is bound in each clause, you're golden.

  this might qualify as a gotcha.

  mats
_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Vance Shipley  
View profile  
 More options Nov 15 2008, 2:35 pm
From: Vance Shipley <van...@motivity.ca>
Date: Sat, 15 Nov 2008 14:35:22 -0500
Local: Sat, Nov 15 2008 2:35 pm
Subject: Re: [erlang-questions] conditional expressions

On Sun, Nov 16, 2008 at 05:17:52AM +1100, damien morton wrote:
}  am I missing something?

Just the variable scope.  Once you have run:

     1> case [] of [] -> X = aaa; X -> ok end.      
     aaa

The variable X is now bound:

     2> X.
     aaa

If you forget it first:

     3> f(X).
     ok

Your second example will work:

     4> case bbb of [] -> X = aaa; X -> ok end.      
     ok

        -Vance
_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael McDaniel  
View profile  
 More options Nov 15 2008, 1:40 pm
From: Michael McDaniel <erla...@autosys.us>
Date: Sat, 15 Nov 2008 10:40:53 -0800
Local: Sat, Nov 15 2008 1:40 pm
Subject: Re: [erlang-questions] conditional expressions
On Sun, Nov 16, 2008 at 05:17:52AM +1100, damien morton wrote:
>    1> case [] of [] -> X = aaa; X -> ok end.

>    aaa

>    2> case bbb of [] -> X = aaa; X -> ok end.

>    ** exception error: no case clause matching bbb

>    am I missing something?

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

 From section '6.8 Case' of the Erlang Reference Manual (Version 5.6.5)

"If there is no matching pattern with a true guard sequence, a case_clause run-time error will occur."

 bbb does not match anything in the second case above; you could substitute
 aaa for X in the second case and it would result the same.

3> X.
aaa

~Michael

--
Michael McDaniel
Portland, Oregon, USA
http://autosys.us

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Carlsson  
View profile  
 More options Nov 15 2008, 6:49 am
From: Richard Carlsson <richa...@it.uu.se>
Date: Sat, 15 Nov 2008 12:49:58 +0100
Local: Sat, Nov 15 2008 6:49 am
Subject: Re: [erlang-questions] conditional expressions

damien morton wrote:
> I keep wanting to have python-like boolean expression evaluation, in
> which a value of nil, 0, an empty list or an empty tuple are considered
> to be false.

> It would be great to have a consise way of saying "the value of this
> expression is expr1 if expr1 evaluates to a non-false value, or expr2
> otherwise".

> In python, you would say "X = foo() or bar()", which would evaluate
> foo(), and if that evaluates to one of None,False,0,[],(),{}, then
> evaluate bar()

Well, andalso/orelse (as someone suggested) don't work since they
require that expr1 and expr2 evaluate to booleans and nothing else.

> I cant for the life of me figure out what the most concise way of
> stating that is in erlang.

> perhaps
>  X = if (T1=foo()) =/= [] -> T1, false -> bar() end

> it would nice to be able to say something like
>  X = foo() otherwise bar().

X = case foo() of
       [] -> bar();
       X1 -> X1
     end

If you need to check for other values as well, replace '[] ->' with
'X when X =:= [] ; X =:= 0 ; ... ->'

But I've always felt that this feature of Python/Perl/... boils down
to sloppy programming style. It basically means that the caller hopes
that the "empty or failure" case is signalled by one of the values
reconized as pseudo-booleans by the language (the programmer might
not actually know the exact interface of the called function, but
guessed that this would work), and the resulting code says nothing to
the reader about the actual set of return values. Furthermore, the
code might do the wrong thing if the function tries to return e.g. '0'
or '{}' on success (as opposed to False or None or whatever it usually
uses for failure). It simply makes the code a lot less tight than it
ought to be. And then, you still can't use the same idiom on abstract
data types to treat e.g. an empty set as "false".

     /Richard
_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hynek Vychodil  
View profile  
 More options Nov 15 2008, 1:37 pm
From: "Hynek Vychodil" <vychodil.hy...@gmail.com>
Date: Sat, 15 Nov 2008 19:37:39 +0100
Local: Sat, Nov 15 2008 1:37 pm
Subject: Re: [erlang-questions] conditional expressions

2008/11/15 damien morton <dmor...@bitfurnace.com>

> 1> case [] of [] -> X = aaa; X -> ok end.
> aaa
> 2> case bbb of [] -> X = aaa; X -> ok end.
> ** exception error: no case clause matching bbb

> am I missing something?

Yes, you missed that shell binds X to aaa in previous expression ;-)

But I think, less magic will be

X = case foo() of
      [] -> bar();
      Y -> Y
  end

--
--Hynek (Pichi) Vychodil

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
damien morton  
View profile  
 More options Nov 15 2008, 10:09 am
From: "damien morton" <dmor...@bitfurnace.com>
Date: Sun, 16 Nov 2008 02:09:43 +1100
Local: Sat, Nov 15 2008 10:09 am
Subject: Re: [erlang-questions] conditional expressions

Would {}/{X} be more clear for 0 or 1 answers, and []/[...] for 0 or more
answers?

Not too keen on the none/{value,Value} approach - it feels irregular
somehow.

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Robert Virding  
View profile  
 More options Nov 15 2008, 4:42 pm
From: "Robert Virding" <rvird...@gmail.com>
Date: Sat, 15 Nov 2008 22:42:46 +0100
Local: Sat, Nov 15 2008 4:42 pm
Subject: Re: [erlang-questions] conditional expressions

You are evaluating these in the shell. In the first case X is bound to aaa,
which means that when you evaluate the second case X is already bound. Which
means that there is no matching clause. If you do:

1> case [] of [] -> X = aaa; X -> ok end.
aaa
2> case bbb of [] -> Y = aaa; Y -> ok end.
ok
3> {X,Y}.
{aaa,bbb}

it works.

Robert

2008/11/15 damien morton <dmor...@bitfurnace.com>

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fredrik Svahn  
View profile  
 More options Nov 15 2008, 4:22 pm
From: "Fredrik Svahn" <fredrik.sv...@gmail.com>
Date: Sat, 15 Nov 2008 22:22:04 +0100
Local: Sat, Nov 15 2008 4:22 pm
Subject: Re: [erlang-questions] conditional expressions

X is bound to aaa in the first line you enter in the shell, so your second
line is really "case bbb of [] -> aaa = aaa; aaa -> ok end.".

You can unbind/forget X in the shell with f() or f(X), see help().

1> case [] of [] -> X = aaa; X -> ok end.
aaa
2> X.
aaa
3> case bbb of [] -> X = aaa; X -> ok end.
** exception error: no case clause matching bbb
4> X.
aaa
5> f(X).
ok
6> X.
* 1: variable 'X' is unbound
7> case bbb of [] -> X = aaa; X -> ok end.
ok
8> X.
bbb

2008/11/15 damien morton <dmor...@bitfurnace.com>

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Fwd: conditional expressions" by damien morton
damien morton  
View profile  
 More options Nov 15 2008, 9:22 am
From: "damien morton" <dmor...@bitfurnace.com>
Date: Sun, 16 Nov 2008 01:22:03 +1100
Local: Sat, Nov 15 2008 9:22 am
Subject: [erlang-questions] Fwd: conditional expressions

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hynek Vychodil  
View profile  
 More options Nov 16 2008, 8:19 am
From: "Hynek Vychodil" <vychodil.hy...@gmail.com>
Date: Sun, 16 Nov 2008 14:19:27 +0100
Local: Sun, Nov 16 2008 8:19 am
Subject: Re: [erlang-questions] Fwd: conditional expressions

2008/11/15 damien morton <dmor...@bitfurnace.com>

It happen because there is not pattern matching in python, perl, ruby, ...
but you don't need it in erlang since there is pattern matching in erlang.

> _______________________________________________
> erlang-questions mailing list
> erlang-questi...@erlang.org
> http://www.erlang.org/mailman/listinfo/erlang-questions

--
--Hynek (Pichi) Vychodil

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "conditional expressions" by Richard O&#39;Keefe
Richard O'Keefe  
View profile  
 More options Nov 17 2008, 10:20 pm
From: "Richard O'Keefe" <o...@cs.otago.ac.nz>
Date: Tue, 18 Nov 2008 16:20:55 +1300
Local: Mon, Nov 17 2008 10:20 pm
Subject: Re: [erlang-questions] conditional expressions

On 15 Nov 2008, at 1:38 pm, damien morton wrote:

> I keep wanting to have python-like boolean expression evaluation, in  
> which a value of nil, 0, an empty list or an empty tuple are  
> considered to be false.

You could do it via a macro that expanded
        ?PY_OR(e1, e2)
to
        case py_val(e1)
           of {X} -> X
            ; false -> e2
        end

where
py_val(undef)  -> false;     % Closer match for Undef than nil is
py_val(0)      -> false;
py_val(0.0)    -> false;
py_val([])     -> false;
py_val({})     -> false;
py_val(false)  -> false;
py_val(X)      -> {X}.

However, this really is not very Erlang-like.
(For one thing, it's not very Dialyser-friendly.)

Lisp had a single value used to represent logical falsehood (NIL),
the empty list ('() => NIL), and a three-letter atom ('NIL => NIL).
Scheme found it very useful to separate them (#f, '(), and 'NIL
are all different in Scheme).  Erlang is closer to Scheme here.

It may be that you are trying to write Python in Erlang,
which isn't the best way to use Erlang.
It may also be that you don't have (m)any cases where
ALL of the 'false' values listed above are actually possible,
just many cases where one of them is possible and you would
like to use the same operator for all of them.  Well, 'case'
is definitely the construct you want.

Can you provide some examples?

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard O'Keefe  
View profile  
 More options Nov 17 2008, 10:24 pm
From: "Richard O'Keefe" <o...@cs.otago.ac.nz>
Date: Tue, 18 Nov 2008 16:24:10 +1300
Local: Mon, Nov 17 2008 10:24 pm
Subject: Re: [erlang-questions] conditional expressions

On 15 Nov 2008, at 2:13 pm, Roger Critchlow wrote:

> I think you're looking for the andalso and orelse operators.  You say:

>   X = foo() orelse bar(),

> and bar() only gets called if the value of foo() is false.

However there is a strong restriction in Erlang that does not
exist in Lisp, Scheme, Smalltalk, &c and does not exist in
Python.
        False or 42             => 42 in Python
        42 or False             => 42 in Python
        1 == 2 orelse 42        => ERROR! in Erlang
        42 orelse 1 == 2        => ERROR! in Erlang

In Erlang, both operands of an andalso or orelse operator
must return 'true' or 'false'.  I strongly suspect that the
OP wants a non-empty list or tuple to count as true, rather
than as an error.

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Serge Aleynikov  
View profile  
 More options Nov 17 2008, 10:38 pm
From: Serge Aleynikov <sal...@gmail.com>
Date: Mon, 17 Nov 2008 22:38:49 -0500
Local: Mon, Nov 17 2008 10:38 pm
Subject: Re: [erlang-questions] conditional expressions
Your 'must' statement is not entirely accurate.  Consider this:

1 == 2 orelse throw(no_bool_return).

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard O'Keefe  
View profile  
 More options Nov 18 2008, 4:52 pm
From: "Richard O'Keefe" <o...@cs.otago.ac.nz>
Date: Wed, 19 Nov 2008 10:52:24 +1300
Local: Tues, Nov 18 2008 4:52 pm
Subject: Re: [erlang-questions] conditional expressions

On 18 Nov 2008, at 4:38 pm, Serge Aleynikov wrote:

> Your 'must' statement is not entirely accurate.

Was there any doubt about what I meant?
Let's try again:
    both 'andalso' and 'orelse' in Erlang have the
    following properties:
        the first operand must either return 'true' or 'false'
        or raise an exception;
        the second operand, if executed, must either return
        'true' or 'false' or raise an exception;
        the Dialyzer will complain if either operand looks
        as if it will return something other than 'false' or 'true';
    and above all:
        you CANNOT use them to get the same effect as
        Lisp's (OR - -) and (AND - -) or Python's similar
        operators, which allow a normal result from the
        second operand to be anything at all.

I have in fact complained about this, not because I particularly
want non-Boolean results from these operators, but because the
code that's inserted to check makes them non-tail-recursive.

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mikael Pettersson  
View profile  
 More options Nov 19 2008, 4:08 am
From: Mikael Pettersson <mi...@it.uu.se>
Date: Wed, 19 Nov 2008 10:08:32 +0100
Local: Wed, Nov 19 2008 4:08 am
Subject: Re: [erlang-questions] conditional expressions
Richard O'Keefe writes:

 >
 > On 18 Nov 2008, at 4:38 pm, Serge Aleynikov wrote:
 >
 > > Your 'must' statement is not entirely accurate.
 >
 > Was there any doubt about what I meant?
 > Let's try again:
 >     both 'andalso' and 'orelse' in Erlang have the
 >     following properties:
 >   the first operand must either return 'true' or 'false'
 >   or raise an exception;
 >   the second operand, if executed, must either return
 >   'true' or 'false' or raise an exception;
 >   the Dialyzer will complain if either operand looks
 >   as if it will return something other than 'false' or 'true';
 >     and above all:
 >   you CANNOT use them to get the same effect as
 >   Lisp's (OR - -) and (AND - -) or Python's similar
 >   operators, which allow a normal result from the
 >   second operand to be anything at all.
 >
 > I have in fact complained about this, not because I particularly
 > want non-Boolean results from these operators, but because the
 > code that's inserted to check makes them non-tail-recursive.

Indeed, the failure of these operators to be properly
tail-recursive makes them utterly useless, and dangerous.

The HiPE compiler once had a performance bug in its
register allocator because some code used an 'andalso'
to control a tail-recursive computation. Needless to say
I've since placed a ban on andalso/orelse in the HiPE code
I maintain.
_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Mercer  
View profile  
 More options Nov 19 2008, 10:10 am
From: "David Mercer" <dmer...@gmail.com>
Date: Wed, 19 Nov 2008 09:10:09 -0600
Local: Wed, Nov 19 2008 10:10 am
Subject: Re: [erlang-questions] conditional expressions
Can someone please explain the tail recursion problem with andalso?  Thanks.

David

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Carlsson  
View profile  
 More options Nov 19 2008, 10:28 am
From: Richard Carlsson <richa...@it.uu.se>
Date: Wed, 19 Nov 2008 16:28:24 +0100
Local: Wed, Nov 19 2008 10:28 am
Subject: Re: [erlang-questions] conditional expressions

David Mercer wrote:
> Can someone please explain the tail recursion problem with andalso?  Thanks.

It checks that the return value of the right hand side is a boolean,
so it must wait for the evaluation to return; it can't just do a tail
call if the left hand side evaluates to true. (The OTP folks thought
that error checking was more important than tail recursion for these
operators.)

    /Richard
_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bjorn Gustavsson  
View profile  
 More options Nov 19 2008, 12:24 pm
From: "Bjorn Gustavsson" <bgustavs...@gmail.com>
Date: Wed, 19 Nov 2008 18:24:46 +0100
Local: Wed, Nov 19 2008 12:24 pm
Subject: Re: [erlang-questions] conditional expressions

On Wed, Nov 19, 2008 at 4:28 PM, Richard Carlsson <richa...@it.uu.se> wrote:
> David Mercer wrote:
>> Can someone please explain the tail recursion problem with andalso?  Thanks.

> It checks that the return value of the right hand side is a boolean,
> so it must wait for the evaluation to return; it can't just do a tail
> call if the left hand side evaluates to true. (The OTP folks thought
> that error checking was more important than tail recursion for these
> operators.)

Maybe we should reconsider that decision for R13.

I agree with Mikael that andalso/orelse are both useless and dangerous.

/Bjorn
--
Björn Gustavsson, Erlang/OTP, Ericsson AB
_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Mercer  
View profile  
 More options Nov 19 2008, 12:26 pm
From: "David Mercer" <dmer...@gmail.com>
Date: Wed, 19 Nov 2008 11:26:45 -0600
Local: Wed, Nov 19 2008 12:26 pm
Subject: Re: [erlang-questions] conditional expressions

Richard Carlsson wrote:
> David Mercer wrote:
> > Can someone please explain the tail recursion problem with andalso?
> Thanks.

> It checks that the return value of the right hand side is a boolean,
> so it must wait for the evaluation to return; it can't just do a tail
> call if the left hand side evaluates to true. (The OTP folks thought
> that error checking was more important than tail recursion for these
> operators.)

Thanks for the reply, but I still do not understand.  If the first operand
evaluates to true the second operand must necessarily be checked, since they
both have to be true in order for the expression to evaluate to true.  What
am I missing (or mißing :-) )?

Cheers,

David

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard Carlsson  
View profile  
 More options Nov 19 2008, 1:26 pm
From: Richard Carlsson <richa...@it.uu.se>
Date: Wed, 19 Nov 2008 19:26:33 +0100
Local: Wed, Nov 19 2008 1:26 pm
Subject: Re: [erlang-questions] conditional expressions

If the LHS is true, then it all hangs on the RHS, so you can A) call it
with a tail call (if the result of the andalso will be returned as the
result of the current function) and hope that it evaluates to a boolean
as it ought to, or B) call it, get the return value, check that it is
indeed a boolean, and either return it as it is or throw an exception
if it is not a boolean.

The current implementation does B, which makes it less suitable for
this  kind of thing:

   all_true([H|T]) -> H andalso all_true(T);
   all_true([]) -> true.

which would be a simple traversal if the implementation was A,
but as it is, we have to return down the stack and check that we
are getting neat little booleans all the way. (Yes, type analysis
could sometimes save the day, but far from always.)

On the other hand, B prevents uses such as this:

   maybe_goodbye(X) -> X andalso io:format("adieu").

(which returns false if X is false, and otherwise prints
a message and returns ok).

     /Richard
_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Richard O'Keefe  
View profile  
 More options Nov 19 2008, 10:01 pm
From: "Richard O'Keefe" <o...@cs.otago.ac.nz>
Date: Thu, 20 Nov 2008 16:01:02 +1300
Local: Wed, Nov 19 2008 10:01 pm
Subject: Re: [erlang-questions] conditional expressions

On 20 Nov 2008, at 4:10 am, David Mercer wrote:

> Can someone please explain the tail recursion problem with andalso?

SML:

fun all(f, []) = true
   | all(f, x::xs) = f x andalso all(f, xs);

The recursive call here is a tail call:
when that recursive call is finished there is nothing
to be done with the result except to return it.

Erlang:

all(_, []) -> true;
all(F, [X|Xs]) -> F(X) andalso all(F, Xs).

The recursive call here is NOT a tail call,
because it is semantically the equivalent of

all(_, []) -> true;
all(F, [X|Xs]) ->
     case F(X)
       of false -> false
        ; true -> R = all(F, Xs),
                  if R == true ; R == false -> R end
     end.

Of course SML and Haskell don't need to check because the
type system means that the code that gets past the compiler
couldn't possibly fail such a check.  But Lisp and Smalltalk
and Python don't bother making any such check, even though
it _might_ fail if it _were_ done.

Skipping the check on R doesn't seem to be much of a problem
in practice.  Loss of tail recursion here IS a problem.

_______________________________________________
erlang-questions mailing list
erlang-questi...@erlang.org
http://www.erlang.org/mailman/listinfo/erlang-questions


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »