3

EDIT: This was marked as a possible duplicate. While the referenced other question does talk about slots, my question is more about how to understand how multiple slots (#1 and #2) work. Also, my question is more specific and might be helpful for other questioners to get to the specific information about using two different slots. Also I've found the answers here to be more helpful to me, in this specific situation, than the other reference. End EDIT

I'm using FoldList as follows:

oep = FoldList[(1 - #1)*(1 - Exp[-#2]) + #1 &, rates];

This works, but I really don't understand what I'm doing. I mean I do understand the calculation, but I don't understand how to generically use #2. In this case it's running through my list of rates (numbers like .000123). Suppose it's on the 10th element of rates. It's using that particular rate as #2, while #1 is the result of the previous calculation.

Is that generally what #1 and #2 are used for - in functions like Fold? Or is there a simpler way to see how #1 and #2 are used?

Mitchell Kaplan
  • 3,696
  • 22
  • 34

2 Answers2

6

If you know any other modern programming language you find similar constructs that are usually called Anonymous or Lambda functions. They have their origin in the Lambda calculus and they are called anonymous because you can use them directly, without giving them a name. A simple example in Mathematica that adds two numbers is

Function[{x, y}, x + y]

You can use this function directly, without binding it (although you could do this) to a function-name

Function[{x, y}, x + y][1, 2]
(* 3 *)

You see that the complete Function construct acts as you would have a defined an add function but here, we can include the complete function body inside it. This is nice if you need a small function inside Fold, Map, or other functional constructs.

One downside is that writing out Function and giving the parameter names x and y is still very verbose. This is the reason why in Mathematica, you can shorten this further. Instead of referring to parameter names, you can refer to the parameter position: x is the first parameter and y is the second parameter. Now, you can write your function body by using these positions or slots:

Function[#1 + #2]

Since this is still long, you can replace the Function[...] with its operator form (...)&. The parentheses are only required if you need to identify exactly which portion of the preceding expression should go into the function body (or if you are unsure about precedence). This leads us to a very short notation

#1+#2&

To make things even more convenient, there are different Slot (#) notations. For instance ## catches all parameters. See the difference here:

#1 + #2 &[1, 2, 3]
(* 3 *)

Plus[##] &[1, 2, 3]
(* 6 *)

With this explanation, it is time to head over to the thread Kuba mentioned and read about all the details in the Wolfram documentation.

halirutan
  • 112,764
  • 7
  • 263
  • 474
1

You can get insight into this by using a small list,e.g.:

FoldList[(1 - #1)*(1 - Exp[-#2]) + #1 &, {r1, r2, r3}] // Column

enter image description here

ubpdqn
  • 60,617
  • 3
  • 59
  • 148