Questions about Mathematica's functional programming style, including the use of pure functions (Function[], #, &) and functions such as Map, Apply, Nest, and Through.
52
votes
7answers
2k 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 ...
25
votes
12answers
971 views
Map a function across a list conditionally
It seems that this is a really basic question, and I feel that the answer should be obvious to me. However, I am not seeing. Can you please help me? Thanks.
Suppose I have a list of data ...
24
votes
3answers
343 views
FoldWhile and FoldWhileList
Mathematica has had NestWhile and NestWhileList for some time. But, to date, it has not implemented a built-in ...
21
votes
6answers
943 views
Can this be written well, without loops?
Inspired by this question I would like to know if the following code can be written without explicit loops (For, While, etc.) in a clean, efficient and non-contrived way. I have been unable to do so.
...
19
votes
1answer
371 views
Prefix operator with low precedence
The question is simple, but I will elaborate on the background as well for those interested in the idea:
How to define a new operator with specified precedence value?
Background
Mathematica was ...
18
votes
3answers
812 views
Programming a numerical method in the functional style
I am new to Mathematica and I would like to learn a bit more about functional programming.
At the moment I have assignments like programming different numerical methods (for integration: ...
16
votes
2answers
765 views
Does every Symbol in Mathematica induce a monad?
From my question about types in Mathematica, I assume that every Symbol in Mathematica is a type if it appears as a Head of some ...
15
votes
3answers
525 views
Can someone explain this snippet: (#[#] &)[#[#][#] &]
A friend showed me this snippet of code today:
(#[#] &)[#[#][#] &]
I can sort of see that we're constructing an anonymous function that takes a function ...
15
votes
1answer
255 views
Are there advantages to using additional arguments of common functions rather than alternative ways of calculating with lists?
(Apologies for the long question title.)
One of the interesting, if sometimes confusing, things about Mathematica is that there is always more than one way to do things. Even intermediate users can ...
14
votes
2answers
543 views
Using a list of tuples in a pure function
I want to use a list of tuples within a function to make assignments.
Say I want to make assignments of the form
value[i,j] = val
What I have is a list of ...
14
votes
3answers
479 views
How can I improve my code for drawing a tree?
This code draws a tree, but it's not in the functional style.
...
14
votes
3answers
450 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:
...
14
votes
2answers
390 views
Going full functional (Haskell style)
I'm trying to define some notation so that Mathematica code would be more functional, similar to Haskell (just for fun): currying, lambdas, infix operator to function conversion, etc.. And I have some ...
13
votes
4answers
592 views
Is there a more elegant and efficient way to write brainf*** style loops in Mathematica?
I'm having some fun writing a brainf*** interpreter in Mathematica. See this wiki article for more info. It works nicely but I'd like to find an elegant, preferably functional, way to handle the ...
13
votes
3answers
232 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, ...