Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #15088 > unrolled thread
| Started by | James Stein <mathgroup@stein.org> |
|---|---|
| First post | 2013-07-02 04:34 +0000 |
| Last post | 2013-07-03 08:51 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Rather simple function returns curious result. Explanation requested. James Stein <mathgroup@stein.org> - 2013-07-02 04:34 +0000
Re: Rather simple function returns curious result. Explanation requested. "Nasser M. Abbasi" <nma@12000.org> - 2013-07-03 08:51 +0000
| From | James Stein <mathgroup@stein.org> |
|---|---|
| Date | 2013-07-02 04:34 +0000 |
| Subject | Rather simple function returns curious result. Explanation requested. |
| Message-ID | <kqtl8l$ath$1@smc.vnet.net> |
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
[toc] | [next] | [standalone]
| From | "Nasser M. Abbasi" <nma@12000.org> |
|---|---|
| Date | 2013-07-03 08:51 +0000 |
| Message-ID | <kr0omc$3eh$1@smc.vnet.net> |
| In reply to | #15088 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web