haskell

Input checks in Haskell data constructors

北慕城南 提交于 2020-01-12 13:48:08
问题 How can I add input checks to Haskell data constructors? Let's say I have import Data.Time.Calendar data SchedulePeriod = SchedulePeriod { startDate :: Day , endDate :: Day , accrualStart :: Day , accrualEnd :: Day , resetDate :: Day , paymentDate :: Day , fraction :: Double } deriving (Show) and I want to impose a constraint startDate < endDate . Is there a way to do it without creating an abstract data type? 回答1: The standard way is to use a smart constructor that checks the precondition

Correct terminology for continuations

二次信任 提交于 2020-01-12 13:01:07
问题 I've been poking around continuations recently, and I got confused about the correct terminology. Here Gabriel Gonzalez says: A Haskell continuation has the following type: newtype Cont r a = Cont { runCont :: (a -> r) -> r } i.e. the whole (a -> r) -> r thing is the continuation (sans the wrapping) The wikipedia article seems to support this idea by saying a continuation is an abstract representation of the control state of a computer program. However, here the authors say that Continuations

Arrow equivalent of mapM?

删除回忆录丶 提交于 2020-01-12 12:53:06
问题 I'm trying to grok & work with Arrows, and am having some difficulty. I have a context where I need an Arrow [a] [b] , and I want to write an Arrow a b and map/sequence it inside the arrow, a la mapM . Specifically, the arrow is a Hakyll Compiler , but I don't think that matters much for the answer. Given an arrow myInnerArrow :: Arrow a => a b c How can I lift this into an arrow myOuterArrow :: Arrow a => a [b] [c] ? I have scoured the base library, particularly in Data.List and Control

Unlike a Functor, a Monad can change shape?

喜夏-厌秋 提交于 2020-01-12 11:47:07
问题 I've always enjoyed the following intuitive explanation of a monad's power relative to a functor: a monad can change shape; a functor cannot. For example: length $ fmap f [1,2,3] always equals 3 . With a monad, however, length $ [1,2,3] >>= g will often not equal 3 . For example, if g is defined as: g :: (Num a) => a -> [a] g x = if x==2 then [] else [x] then [1,2,3] >>= g is equal to [1,3] . The thing that troubles me slightly, is the type signature of g . It seems impossible to define a

Haskell Space Leak

对着背影说爱祢 提交于 2020-01-12 10:20:13
问题 all. While trying to solve some programming quiz: https://www.hackerrank.com/challenges/missing-numbers , I came across with space leak. Main function is difference , which implements multi-set difference. I've found out that List ':' and Triples (,,) kept on heaps with -hT option profiling. However, only big lists are difference 's two arguments, and it shrinks as difference keeps on tail recursion. But the memory consumed by lists keeps increasing as program runs. Triples is ephemeral array

Haskell Space Leak

ε祈祈猫儿з 提交于 2020-01-12 10:20:09
问题 all. While trying to solve some programming quiz: https://www.hackerrank.com/challenges/missing-numbers , I came across with space leak. Main function is difference , which implements multi-set difference. I've found out that List ':' and Triples (,,) kept on heaps with -hT option profiling. However, only big lists are difference 's two arguments, and it shrinks as difference keeps on tail recursion. But the memory consumed by lists keeps increasing as program runs. Triples is ephemeral array

Confused about diagrams of Yampa switches

↘锁芯ラ 提交于 2020-01-12 09:53:07
问题 There is some diagrams of Yampa switches at: http://www.haskell.org/haskellwiki/Yampa/switch http://www.haskell.org/haskellwiki/Yampa/rSwitch http://www.haskell.org/haskellwiki/Yampa/kSwitch (and so on). I've found that the switch , the only one diagram with description, is the easiest one to get understand. The others seems hard to follow the similar symbols to read the diagrams. For example, to try to read the rSwitch with the symbols used in the switch may be: Be a recursive SF which is

Haskell string to list [duplicate]

泄露秘密 提交于 2020-01-12 07:53:46
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: convert string list to int list in haskell I have a string 12345 . How can I show it in list form like [1,2,3,4,5] ? And also what if i have a string like ##%% ? I can't convert it to Int . How can view it in the form [#,#,%,%] ? 回答1: Use intersperse myShow :: String -> String myShow s = concat ["[", intersperse ',' s, "]"] 回答2: import Data.Char (digitToInt) map digitToInt "12345" 回答3: You should map the

Typed FP: Tuple Arguments and Curriable Arguments

我的未来我决定 提交于 2020-01-12 07:35:10
问题 In statically typed functional programming languages, like Standard ML, F#, OCaml and Haskell, a function will usually be written with the parameters separated from each other and from the function name simply by whitespace: let add a b = a + b The type here being " int -> (int -> int) ", i.e. a function that takes an int and returns a function which its turn takes and int and which finally returns an int. Thus currying becomes possible. It's also possible to define a similar function that

Why are higher rank types so fragile in Haskell

邮差的信 提交于 2020-01-12 07:28:47
问题 I was messing around with the runST function. Which has type (forall s. ST s a) -> a and it seems like trying to use it in any way that isn't directly applying without any indirection breaks it in pretty nasty ways. runST :: (forall s. ST s a) -> a const :: a -> b -> a so by substituting a in const for forall s. ST s a you should get the type of const runST const runST :: b -> (forall s. ST s a) -> a but instead GHC says that it can't match a with (forall s. ST s a) -> a but since a literally