Questions tagged [purely-functional]

Purely Functional is a term in computer science used to describe algorithms, data structures, or programming languages that do not allow modification of data at run-time.

Purely Functional is a term in computer science used to describe algorithms, data structures, or programming languages that do not allow modification of data at run-time.

A pure function is a function whose returned value depends on nothing other than the arguments, and that causes no side effects; it contains no mutable hidden states. One consequence is that given the same arguments it will return the same value no matter when it is called, or how many times it is called, in the course of the program. In other words, such a function call (a "pure expression") has a value that does not depend on history or context.

Related concepts:

243 questions
10
votes
3 answers

Why must we use state monad instead of passing state directly?

Can someone show a simple example where state monad can be better than passing state directly? bar1 (Foo x) = Foo (x + 1) vs bar2 :: State Foo Foo bar2 = do modify (\(Foo x) -> Foo (x + 1)) get
ais
  • 2,514
  • 2
  • 17
  • 24
9
votes
3 answers

Would the ability to declare Lisp functions 'pure' be beneficial?

I have been reading a lot about Haskell lately, and the benefits that it derives from being a purely functional language. (I'm not interested in discussing monads for Lisp) It makes sense to me to (at least logically) isolate functions with…
9
votes
3 answers

A Functional-Imperative Hybrid

Pure functional programming languages do not allow mutable data, but some computations are more naturally/intuitively expressed in an imperative way -- or an imperative version of an algorithm may be more efficient. I am aware that most functional…
user793587
9
votes
3 answers

What does it mean to "run" inside a monad?

Working through Haskell textbook chapters on different monads, I repeatedly get lost when the authors jump from explaining the details of bind and the monad laws to actually using monads. Suddenly, expressions like "running a function in a monadic…
Ulrich Schuster
  • 1,670
  • 15
  • 24
9
votes
2 answers

Can pure functions be asynchronous?

While going through the definition for pure functions, it's generally defined with two traits: 1) Should produce same output given the same input 2) Should not produce any side effects Does it also imply that a pure function shouldn't be…
9
votes
5 answers

Determine if a static method is purely functional

Given a java.lang.reflect.Method object, is there anyway to determine whether the method is purely functional (i.e., given the same input, it will always produce the same output and it is stateless. In other words, the function does not depend on…
One Two Three
  • 22,327
  • 24
  • 73
  • 114
8
votes
3 answers

How does the presence of the "error" function bear on the purity of Haskell?

I've always wondered how the Haskell exception system fits in with the whole "Pure functional language" thing. For example see the below GHCi session. GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help Prelude> head [] *** Exception:…
8
votes
1 answer

Haskell: use last reference to a variable to efficiently create a new variable

This C code can conceptually be described as creating a new array identical to an input array but with 1 as the first element: int* retire_and_update(int* arr) { arr[0] = 1; return arr; } This is a pure function (wink wink nudge nudge) so…
Praxeolitic
  • 22,455
  • 16
  • 75
  • 126
8
votes
6 answers

Why is printf() an impure function?

As far as I know, impure functions are those which do not always return the same value when called with the same parameters (I must be missing something, or may be wrong, correct me if I am). So why is printf() considered to an impure function?
cirronimbo
  • 909
  • 9
  • 19
8
votes
2 answers

Analysis and Design for Functional Programming

How do you deal with analysis and design phases when you plan to develop a system using a functional programming language like Haskell? My background is in imperative/object-oriented programming languages, and therefore, I am used to use case…
7
votes
0 answers

How do I ensure a pure function only calls pure functions in TypeScript / JavaScript?

I want to embrace functional programming, and ensure that my pure functions not change state. How do I ensure that a function that I intend to be pure only calls other functions which are themselves pure? I see two ways, which have…
Tom Hale
  • 40,825
  • 36
  • 187
  • 242
7
votes
1 answer

Identifying pure functions in python

I have a decorator @pure that registers a function as pure, for example: @pure def rectangle_area(a,b): return a*b @pure def triangle_area(a,b,c): return ((a+(b+c))(c-(a-b))(c+(a-b))(a+(b-c)))**0.5/4 Next, I want to identify a newly…
Uri Goren
  • 13,386
  • 6
  • 58
  • 110
7
votes
2 answers

F# UnitTesting function with side effect

I am C# dev that has just starting to learn F# and I have a few questions about unit testing. Let's say I want to the following code: let input () = Console.In.ReadLine() type MyType= {Name:string; Coordinate:Coordinate} let readMyType = …
7
votes
1 answer

Are all pure functions in functional programming continuous?

I know that the set of Haskell functions is only a subset of all mathematical functions, because it is a programming language, so all of its functions must be computable. But is it true that all Haskell functions (and pure functions in general) are…
7
votes
1 answer

Is there a way to cache a function result in elm?

I want to calculate nth Fibonacci number with O(1) complexity and O(n_max) preprocessing. To do it, I need to store previously calculated value like in this C++ code: #include using namespace std; vector cache; int fibonacci(int n) { …
user2136963
  • 2,526
  • 3
  • 22
  • 41
1 2
3
16 17