Re: Rather simple function returns curious result. Explanation requested.

*To*: mathgroup at smc.vnet.net*Subject*: [mg131403] Re: Rather simple function returns curious result. Explanation requested.*From*: "Nasser M. Abbasi" <nma at 12000.org>*Date*: Wed, 3 Jul 2013 05:00:46 -0400 (EDT)*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com*Delivered-to*: l-mathgroup@wolfram.com*Delivered-to*: mathgroup-outx@smc.vnet.net*Delivered-to*: mathgroup-newsendx@smc.vnet.net*References*: <kqtl8l$ath$1@smc.vnet.net>*Reply-to*: nma at 12000.org

On 7/1/2013 11:34 PM, James Stein wrote: > For reasons that escape me, the simple function below fails to return > an empty List when n==2; Is this a bug? If not, what is the > explanation? If the return value for n==2 is changed to an integer or > a string, the function behaves as expected.(Of course, this bizarre > function is the result of simplifying a more reasonable one.) > > Clear[ f ]; > f [run : { List___ } ] := Module [ { n }, > n = run // Length; > If [ n != 2, Return [ n ] ]; > Module [ { } , > { } (* Return Empty List if n==2 *) > ] > ] ; > f [ { } ] > f [ { { } } ] > f [ { { }, { } } ] > f [ { { }, { }, { } } ] > f [ { { }, { }, { }, { } } ] > > When I run the above, I get these four outputs: > 0 > 1 > Sequence[{}, {}][] > 3 > 4 > I can't really explain myself why it does that, I do not use named patterns much. But just an observation is that it seems due to the use of named pattern. Compare this, which just says that the head of the input should be a list, whichgives the expected result: ------------------------------------ Clear[f]; f[run_List] := Module[{n, result}, n = Length[run]; If[n != 2, n, Module[{}, {}]] ]; f[{{}}] f[{{}, {}}] f[{{}, {}, {}}] ---------------------------- 1 {} 3 To this, which uses named pattern ------------------------------------ Clear[f]; f[run : List___] := Module[{n, result}, n = Length[run]; If[n != 2, n, Module[{}, {}]] ]; f[{{}}] f[{{}, {}}] f[{{}, {}, {}}] ---------------------------- 1 {{}, {}}[] 3 --Nasser