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
76
votes
7 answers

Purity vs Referential transparency

The terms do appear to be defined differently, but I've always thought of one implying the other; I can't think of any case when an expression is referentially transparent but not pure, or vice-versa. Wikipedia maintains separate articles for…
62
votes
5 answers

What is the benefit of purely functional data structure?

There are large number of texts on data structures, and libraries of data structures code. I understand that purely functional data structure is easier to reason about. However I have trouble to understand the real world advantage of using purely…
48
votes
6 answers

Why don't purely functional languages use reference counting?

In purely functional languages, data is immutable. With reference counting, creating a reference cycle requires changing already created data. It seems like purely functional languages could use reference counting without worrying about the…
47
votes
2 answers

Why is catching an exception non-pure, but throwing an exception is pure?

In Haskell, you can throw an exception from purely functional code, but you can only catch in IO code. Why? Can you catch in other contexts or only the IO monad? How do other purely functional languages handle it?
Craig P. Motlin
  • 26,452
  • 17
  • 99
  • 126
46
votes
6 answers

Learning Haskell with a view to learning Scala

I've read a few questions such as Scala vs Haskell discussing the merits of both languages or which to learn, but I already know that I'd like to learn Scala. I was a Java programmer at uni and now mainly use PHP. I want to learn Scala as it looks…
jaz9090
  • 951
  • 10
  • 22
41
votes
5 answers

Why are "pure" functions called "pure"?

A pure function is one that has no side effects -- it cannot do any kind of I/O and it cannot modify the state of anything -- and it is referentially transparent -- when called multiple times with the same inputs, it always gives the same…
Tyler
  • 21,762
  • 11
  • 61
  • 90
40
votes
9 answers

Efficient heaps in purely functional languages

As an exercise in Haskell, I'm trying to implement heapsort. The heap is usually implemented as an array in imperative languages, but this would be hugely inefficient in purely functional languages. So I've looked at binary heaps, but everything I…
39
votes
3 answers

Is having a `(a -> b) -> b` equivalent to having an `a`?

In a pure functional language, the only thing you can do with a value is apply a function to it. In other words, if you want to do anything interesting with a value of type a you need a function (for example) with type f :: a -> b and then apply it.…
Mark Bolusmjak
  • 23,606
  • 10
  • 74
  • 129
31
votes
3 answers

Can a `ST`-like monad be executed purely (without the `ST` library)?

This post is literate Haskell. Just put in a file like "pad.lhs" and ghci will be able to run it. > {-# LANGUAGE GADTs, Rank2Types #-} > import Control.Monad > import Control.Monad.ST > import Data.STRef Okay, so I was able to figure how to…
PyRulez
  • 10,513
  • 10
  • 42
  • 87
29
votes
4 answers

Clojure: working with a java.util.HashMap in an idiomatic Clojure fashion

I have a java.util.HashMap object m (a return value from a call to Java code) and I'd like to get a new map with an additional key-value pair. If m were a Clojure map, I could use: (assoc m "key" "value") But trying that on a HashMap…
foxdonut
  • 7,779
  • 7
  • 34
  • 33
28
votes
4 answers

Kotlin: Update Immutable List Element

Kotlin beginner here. How do I take a list and without mutating it, create a second (immutable) list with one updated element at a specific index? I'm thinking of two ways, both of which seem like they may incur performance hits, mutate the…
Eric
  • 1,511
  • 1
  • 15
  • 28
28
votes
2 answers

Logging from within a Functional Programming Paradigm

I prefer to stick as closely as possible to the functional paradigm, squeezing as close as I can get to the purely functional when my brain is up for the challenge. I use F# when possible. Usually, I'm stuck with either VB.NET or C# (or VBA, when…
Jeff Maner
  • 1,179
  • 9
  • 23
27
votes
5 answers

Are side effects everything that cannot be found in a pure function?

Is it safe to say that the following dichotomy holds: Each given function is either pure or has side effects If so, side effects (of a function) are anything that can't be found in a pure function.
Trident D'Gao
  • 18,973
  • 19
  • 95
  • 159
26
votes
4 answers

Purely functional concurrent skip list

Skip lists (Pugh, 1990) provide sorted dictionaries with logarithmic-time operations like search trees but skip lists are much more amenable to concurrent updates. Is it possible to create an efficient purely functional concurrent skip list? If not,…
J D
  • 48,105
  • 13
  • 171
  • 274
23
votes
2 answers

scala functional - methods/functions inside or outside case class?

as a beginner in Scala - functional way, I'm little bit confused about whether should I put functions/methods for my case class inside such class (and then use things like method chaining, IDE hinting) or whether it is more functional approach to…
xwinus
  • 886
  • 3
  • 12
  • 28
1
2 3
16 17