referential-transparency

Referential transparency in functional programming

风流意气都作罢 提交于 2020-01-14 17:50:13
问题 I am new to JS and was learning functional programming and came across the term "referential transparency". Also, I found this statement "Referential transparency says it's safe to replace a pure function with its value". Does it mean that the use of RT makes it easy for JIT compiler to replace function with its return value as long as function gets hot? Is that true? 回答1: Here's an example: This is a pure function: it will always return the same output for the same input const even = x => x

Optimization of Function Calls in Haskell

醉酒当歌 提交于 2019-12-22 02:52:30
问题 Not sure what exactly to google for this question, so I'll post it directly to SO: Variables in Haskell are immutable Pure functions should result in same values for same arguments From these two points it's possible to deduce that if you call somePureFunc somevar1 somevar2 in your code twice, it only makes sense to compute the value during the first call. The resulting value can be stored in some sort of a giant hash table (or something like that) and looked up during subsequent calls to the

Haskell - How can I use pure functions inside IO functions?

孤街醉人 提交于 2019-12-20 06:44:09
问题 How can I use pure functions inside IO functions? :-/ For example: I'm reading a file (IO function) and I want to parse its context, a string, by using a pure function with referential transparency. It seems such worlds, pure functions and IO functions, are separated. How can I possibly bridge them? 回答1: The simplest way is to use fmap , which has the following type: fmap :: (Functor f) => (a -> b) -> f a -> f b IO implements Functor , which means that we can specialize the above type by

Is Haskell truly pure (is any language that deals with input and output outside the system)?

女生的网名这么多〃 提交于 2019-12-17 07:12:57
问题 After touching on Monads in respect to functional programming, does the feature actually make a language pure, or is it just another "get out of jail free card" for reasoning of computer systems in the real world, outside of blackboard maths? EDIT: This is not flame bait as someone has said in this post, but a genuine question that I am hoping that someone can shoot me down with and say, proof, it is pure. Also I am looking at the question with respect to other not so pure Functional

Random-Pivot Quicksort in Haskell

我们两清 提交于 2019-12-13 13:32:56
问题 Is it possible to implement a quicksort in Haskell (with RANDOM-PIVOT) that still has a simple Ord a => [a]->[a] signature? I'm starting to understand Monads, and, for now, I'm kind of interpreting monads as somethink like a 'command pattern', which works great for IO. So, I understand that a function that returns a random number should actually return a monadic value like IO, because, otherwise, it would break referential transparency. I also understand that there should be no way to

How to catch (and ignore) a call to the error function?

一世执手 提交于 2019-12-12 08:19:09
问题 I'm surprised I couldn't find an answer to this anywhere. I'm writing a roguelike and I'm using the ncurses library from hackage, which is a pretty good wrapper around the ncurses library. Now ncurses has this quirk where if you try to write the bottom right character, it does so, then it tries to move the cursor to the next character, then it fails because there's nowhere to move it to. It returns an error value that you can only ignore. My problem is that the haskell ncurses library writer

Class set method in Haskell using State-Monad

穿精又带淫゛_ 提交于 2019-12-11 18:19:27
问题 I've recently had a look at Haskell's Monad - State. I've been able to create functions that operate with this Monad, but I'm trying to encapsulate the behavior into a class, basically I'm trying to replicate in Haskell something like this: class A { public: int x; void setX(int newX) { x = newX; } void getX() { return x; } } I would be very grateful if anyone can help with this. Thanks! 回答1: I would start off by noting that Haskell, to say the least, does not encourage traditional OO-style

Can a pure function return a Symbol?

北慕城南 提交于 2019-12-08 07:56:58
问题 This may border on philosophical, but I thought it would be the right place to ask. Suppose I have a function that creates a list of IDs. These identifiers are only used internally to the application, so it is acceptable to use ES2015 Symbol() here. My problem is that, technically , when you ask for a Symbol, I'd imagine the JS runtime creates a unique identifier (random number? memory address? unsure) which, to prevent collisions, would require accessing global state. The reason I'm unsure

Referential transparency with polymorphism in Haskell

我的未来我决定 提交于 2019-12-06 17:12:21
问题 Say I have a function: f :: Int -> (Rational, Integer) f b = ((toRational b)+1,(toInteger b)+1) I want to abstract away the (+1) like so: f :: Int -> (Rational, Integer) f b = (h (toRational b) ,h (toInteger b)) where h = (+1) This wont work obviously, but if I specify the type signature it will work: f :: Int -> (Rational, Integer) f b = (h (toRational b) ,h (toInteger b)) where h :: Num a => a -> a h = (+1) Say I now want to further abstract the function by passing h as a parameter: f ::

Can a pure function return a Symbol?

廉价感情. 提交于 2019-12-06 15:15:31
This may border on philosophical, but I thought it would be the right place to ask. Suppose I have a function that creates a list of IDs. These identifiers are only used internally to the application, so it is acceptable to use ES2015 Symbol() here. My problem is that, technically , when you ask for a Symbol, I'd imagine the JS runtime creates a unique identifier (random number? memory address? unsure) which, to prevent collisions, would require accessing global state. The reason I'm unsure is because of that word, "technically". I'm not sure (again, from a philosophical standpoint) if this