Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #10994 > unrolled thread
| Started by | "Nasser M. Abbasi" <nma@12000.org> |
|---|---|
| First post | 2012-07-20 07:54 +0000 |
| Last post | 2012-07-21 03:46 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
can one make local symbol in a pure function? "Nasser M. Abbasi" <nma@12000.org> - 2012-07-20 07:54 +0000
Re: can one make local symbol in a pure function? Richard Fateman <fateman@cs.berkeley.edu> - 2012-07-21 03:46 +0000
| From | "Nasser M. Abbasi" <nma@12000.org> |
|---|---|
| Date | 2012-07-20 07:54 +0000 |
| Subject | can one make local symbol in a pure function? |
| Message-ID | <jub2s2$eid$1@smc.vnet.net> |
I was learning a bit about pure functions.
Suppose we have this toy example:
-----------------
Clear[f, x]
f[x_] := Module[{a = 2}, x^a]
lst = {1, 2, 3};
Map[f[#] &, lst]
-------------------
---> {1, 4, 9}
Now I'd like to define the pure function
in-line, using the Function[] syntax, but
at the same time be able to make a local symbol
'a' like I did when using Module. But the
syntax does not allow one to do that. There is
no place to define it:
------------------
lst = {1, 2, 3};
Map[Function[{x}, x^2], lst]
----------------
---> {1, 4, 9}
So if someone wants to make a temporary symbol to use
for temporary calculation inside the pure function, like
'a' in this example, and at the same time not have it be global
symbol, then one must use a Module? I can write
---------------------
lst = {1, 2, 3};
Map[Function[{x}, a=2; x^a], lst]
----------------------
But now 'a' is global, while when using Module it
was local.
Any way to make local symbols in a pure function like
with Module?
thanks,
--Nasser
[toc] | [next] | [standalone]
| From | Richard Fateman <fateman@cs.berkeley.edu> |
|---|---|
| Date | 2012-07-21 03:46 +0000 |
| Message-ID | <jud8ma$j2u$1@smc.vnet.net> |
| In reply to | #10994 |
On 7/20/2012 12:54 AM, Nasser M. Abbasi wrote:
> I was learning a bit about pure functions.
> Suppose we have this toy exa
>
> ---------------------
> lst = {1, 2, 3};
> Map[Function[{x}, a=2; x^a], lst]
> ----------------------
Map[ Function[{x},Module[ ...] ...
>
I suggest you read Structure & Interpretation of Computer Programs
by Abelson/Sussman
if you want to learn about pure functions.
Mathematica doesn't really have them. It just has global names that
have peculiar lexicographic appendages.
Try
Module[{a},a]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web