maybe

Haskell maps returning a monad

我是研究僧i 提交于 2019-12-22 03:40:21
问题 The lookup function in Data.Map and Data.IntMap currently return values wrapped in Maybe with the type signature lookup :: Ord k => k -> Map k a -> Maybe a It used to have the more general type of lookup :: (Monad m, Ord k) => k -> Map k a -> m a I realize the former likely reduces the need of extra type specification, but the latter would make it much more general and allow lookup to be used in list comprehensions. Is there any way to mimic this behavior with the newer version, or would I

What's the difference between undefined in Haskell and null in Java?

爷,独闯天下 提交于 2019-12-20 08:48:10
问题 Both are terms whose type is the intersection of all types (uninhabited). Both can be passed around in code without failing until one attempts to evaluate them. The only difference I can see is that in Java, there is a loophole which allows null to be evaluated for exactly one operation, which is reference equality comparison ( == )--whereas in Haskell undefined can't be evaluated at all without throwing an exception. Is this the only difference? Edit What I'm really trying to get at with

What's the difference between undefined in Haskell and null in Java?

混江龙づ霸主 提交于 2019-12-20 08:48:00
问题 Both are terms whose type is the intersection of all types (uninhabited). Both can be passed around in code without failing until one attempts to evaluate them. The only difference I can see is that in Java, there is a loophole which allows null to be evaluated for exactly one operation, which is reference equality comparison ( == )--whereas in Haskell undefined can't be evaluated at all without throwing an exception. Is this the only difference? Edit What I'm really trying to get at with

Is there a standard option workflow in F#?

喜夏-厌秋 提交于 2019-12-19 05:03:28
问题 Is there an option (maybe) wokflow (monad) in the standrd F# library? I've found a dozen of hand-made implementations (1, 2) of this workflow, but I don't really want to introduce non-standard and not very trusted code into my project. And all imaginable queries to google and msdn gave me no clue where to find it. 回答1: There's no Maybe monad in the standard F# library. You may want to look at FSharpx, a F# extension written by highly-qualified members of F# community, which has quite a number

Is there a standard option workflow in F#?

烂漫一生 提交于 2019-12-19 05:03:04
问题 Is there an option (maybe) wokflow (monad) in the standrd F# library? I've found a dozen of hand-made implementations (1, 2) of this workflow, but I don't really want to introduce non-standard and not very trusted code into my project. And all imaginable queries to google and msdn gave me no clue where to find it. 回答1: There's no Maybe monad in the standard F# library. You may want to look at FSharpx, a F# extension written by highly-qualified members of F# community, which has quite a number

Why is the use of Maybe/Option not so pervasive in Clojure?

两盒软妹~` 提交于 2019-12-18 10:32:33
问题 Why does Clojure, despite such an emphasis on functional paradigm, not use the Maybe / Option monad to represent optional values? The use of Option is quite pervasive in Scala, a functional programming language I use regularly. 回答1: Clojure is not statically typed, so doesn't need the strict this/that/whatever type declarations that are necessary in haskell (and, I gather, Scala). If you want to return a string, you return a string; if you return nil instead, that's okay too. "Functional"

Is the implementation of `<*>` based on `fmap` special to Maybe applicative or can it be generalized to other applicatives?

眉间皱痕 提交于 2019-12-13 09:46:50
问题 In Maybe applicative, <*> can be implemented based on fmap . Is it incidental, or can it be generalized to other applicative(s)? (<*>) :: Maybe (a -> b) -> Maybe a -> Maybe b Nothing <*> _ = Nothing (Just g) <*> mx = fmap g mx Thanks. See also In applicative, how can `<*>` be represented in terms of `fmap_i, i=0,1,2,...`? 回答1: It cannot be generalized. A Functor instance is unique: instance Functor [] where fmap = map but there can be multiple valid Applicative instances for the same type

Question about applicative and nested Maybe

孤者浪人 提交于 2019-12-10 18:57:43
问题 I wrote this function: appFunc :: Integer -> Integer -> Bool -> Maybe (Integer,Integer) appFunc i1 i2 b = if b then Just (i1,i2) else Nothing And then I use it as such in GHCi: > appFunc <$> Just 3 <*> Nothing <*> Just True Nothing Which is great because if at least one of the parameters is Nothing then the whole expression evaluates to Nothing . However, when all parameters are Just then I get a nested Maybe : > appFunc <$> Just 3 <*> Just 1 <*> Just False Just Nothing Ideally, I would like

How can I use &&& with a -> Maybe a

旧街凉风 提交于 2019-12-10 17:34:35
问题 I had two functions f1:: String -> Int f2:: String -> Int f3:: String -> (Int,Int) f3 = f1 &&& f2 then they was changed to String -> Maybe Int f1:: String -> Maybe Int f2:: String -> Maybe Int f3:: String -> (Maybe Int,Maybe Int) Is there pointfree way to get function f4:: String -> Maybe (Int, Int) So if both f1 and f2 return Just, f4 will also return Just otherwise Nothing 回答1: import Control.Arrow import Control.Applicative h :: (Applicative f) => (a -> f b) -> (a -> f c) -> a -> f (b, c)

Function like catMaybes, but counting Nothing values

情到浓时终转凉″ 提交于 2019-12-10 12:37:26
问题 I have a list like this: let foo = [Just 1, Just 2, Nothing, Just 3, Nothing, Nothing] By using catMaybes I can extract only the Just -constructed values: catMaybes foo -- [1,2,3] I'm now looking for a function that not only yields a list of Just s but also a count of Nothing s for a finite list by traversing it once. It should have a signature like this: catMaybesCount :: [Maybe a] -> ([a], Int) Note: This question was answered Q&A-style and therefore intentionally does not show any research