purely-functional

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

倾然丶 夕夏残阳落幕 提交于 2021-02-18 22:09:48
问题 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: Prelude.head: empty list Prelude> :t head head :: [a] -> a Prelude> :t error error :: [Char] -> a Prelude> error "ranch" *** Exception: ranch CallStack (from HasCallStack): error, called at <interactive>:4:1 in interactive:Ghci1 Prelude> The type of

PHP pass objects by value

梦想的初衷 提交于 2021-02-05 09:27:55
问题 I'd like to implement a pure function in PHP How do I pass an object by value and not by reference? In other words, this is the expected output: function change($obj) { $obj->set_value(2); } $obj = new Object(); $obj->set_value(1); change($obj); echo $obj->get_value(); // 1 回答1: read here: http://php.net/manual/en/language.oop5.cloning.php you really shouldn't pass by value, as that would require a deep copy aka. deep clone, or an insane amount allocated for for parameters.. if you really

Is this JavaScript function, taking a mutable reference argument, a pure function?

五迷三道 提交于 2021-01-27 07:11:13
问题 I have the same question as this one, but in the context of JavaScript. From Wikipedia: [a pure function's] return value is the same for the same arguments It's further claimed there that a pure function is not allowed to have a variation in return value with "mutable reference arguments". In JavaScript, every normal object is passed as a "mutable reference argument". Consider the following example: const f = (arr) => arr.length const x = [] console.log( f(x) ) // 0 x.push(1); console.log( f

Is this JavaScript function, taking a mutable reference argument, a pure function?

不羁岁月 提交于 2021-01-27 07:09:50
问题 I have the same question as this one, but in the context of JavaScript. From Wikipedia: [a pure function's] return value is the same for the same arguments It's further claimed there that a pure function is not allowed to have a variation in return value with "mutable reference arguments". In JavaScript, every normal object is passed as a "mutable reference argument". Consider the following example: const f = (arr) => arr.length const x = [] console.log( f(x) ) // 0 x.push(1); console.log( f

How lazy evaluation forced Haskell to be pure

你说的曾经没有我的故事 提交于 2020-12-30 05:51:43
问题 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 them to keep Haskell pure as opposed to a strict evaluation stragegy ? 回答1: I think the answer by Jubobs already sums it up nicely (with good references). But, in my own words, what I think SPJ and friends are referring to is this: Having to go

How to view higher-order functions and IO-actions from a mathematical perspective?

左心房为你撑大大i 提交于 2020-12-26 04:03:51
问题 I am trying to understand functional programming from first principles, yet I am stuck on the interface between the pure functional world and the impure real world that has state and side effects. From a mathematical perspective, what is a function that returns a function? what is a function that returns an IO action (like Haskell's IO type)? To elaborate: In my understanding, a pure function is a map from domain to co-domain. Ultimately, it is a map from some values in computer memory to

How to view higher-order functions and IO-actions from a mathematical perspective?

别等时光非礼了梦想. 提交于 2020-12-26 04:00:09
问题 I am trying to understand functional programming from first principles, yet I am stuck on the interface between the pure functional world and the impure real world that has state and side effects. From a mathematical perspective, what is a function that returns a function? what is a function that returns an IO action (like Haskell's IO type)? To elaborate: In my understanding, a pure function is a map from domain to co-domain. Ultimately, it is a map from some values in computer memory to

Scala fold right and fold left

≯℡__Kan透↙ 提交于 2020-06-10 03:29:48
问题 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 here but I haven't find something beginner friendly. So the code provided by the book is: def foldRight[A,B](as: List[A], z: B)(f: (A, B) => B): B = as match { case Nil => z case Cons(h, t) => f(h, foldRight(t, z)(f)) } def foldLeft[A,B](l: List[A],

Why don't purely functional languages use reference counting?

北战南征 提交于 2020-02-17 07:01:33
问题 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

Why don't purely functional languages use reference counting?

百般思念 提交于 2020-02-17 06:56:55
问题 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