# Tagged Questions

Questions about Mathematica's functional programming style, including the use of pure functions (Function[], #, &) and functions such as Map, Apply, Nest, and Through.

2answers
80 views

### How to define a function to save a list to a same-name file

I want a convenient way to save lots of intermediate data lists into files, so that next time the files can be imported automatically to homonym list variables in the notebook, without having to ...
1answer
70 views

...
1answer
103 views

### How to make a custom function as inbuilt

I have a custom function like following: ...
2answers
58 views

### How to transform the $y$ values in a list of $(x,y)$ pairs [duplicate]

How can I transform data = {{1, 0}, {0, 1}, {1, 1}} to {{1, f[0]}, {0, f[1]}, {1, f[1]}} This is similar to ...
5answers
206 views

### Transforming a list of the form {{{x, y}, z}, … } into the form {{x, y, z}, … } [duplicate]

This should be a fairly simple question, but I can't figure out how to do it. I have a list of points of the following form: ...
1answer
196 views

### Alternatives to For/Do Loop to optimize indexing Matrix

I am trying to optimize my code that searches for elements in the rows of matrix that are greater than 0. Then use that position to grab the element from other matrix. I use the do loop to iterate ...
0answers
21 views

### Express anonymous with formal parameters [duplicate]

I'd like to express the function F = Function[{#2[[1]] + #1[[2]]/#1[[1]], #2[[2]] + 1/#1[[1]]}] using explicit formal parameters. I've tried ...
2answers
2k views

### Scan vs. Map vs. Apply

I cannot understand the difference between Apply, Scan, and Map. I have tried to play with ...
1answer
468 views

### Gâteaux (directional) derivatives and higher order differentials of a functional

