purely-functional

Correct functional implementation on Binomial Heap

牧云@^-^@ 提交于 2019-12-11 02:17:07
问题 I am reading Binomial Heap in Purely Functional Data Structures. The implementation of insTree function confused me quite much. Here are the set of codes datatype Tree = Node of int * Elem.T * Tree list fun link (t1 as Node (r, x1, c1), t2 as Node (_, x2, c2)) = if Elem.leq (x1, x2) then Node (r+1, x1, t2::c1) else Node (r+1, x2, t1::c2) fun rank (Node (r, x, c)) = r fun insTree (t, []) = [t] | insTree (t, ts as t' :: ts') = if rank t < rank t' then t::ts else insTree (link (t, t'), ts') My

Why is printf() an impure function?

一个人想着一个人 提交于 2019-12-10 14:28:51
问题 As far as I know, impure functions are those which do not always return the same value when called with the same parameters (I must be missing something, or may be wrong, correct me if I am). So why is printf() considered to an impure function? 回答1: No, an "impure" function is a function with side-effects . In other words, no matter how many times you call it, a pure function will affect nothing other than its output . For example, foo is impure , even though it return zero : int x; int foo()

Is it possible for pure functions in Haskell to mutate local copies of variables?

五迷三道 提交于 2019-12-10 13:38:59
问题 Is it possible for pure functions in Haskell to mutate local copies of variables, in the way that clojure can as mentioned in Functional Programming Is A Scam!, by David Nolen? If not what are the reasons for this, and if so are there any examples anyone could point me to? A similar question was asked in Functions that look pure to callers but internally use mutation and the general consensus seemed to be that it was OK for pure functions to perform mutation, as long as the mutations were

How Monads are considered Pure?

你。 提交于 2019-12-09 12:22:32
问题 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 pure from any point of view. Is it because Haskell assumes, like all pure functions, that IO function has a return value too, but in form of opcode or something? I am really confused. 回答1: One way to think of this is that a value of type IO a is a

Side effects in Scala

喜夏-厌秋 提交于 2019-12-09 02:29:40
问题 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,

The place of closures in functional programming

杀马特。学长 韩版系。学妹 提交于 2019-12-08 04:33:10
问题 I have watched the talk of Robert C Martin "Functional Programming; What? Why? When?" https://www.youtube.com/watch?v=7Zlp9rKHGD4 The main message of this talk is that a state is unacceptable in functional programming. Martin goes even further, claims that assigments are 'evil'. So... keeping in mind this talk my question is, where is a place for closure in functional programming? When there is no state or no variable in a functional code, what would be a main reason to create and use such

Timing out pure functions

橙三吉。 提交于 2019-12-03 22:25:56
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 work. Replace the (return . fact) 99999999 with a "real" IO function like getLine and this works as expected. The point is that return (fact 999999999) immediately returns and doesn't trigger the timeout. It returns a thunk that will be evaluated later. If you force evaluation of the return value, main = do maybeNum <- timeout (10 ^ 7) $ return $! fact 99999999

How Monads are considered Pure?

痞子三分冷 提交于 2019-12-03 14:34:52
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 pure from any point of view. Is it because Haskell assumes, like all pure functions, that IO function has a return value too, but in form of opcode or something? I am really confused. One way to think of this is that a value of type IO a is a "recipe", containing a list of instructions that if performed would have side effects. Constructing that

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

对着背影说爱祢 提交于 2019-12-03 13:29:25
问题 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 回答1: 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' +

Scala String Equality Question from Programming Interview

…衆ロ難τιáo~ 提交于 2019-12-03 10:58:27
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 characters as well as a special character representing the backspace symbol. Let's call this backspace character '/'. When you get to the keyboard, you type this sequence of characters, including the backspace/delete character. The solution you are to implement must check if the two sequences of characters produce the same output. For example, "abc", "aa