purely-functional

A way to avoid a common use of unsafePerformIO

僤鯓⒐⒋嵵緔 提交于 2019-11-30 17:36:06
I often find this pattern in Haskell code: options :: MVar OptionRecord options = unsafePerformIO $ newEmptyMVar ... doSomething :: Foo -> Bar doSomething = unsafePerformIO $ do opt <- readMVar options doSomething' where ... Basically, one has a record of options or something similar, that is initially set at the program's beginning. As the programmer is lazy, he doesn't want to carry the options record all over the program. He defines an MVar to keep it - defined by an ugly use of unsafePerformIO . The programmer ensures, that the state is set only once and before any operation has taken

Can a `ST`-like monad be executed purely (without the `ST` library)?

一世执手 提交于 2019-11-30 11:12:20
问题 This post is literate Haskell. Just put in a file like "pad.lhs" and ghci will be able to run it. > {-# LANGUAGE GADTs, Rank2Types #-} > import Control.Monad > import Control.Monad.ST > import Data.STRef Okay, so I was able to figure how to represent the ST monad in pure code. First we start with our reference type. Its specific value is not really important. The most important thing is that PT s a should not be isomorphic to any other type forall s . (In particular, it should be isomorphic

A way to avoid a common use of unsafePerformIO

蹲街弑〆低调 提交于 2019-11-30 01:32:04
问题 I often find this pattern in Haskell code: options :: MVar OptionRecord options = unsafePerformIO $ newEmptyMVar ... doSomething :: Foo -> Bar doSomething = unsafePerformIO $ do opt <- readMVar options doSomething' where ... Basically, one has a record of options or something similar, that is initially set at the program's beginning. As the programmer is lazy, he doesn't want to carry the options record all over the program. He defines an MVar to keep it - defined by an ugly use of

Can a `ST`-like monad be executed purely (without the `ST` library)?

放肆的年华 提交于 2019-11-29 22:55:52
This post is literate Haskell. Just put in a file like "pad.lhs" and ghci will be able to run it. > {-# LANGUAGE GADTs, Rank2Types #-} > import Control.Monad > import Control.Monad.ST > import Data.STRef Okay, so I was able to figure how to represent the ST monad in pure code. First we start with our reference type. Its specific value is not really important. The most important thing is that PT s a should not be isomorphic to any other type forall s . (In particular, it should be isomorphic to neither () nor Void .) > newtype PTRef s a = Ref {unref :: s a} -- This is defined liked this to make

Explicit Purely-Functional Data-Structure For Difference Lists

人走茶凉 提交于 2019-11-29 11:26:30
In Haskell, difference lists , in the sense of [a] representation of a list with an efficient concatenation operation seem to be implemented in terms of function composition . Functions and (dynamic) function compositions, though, must also be represented somehow in the computer's memory using data structures, which raises the question of how dlists could be implemented in Haskell without using function compositions, but, rather, through some basic purely-functional node-based data structures. How could this be done with the same performance guarantees as those of function composition? (++) 's

Clojure: working with a java.util.HashMap in an idiomatic Clojure fashion

淺唱寂寞╮ 提交于 2019-11-28 20:20:01
I have a java.util.HashMap object m (a return value from a call to Java code) and I'd like to get a new map with an additional key-value pair. If m were a Clojure map, I could use: (assoc m "key" "value") But trying that on a HashMap gives: java.lang.ClassCastException: java.util.HashMap cannot be cast to clojure.lang.Associative No luck with seq either: (assoc (seq m) "key" "value") java.lang.ClassCastException: clojure.lang.IteratorSeq cannot be cast to clojure.lang.Associative The only way I managed to do it was to use HashMap 's own put , but that returns void so I have to explicitly

What is the benefit of purely functional data structure?

瘦欲@ 提交于 2019-11-28 15:42:53
There are large number of texts on data structures, and libraries of data structures code. I understand that purely functional data structure is easier to reason about. However I have trouble to understand the real world advantage of using purely functional data structure in pragmatic code (using functional programming language or not) over the imperative counterpart. Can somebody provide some real world cases where purely functional data structure has advantage and why? Examples along the line like I use data_structure_name in programming_language to do application because it can do certain

Purity vs Referential transparency

那年仲夏 提交于 2019-11-28 04:37:55
The terms do appear to be defined differently, but I've always thought of one implying the other; I can't think of any case when an expression is referentially transparent but not pure, or vice-versa. Wikipedia maintains separate articles for these concepts and says: From Referential transparency : If all functions involved in the expression are pure functions, then the expression is referentially transparent. Also, some impure functions can be included in the expression if their values are discarded and their side effects are insignificant. From Pure expressions : Pure functions are required

Clojure: working with a java.util.HashMap in an idiomatic Clojure fashion

强颜欢笑 提交于 2019-11-27 12:51:19
问题 I have a java.util.HashMap object m (a return value from a call to Java code) and I'd like to get a new map with an additional key-value pair. If m were a Clojure map, I could use: (assoc m "key" "value") But trying that on a HashMap gives: java.lang.ClassCastException: java.util.HashMap cannot be cast to clojure.lang.Associative No luck with seq either: (assoc (seq m) "key" "value") java.lang.ClassCastException: clojure.lang.IteratorSeq cannot be cast to clojure.lang.Associative The only way

Why is catching an exception non-pure, but throwing an exception is pure?

我的梦境 提交于 2019-11-27 10:30:22
问题 In Haskell, you can throw an exception from purely functional code, but you can only catch in IO code. Why? Can you catch in other contexts or only the IO monad? How do other purely functional languages handle it? 回答1: Because throwing an exception inside a function doesn't make that function's result dependent on anything but the argument values and the definition of the function; the function remains pure. OTOH catching an exception inside a function does (or at least can) make that