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
22
votes
3 answers

How to perform side-effects in pure functional programming?

I am dealing with the concept of functional programming for a while now and find it quite interesting, fascinating and exciting. Especially the idea of pure functions is awesome, in various terms. But there is one thing I do not get: How to deal…
Golo Roden
  • 140,679
  • 96
  • 298
  • 425
21
votes
1 answer

Timing out pure functions

How can I "kill" a pure calculation which is taking too long? I tried import System.Timeout fact 0 = 1 fact n = n * (fact $ n - 1) main = do maybeNum <- timeout (10 ^ 7) $ (return . fact) 99999999 print maybeNum However, this doesn't…
amindfv
  • 8,438
  • 5
  • 36
  • 58
19
votes
5 answers

A way to avoid a common use of unsafePerformIO

I often find this pattern in Haskell code: options :: MVar OptionRecord options = unsafePerformIO $ newEmptyMVar ... doSomething :: Foo -> Bar doSomething = unsafePerformIO $ do opt <- readMVar options doSomething' where ... Basically, one…
fuz
  • 88,405
  • 25
  • 200
  • 352
19
votes
4 answers

How to implement "return early" logic in F#

I am familiar with the fact that in F# there is no equivalent "return" keyword. However we came across an issue recently where we have needed a workflow that consists of many steps, where each step can return a good or bad result. If a bad result…
bstack
  • 2,466
  • 3
  • 25
  • 38
18
votes
4 answers

Scala String Equality Question from Programming Interview

Since I liked programming in Scala, for my Google interview, I asked them to give me a Scala / functional programming style question. The Scala functional style question that I got was as follows: You have two strings consisting of alphabetic…
16
votes
3 answers

Scala fold right and fold left

I am trying to learn functional programming and Scala, so I'm reading the "Functional Programming in Scala" by Chiusano and Bjarnason. I' m having trouble understanding what fold left and fold right methods do in case of a list. I've looked around…
jrsall92
  • 572
  • 1
  • 5
  • 19
16
votes
2 answers

Side effects in Scala

I am learning Scala right in these days. I have a slight familiarity with Haskell, although I cannot claim to know it well. Parenthetical remark for those who are not familiar with Haskell One trait that I like in Haskell is that not only functions…
Andrea
  • 20,253
  • 23
  • 114
  • 183
15
votes
2 answers

How to check if a function is pure in Python?

A pure function is a function similar to a Mathematical function, where there is no interaction with the "Real world" nor side-effects. From a more practical point of view, it means that a pure function can not: Print or otherwise show a message Be…
13
votes
5 answers

How can I avoid nested ternary expressions in my code?

I have code like this. How can I write it in cleaner, more elegant way using functional programming in JavaScript? I want to get rid of nested ternary expressions. Any ideas? props => ({ iconColor: props.isPriority ? (props.isCompleted ?…
12
votes
2 answers

F# PurelyFunctionalDataStructures WeightBiasedLeftistHeap ex 3.4

I'm working on Okasaki's Purely Functional Data Structures and trying to build F# implementations of things. I'm also going through the exercises listed in the book (some are pretty challenging). Well I'm stuck on exercise 3.4 which calls for…
12
votes
5 answers

In pure functional languages, is data (strings, ints, floats.. ) also just functions?

I was thinking about pure Object Oriented Languages like Ruby, where everything, including numbers, int, floats, and strings are themselves objects. Is this the same thing with pure functional languages? For example, in Haskell, are Numbers and…
11
votes
4 answers

How lazy evaluation forced Haskell to be pure

I remember seeing a presentation in which SPJ said that lazy evaluation forced them to keep Haskell pure (or something along that line). I often see many Haskellers saying the same. So, I would like to understand how lazy evaluation strategy forced…
Sibi
  • 47,472
  • 16
  • 95
  • 163
11
votes
2 answers

When is it okay to modify a variable in functional languages?

So I'm teaching myself functional programming using Racket Scheme, and I love it so far. As an exercise for myself, I've been trying to implement a few simple tasks in a purely functional way. I know immutability is an important part of functional…
10
votes
1 answer

Locally editing a purely functional tree

Let's define a tree T: A / \ B C / \ D E Let's say a new node is added to E, yielding T': A / \ B C / \ D E \ G In a mutable language this is an easy task - just update E's children and we're done. However,…
Philip Kamenarsky
  • 2,757
  • 2
  • 24
  • 30
10
votes
4 answers

How Monads are considered pure?

I am very much new to Haskell, and really impressed by the language's "architecture", but it still bothers me how monads can be pure. As you have any sequence of instructions, it makes it an impure function, especially functions with I/O wouldn't be…
lycuid
  • 2,555
  • 1
  • 18
  • 28
1
2
3
16 17