purely-functional

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

社会主义新天地 提交于 2019-12-03 05:23:40
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. This very much depends on the definitions that you choose. It is definitely fair to say that a function is pure or impure . A pure function always returns the same result and does not modify the environment. An impure function can return different results when it is executed repeatedly (which can be caused by doing something to the environment). Are all impurities side-effects? I would not say so -

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

回眸只為那壹抹淺笑 提交于 2019-12-03 03:38:13
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 State passing is often tedious, error-prone, and hinders refactoring. For example, try labeling a binary tree or rose tree in postorder: data RoseTree a = Node a [RoseTree a] deriving (Show) postLabel :: RoseTree a -> RoseTree Int postLabel = fst . go 0 where go i (Node _ ts) = (Node i' ts', i' + 1) where (ts', i') = gots i ts gots i [] = ([], i) gots i (t:ts) = (t':ts', i'') where (t', i') = go i t (ts

Learning Haskell with a view to learning Scala

折月煮酒 提交于 2019-12-03 03:29:10
问题 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 like an improvement on Java for personal projects and I'd also like to learn a functional language to improve my knowledge as a programmer. I am wondering if it would be a good idea to learn Haskell as an introduction to functional programming as it

Efficient heaps in purely functional languages

╄→尐↘猪︶ㄣ 提交于 2019-12-03 01:56:40
问题 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 found so far describes them from an imperative viewpoint and the algorithms presented are hard to translate to a functional setting. How to efficiently implement a heap in a purely functional language such as Haskell? Edit: By efficient I mean it

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

主宰稳场 提交于 2019-12-03 01:39:49
问题 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. If someone hands you (flip apply) a with type (a -> b) -> b , is that a suitable replacement for a ? And what would you call something with type (a -> b) -> b ? Seeing as it appears to be a stand-in for an a , I'd be tempted to call it a proxy, or

Why don't purely functional languages use reference counting?

谁说我不能喝 提交于 2019-12-02 20:01:49
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 possibility of cycles. Am is right? If so, why don't they? I understand that reference counting is slower than GC in many cases, but at least it reduces pause times. It would be nice to have the option to use reference counting in cases where pause times are bad. JaredPar Your question is based on a faulty assumption. It's perfectly possible to have

Learning Haskell with a view to learning Scala

允我心安 提交于 2019-12-02 17:00:57
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 like an improvement on Java for personal projects and I'd also like to learn a functional language to improve my knowledge as a programmer. I am wondering if it would be a good idea to learn Haskell as an introduction to functional programming as it is purely functional so I'd properly learn it rather than haphazard using bits of functional in Scala

Efficient heaps in purely functional languages

心不动则不痛 提交于 2019-12-02 15:32:09
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 found so far describes them from an imperative viewpoint and the algorithms presented are hard to translate to a functional setting. How to efficiently implement a heap in a purely functional language such as Haskell? Edit: By efficient I mean it should still be in O(n*log n), but it doesn't have to beat a C program. Also, I'd like to use purely

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

橙三吉。 提交于 2019-12-02 15:08:45
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. If someone hands you (flip apply) a with type (a -> b) -> b , is that a suitable replacement for a ? And what would you call something with type (a -> b) -> b ? Seeing as it appears to be a stand-in for an a , I'd be tempted to call it a proxy, or something from http://www.thesaurus.com/browse/proxy . luqui's answer is excellent but I'm going to

Side effects in Scala

狂风中的少年 提交于 2019-12-01 02:37:06
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 are first-class citizens, but side effects (let me call them actions) are. An action that, when executed, will endow you with a value of type a , belongs to a specific type IO a . You can pass these actions around pretty much like any other value, and combine them in interesting ways. In fact, combining the side effects is the only way in Haskell to