I would like to calculate the GĂ˘teaux derivative of a functional (i.e. a function depending on functions). A simple example for the Dirichlet functional: $L(u(x))=\int_0^1 \frac{1}{2} (u'(x))^2 dx$ ...
1answer
39 views

### Define inverse Laplace operator as map from functions to functions

I am trying to implement the inverse Laplace operator through P[g_] := h /.NDSolve[{h''[x] == g[x], h[0] == 0, h[1] == 0}, h, {x, 0, 1}][[1]] If I now define ...
1answer
509 views

### Does Mathematica have a functional programming idiom to loop over a list till a condition is met? [duplicate]

The module below takes a list of integers and performs fadic addition on it (if a bit slowly). ...
0answers
35 views

### Which the functions of parameters can use Span [closed]

As I know,ImageValue,PixelValue,ReplaceImageValue,...
2answers
45 views

### How to round all the numbers in a function?

I have a complicated function that is a combination of floating-point numbers and variables. I want all the numbers rounded off, but I have no clue how to do this. For example I would want ...
1answer
195 views

### How to construct the RHS of Wick's theorem

I've been trying this for a few days now, but I just can't seem to find an efficient (speed-wise) functional way to code the following task. Context: I would like to construct an expressions that is ...
1answer
176 views

### Core functions or operations that every aspiring Mathematica programmer should know? [duplicate]

I am aware that there are common yet very powerful operations like Map, Thread, Apply, ...
0answers
313 views

### Is there research about the Mathematica programming paradigm?

There seem to be lots of papers discussing the functional programming paradigm terminology for Haskell, such as monad, category theory, lambda calculus, *morphism, etc. People study concepts and ...
0answers
50 views

### Different ways to program a recursive algorithm

Construct f (a numeric function) which takes multiple arguments with the following property: The single argument case $f(x_1)$ is meaningless, and the two-argument ...
3answers
179 views

### The usage of MapIndexed, can it be removed?

project euler 67 When I'm solving this problem, my solution is as following. You can download the data file there. ...
8answers
5k views

### General strategies to write big code in Mathematica?

I think after six months of exposure to Mathematica and the Wolfram Language I am fairly OK with writing short codes and snippets. However, what are some general strategies to use in order to write ...
2answers
89 views

### Partial Function Evaluation [closed]

How can I evaluate only part of a stack of function calls? In particular, given f[x_,y_,z_]=x+y+z;, say, I'd like to define a function ...
0answers
58 views

### Strange sharp spikes causing overflow while doing gradient descent

I am trying to find a function h(r) that minimises a functional H(h) by gradient descent. The result of H(h) is a single number. (Basically, I have a field configuration in space and I am trying to ...
1answer
69 views

### How to use currying to write a tail recursive function for summing factorials given a certain bound

I am very confused about how can i use currying to write a tail recursive function for summing factorials within a certain bound or summing the bounds i.e. given [1,5] i should be getting 1!+2!+3!+4!+...
1answer
65 views

### Pairing values with their indexes for MapThread

Several operations require you to map over a list of values and to know the index of each value as you process it. To accomplish this, my current somewhat ugly solution uses MapThread: ...
2answers
749 views

### What's the most “functional” way to do Cholesky decomposition?

I can do Cholesky in a procedural style, such as: ...
6answers
826 views

### What's the cleanest functional-programming style way to perform this list operation?

I am looking for the correct method for accomplishing the unknown piece in the following code snippet: rolls = Tuples[Range[1, 4], {2}] ...
3answers
111 views

### Counting runs in bits, along with their starts and lengths, in functional style

Here is some code to visualize Project Euler Problem 502. The code scans for runs of bits in the binary representation of an integer, and outputs the beginning position of each run and the length of ...
4answers
447 views

### How to keep some of the results of the NestList

I'm using some iterative arithmetics to calculate wave propagation with the help of NestList. I have to use a small step size for iteration to guarantee the accuracy, which lead to too much data (e.g, ...
0answers
71 views

### RealDigits and functional style

So while thinking about Replacing numbers of which you only know certain digits I have faced, again, a problem that I find hard to extract from RealDigits' output ...
0answers
142 views

### Variable naming conventions for nested pure functions [closed]

My understanding is that for nesting pure functions, one should use the explicit version of pure functions. I would like to know the best way of making it readable for people. I currently have ...
1answer
50 views

### Recognition Power Head for numeric value

We know: Map[Head, {a, a^2, a^3}] (* {Symbol, Power, Power} *) and Map[Head, {2, 2^2, 2^3}] all of them will be known as <...
3answers
117 views

### how to map a function on a variable list

The following is just an example that captures what I want to do (more on it at the bottom). I'm trying to do something that can be paraphrased as follows: I have a global list, say ...
0answers
180 views

### How best to simulate n-body systems in a functional way

I want to simulate a N-body system in the most functional way possible. The system I'm modeling are metallic colloidal particles in a solution of peroxide water, as here http://arxiv.org/pdf/1502....
1answer
125 views

### The basic primitive recursive functions

How can the basic primitive recursive functions be expressed in the Wolfram Language? Apparently here's an example for primitive recursion: ...
1answer
145 views

### How can I do a two dimensional Fold? [closed]

I'd like to Fold over higher dimensional objects than lists. Semantically, the format would be something like ...
7answers
1k views

### Better code to find Narcissistic number

My code finding Narcissistic numbers is not that slow, but it's not in functional style and lacks flexibility: if $n \neq 7$, I have to rewrite my code. Could you give some good advice? ...
4answers
2k views

### Using pure functions in Table

I need a table with the elements made of pure functions and list elements. This is a simplified example: I need a list as: ...
3answers
283 views

### How to get “Signature” of each element of “Permutation” output

I need to sum over all possible permutations, multiplied by the Signature of each permutation of a given list of symbols. But, I don't know how to determine the <...
1answer
269 views

### MapAt and generic Functions on Associations and Datasets

These are distinct but related questions about mapping functions to Associations and Datasets. Given: assoc = <| a -> 1, b -> 2, c -> 3|> And <...
4answers
927 views

### How can I avoid using a For-loop in my code?

I am trying to avoid using For because I heard that For is not cool in functional programming. Below is a problem that the first ...
0answers
86 views

### Delimited Continuations: Easy? or Fundamentally Difficult?

Apologies in advance for the length of this question, but this is as short as I have been able to make it. The Prescription I'm looking at the following tutorial on delimited (or composable) ...
3answers
95 views

### How to organize expression by symbols (like Collect), but apply different functions to each coefficient

I want to mimic the functionality of Collect[expr, {vars}, func], but with the following modification: The function f that is ...
8answers
3k views

### Functional style using lazy lists?

Let's say I want to answer the question "what are the first 400 palindromic prime numbers?" The first approach that comes to my mind from the set of languages that I know is to use some sort of lazy ...
4answers
294 views

### Handy / flexible directory tree operations

Is there any shorter / more handy / more flexible way to get directory tree as a nested Rules/Associations list? I tried to incorporate ...
1answer
75 views

### Defining a list of functions

I'm using polynomial mappings such as s[{x_,y_}] := {x + y^2, y -x} and playing with compositions of such mapping, e.g. ...
1answer
106 views

I have data ...
2answers
104 views

### Repeatedly apply a bivariate function with one argument running over a list

I'd like to know how, given symbols f and g, and a list {a,b,c,...}, to produce the ...
2answers
46 views

### Deciding whether the return value of a function is defined based on its input [duplicate]

I have been met with the following problem: I have defined the following: myFunc[e_Integer]; That means, if I call: ...
1answer
99 views

### Iterations in Mathematica [closed]

this is probably a very easy question for all of you. I have a stochastic Mathematica code (not important what it's exactly) which gives me a number at the end. Now I want Mathematica to run this ...
1answer
204 views

### How to terminate early a functional computation

Problem description: Given a list $A=\{1,2,0,7,0,-1,2,6,\ldots\}$ and a list $B=\{0,7,0\}$ write a program that determines whether $B$ appears in $A$ or not. Note: the order of $B$'s items does matter....
2answers
122 views

### A better way than using nested For-loops to make a search list

I'm generating a List that can be up to 26 values long, a simplified example is below. Is there a better way than nested loops? ...