haskell

any working operator overloading example in haskell

戏子无情 提交于 2020-01-17 05:28:21
问题 I want to overload any operator . i want to do such a simple function that for instance think about overloading of == operator .Overload == such that x==y returns x . Or x==y return x+y. It doesn't matter what . Can you show me any simple operator overloading example? I cannot find any example on the web unfortunately. For example;when i call Tree a == Tree a return 5 (it always return 5. I select it ,it is not related to any thing) or when i call 3==4 return : 7 I tried the below codes(i

join function and type issues haskell

对着背影说爱祢 提交于 2020-01-17 04:51:30
问题 I have 2 lists Expressions and bindings (id = Expr), and trying to replace each expression with its equivalent from the bindings list in a new list called newE, where Expression = id .. Initially, I have only one expression: eq (div (add 2 7) (sub 5 2)) 3 I want to replace each identifier in this expression with its equivalent from the bindings list, so I tried to split this expression into a list of strings and removed brackets, to separate each identifier .. This is how I tried to implement

join function and type issues haskell

瘦欲@ 提交于 2020-01-17 04:51:24
问题 I have 2 lists Expressions and bindings (id = Expr), and trying to replace each expression with its equivalent from the bindings list in a new list called newE, where Expression = id .. Initially, I have only one expression: eq (div (add 2 7) (sub 5 2)) 3 I want to replace each identifier in this expression with its equivalent from the bindings list, so I tried to split this expression into a list of strings and removed brackets, to separate each identifier .. This is how I tried to implement

Haskell - parse error on input `=' [duplicate]

心不动则不痛 提交于 2020-01-17 04:32:28
问题 This question already has an answer here : Parse error in valid code [duplicate] (1 answer) Closed 5 years ago . when implementing the code for the "Towers of Hanoi" problem I get the following error message: hanoi.hs:4:24: parse error on input `=' Failed, modules loaded: none. Here is the code: hanoi 1 i j = [(i, j)] hanoi n i j = hanoi n' i otherT ++ [(i,j)] ++ hanoi n' otherT j where n' = n-1 otherT = 1+2+3-i-j Any Ideas? 回答1: Your editor and the compiler see the tabs differently. Avoid

Why do I get “non-exhaustive patterns in function”?

回眸只為那壹抹淺笑 提交于 2020-01-17 02:21:09
问题 In the following code Haskell complains about Non-exhaustive patterns in function prime' prime :: Int -> [Int] prime x = prime' [2..x] where prime' (p:ps)= p : prime' [x | x <- ps, mod x p > 0 && prime'' x [2..div x 2]] prime'' _ [] = True prime'' n (x:xs) | mod n x == 0 = False | otherwise = prime'' n xs prime' []=[] I can't find my mistake. Could someone explain why this happens, and what it means? 回答1: Indentation. The last line defines another function called prime' . Therefore, prime

Add nested property to returned record

不打扰是莪最后的温柔 提交于 2020-01-16 23:15:51
问题 I have a Yesod route handler that returns a JSON with the object { id: 1, title: "foo" content: "bar" } I would like to add a _links property with some metadata, that doesn't exist in on the Entity itself, e.g. { id: 1, title: "foo" content: "bar" _links: {self: http://localhost:3000/events/1} } How can I add the _links to the existing Entity record? Here's my handler: getEventR :: EventId -> Handler Value getEventR eid = do event <- runDB $ get404 eid render <- getUrlRender let renderedUrl =

Add nested property to returned record

霸气de小男生 提交于 2020-01-16 23:12:31
问题 I have a Yesod route handler that returns a JSON with the object { id: 1, title: "foo" content: "bar" } I would like to add a _links property with some metadata, that doesn't exist in on the Entity itself, e.g. { id: 1, title: "foo" content: "bar" _links: {self: http://localhost:3000/events/1} } How can I add the _links to the existing Entity record? Here's my handler: getEventR :: EventId -> Handler Value getEventR eid = do event <- runDB $ get404 eid render <- getUrlRender let renderedUrl =

IDs from State Monad in Haskell [duplicate]

倾然丶 夕夏残阳落幕 提交于 2020-01-16 19:00:48
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Creating unique labels in Haskell I've got a datatype Person and some input data from which I will create the Persons. I'd like to have each Person have its own ID (let's say integers [0..]). I could do this with recursion, but since I'm doing this in Haskell, I'd like to understand the monads. The State Monad is probably the best for this job, I suppose? The thing is, I don't really understand lots of things:

How to plug this type hole?

限于喜欢 提交于 2020-01-16 18:14:08
问题 If I have a returned type of an expression/value: :: Control.Monad.IO.Class.MonadIO m => m (Either PDFInfoError PDFInfo) How do I get the PDFInfo out of it? Perhaps more importantly, what process does one use to figure such things out. I'd like to leverage typed holes or some other process to be able to reason through these types (no pun intended) of questions on my own. Still reading through my first Haskell book, but wanting to understand how a more experienced Haskeller would solve this

Can I use special characters like tabulations and newlines in Show?

喜你入骨 提交于 2020-01-16 17:35:50
问题 data Pair = P Int Int instance Show Pair where show (P n1 n2) = (show n1) ++ "\t" ++ (show n2) Result: GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help \Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Main ( labn.hs, interpreted ) Ok, modules loaded: Main. *Main> show (P 5 6) "5\t6" OK for a pair of ints this might be an artificial problem, but my actual use case is