RE: Varför kan jag inte inte anropa A -- B -- C ?

Erik Stenman Erik.Stenman@REDACTED
Tue Oct 14 12:38:48 CEST 2003


Interesting '--' is apparently evaluated right to left:
1> [1,2,3,4,5,6]--[1,2,3,4]--[4].
[4,5,6]
Using parentheses gives you the expected result:
12> (([1,2,3,4,5,6]--[1,2])--[3,4])--[5,6].     
[]
But the "spec" says: Operators with the same priority are evaluated left to
right.
See:
http://www.erlang.org/doc/r9c/doc/reference_manual/part_frame.html
 6.23 Operator Precedence
 Operator precedence in falling priority:
 :
 #
 Unary + - bnot not
 / * div rem band and
 + - bor bxor bsl bsr or xor
 ++ --
 == /= =< < >= > =:= =/=
 andalso
 orelse
 = !
 catch

 When evaluating an expression, the operator with the highest priority is
evaluated first. 
 Operators with the same  priority are evaluated left to right. Example:

 6 + 5 * 4 - 3 / 2 evaluates to
 6 + 20 - 1.5 evaluates to
 26 - 1.5 evaluates to
 24.5

Looking at erl_parse.yrl one can see that list_op ('--' and '++') are right
associated.
So either the documentation or the implementation is wrong.
Changing line 105 from:
expr_300 -> expr_400 list_op expr_300 :
	mkop('$1', '$2', '$3').
expr_300 -> expr_400 : '$1'.
to:
expr_300 -> expr_300 list_op expr_400 :
	mkop('$1', '$2', '$3').
expr_300 -> expr_400 : '$1'.
Will give you a left associated parser:
Eshell V5.3  (abort with ^G)
1> [1,2,3,4,5,6]--[1,2]--[3,4]--[5,6].
[]


/Erik

> Lennart Öhman wrote
> 
> Hi, the -- operator works with two arguments. I think you 
> shall concider what you did as:
> 
> [1,2,3,4,5,6] -- (A -- (B -- C)).
> 
> If you try
> 
> ((([1,2,3,4,5,6] -- A) -- B) -- C).
> 
> I beleive you will accomplish what you intended.
> 
> Best Regards,
> Lennart
> 
> 
> Anders Fluur (LN/EAB) wrote:
> > Hej,
> > Varför kan jag inte inte anropa A -- B -- C, när A ++ B ++ 
> C fungerar?
> >  
> > (erlang)82 <mailto:e_Erlang__Global_pm1_1_2_1@)82>> A = [1,2]. [1,2]
> > (erlang)83 <mailto:erlang__Global_pm1_1_2_1@)83>> B=[3,4].
> > [3,4]
> > (erlang)84 <mailto:e_Erlang__Global_pm1_1_2_1@)84>> C=[5,6].
> > [5,6]
> > (erlang)85 <mailto:erlang__Global_pm1_1_2_1@)85>> 
> [1,2,3,4,5,6]--A--B--C.
> > [3,4,5,6]
> > (erlang)86 <mailto:e_Erlang__Global_pm1_1_2_1@)86>> A++B++C.
> > [1,2,3,4,5,6]
> >  
> > Mvh
> > Anders
> > 
> 
> 
> -------------------------------------------------------------
> Lennart Ohman                   phone   : +46-8-587 623 27
> Sjoland & Thyselius Telecom AB  cellular: +46-70-552 6735
> Sehlstedtsgatan 6               fax     : +46-8-667 8230
> SE-115 28 STOCKHOLM, SWEDEN     email   : lennart.ohman@REDACTED
> 
> 




More information about the erlang-questions mailing list