referential-transparency

Referential Transparency

若如初见. 提交于 2019-12-06 01:39:20
问题 What is the meaning of the term "Non-observable" when used in context with the term "referentially transparent" in functional programming? 回答1: As you might know, the term "referentially transparent" means that the value of expression can depend only on the values of its parts, and not on any other facts about them. For example, it cannot depend on the following: Whether some part of expression is already evaluated or not (in a lazy language) Whether two equal values are shared (are pointers

Optimization of Function Calls in Haskell

自古美人都是妖i 提交于 2019-12-04 22:49:54
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 function. I have two questions: Does GHC actually do this kind of optimization? If it does, what is

Referential transparency with polymorphism in Haskell

与世无争的帅哥 提交于 2019-12-04 22:48:13
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 :: Num a => Int -> (a -> a) -> (Rational, Integer) f b g = (h (toRational b) ,h (toInteger b)) where h ::

Type inference interferes with referential transparency

这一生的挚爱 提交于 2019-12-04 16:00:00
问题 What is the precise promise/guarantee the Haskell language provides with respect to referential transparency? At least the Haskell report does not mention this notion. Consider the expression (7^7^7`mod`5`mod`2) And I want to know whether or not this expression is 1. For my safety, I will do perform this twice: ( (7^7^7`mod`5`mod`2)==1, [False,True]!!(7^7^7`mod`5`mod`2) ) which now gives (True,False) with GHCi 7.4.1. Evidently, this expression is now referentially opaque. How can I tell

Referential Transparency

你。 提交于 2019-12-04 05:30:43
What is the meaning of the term "Non-observable" when used in context with the term "referentially transparent" in functional programming? As you might know, the term "referentially transparent" means that the value of expression can depend only on the values of its parts, and not on any other facts about them. For example, it cannot depend on the following: Whether some part of expression is already evaluated or not (in a lazy language) Whether two equal values are shared (are pointers to the same location in memory) or not Whether a data structure is cyclic (i.e. its pointers create a cycle)

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

眉间皱痕 提交于 2019-12-04 00:32:19
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 dutifully checks for any errors on all calls, and when there is one, he calls: error "drawText: etc etc

Are there any purely functional Schemes or Lisps?

北城余情 提交于 2019-12-03 18:34:33
问题 I've played around with a few functional programming languages and really enjoy the s-expr syntax used by Lisps (Scheme in particular). I also see the advantages of working in a purely functional language. Therefore: Are there any purely functional Schemes (or Lisps in general)? 回答1: Probably not, at least not as anything other than toys/proofs of concept. Note that even Haskell isn't 100% purely functional--it has secret escape hatches, and anything in IO is only "pure" in some torturous,

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

让人想犯罪 __ 提交于 2019-12-01 19:15:28
问题 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 there a bidirectional multimap persistent data structure?

谁说胖子不能爱 提交于 2019-12-01 04:09:51
In other words, can we model many to many relationships in a persistent data structure efficiently? A pair of unidirectional multimaps was suggested. However, I'm not sure how this would work well for removal in a persistent data structure. Let's take the case where we have keys 1..4 to values "1".."4" and let's say they each refer to all the others, so we have two maps that look very similar for both directions: {1 => ["2","3","4"], 2 => ["1","3","4"], ...} {"1" => [2,3,4], "2" => [1,3,4], ...} Now we want to remove item 1 completely from the system. That requires changing one node in the

Is there a bidirectional multimap persistent data structure?

不羁岁月 提交于 2019-12-01 02:07:46
问题 In other words, can we model many to many relationships in a persistent data structure efficiently? A pair of unidirectional multimaps was suggested. However, I'm not sure how this would work well for removal in a persistent data structure. Let's take the case where we have keys 1..4 to values "1".."4" and let's say they each refer to all the others, so we have two maps that look very similar for both directions: {1 => ["2","3","4"], 2 => ["1","3","4"], ...} {"1" => [2,3,4], "2" => [1,3,4], .