haskell

Zoom instance over Free Monad

若如初见. 提交于 2020-01-05 04:24:10
问题 I'm attempting to build a free monad (using free) which acts just like a StateT monad, but which allows you to also run monads over a base state AppState . I have a separate constructor LiftAction which holds those types. The idea is that you keep zoom ing Actions down until they reach AppState, which can store different states inside its extension map. Here was my earlier (failed) attempt using mtl: Lift through nested state transformers (mtl) Anyways, since it's basically a wrapper over

Debug GHC compilation error returned at runtime by hint's `Language.Haskell.Interpreter.runInterpreter`

倾然丶 夕夏残阳落幕 提交于 2020-01-05 04:11:07
问题 I've re-posted this question to focus more tightly on the specific error, and to better enumerate what I've already tried. I'm trying to parse some Haskell code during the runtime of a Haskell program using the hint package. The outer program compiles, but when I run it the inner compilation step fails. I'm getting a description of what I assume is a syntax problem, and a location in the "interactive" code, but I have no idea how to view the code in question. Here's Main.hs module Main where

Trying to install Image.Codec.DevIL under windows. Needs pthread and IL. Can't get IL to work

橙三吉。 提交于 2020-01-05 02:51:07
问题 This is the output from cabal install codec-image-devil : Resolving dependencies... Configuring Codec-Image-DevIL-0.2.3... cabal: Missing dependency on a foreign library: * Missing C library: IL This problem can usually be solved by installing the system package that provides this library (you may need the "-dev" version). If the library is already installed but in a non-standard location then you can use the flags --extra-include-dirs= and --extra-lib-dirs= to specify where it is. cabal:

what happens when executing `(read “[Red]”) :: [Color]` under ghci?

那年仲夏 提交于 2020-01-05 02:33:52
问题 I am reading the subsection of Real World Haskell Chapter 6 (typeclasses) about aninstance of Read for Color , which implements readsPrec function. I don't know what happens when I type (read "[Red]") :: [Color] in GHCi, getting a [Red] result. For simplicity, I changed this implementation of the function a little, as shown below: instance Read Color where readsPrec _ value = [(Red, drop (length "Red") value)] Now, my confusion is: in GHCi, we can use the above as follows: *Main> let

Better interface for composing destructive operators

点点圈 提交于 2020-01-05 02:33:14
问题 I've got the following code in an old project of mine: -- |ImageOperation is a name for unary operators that mutate images inplace. newtype ImageOperation c d = ImgOp (Image c d-> IO ()) -- |Compose two image operations (#>) :: ImageOperation c d-> ImageOperation c d -> ImageOperation c d (#>) (ImgOp a) (ImgOp b) = ImgOp (\img -> (a img >> b img)) -- |An unit operation for compose nonOp = ImgOp (\i -> return ()) -- |Apply image operation to a Copy of an image img <# op = unsafeOperate op img

How to implement search in file system in haskell?

。_饼干妹妹 提交于 2020-01-04 23:45:47
问题 I'm not exactly new to haskell, but haven't used it much in real world. So what I want to do is to find all git repositories starting from some folders. Basically I'm trying to do this find . -type d -exec test -e '{}/.git' ';' -print -prune only faster via using haskell concurrency features. This is what I got so far. import Control.Concurrent.Async import System.Directory (doesDirectoryExist) import System.FilePath ((</>)) import System.IO (FilePath) isGitRepo :: FilePath -> IO Bool

How to use foldr correctly in Haskell?

六眼飞鱼酱① 提交于 2020-01-04 23:05:30
问题 I'm trying to write a function which behave like this: correctCards :: [Card] -> [Card] -> Int It takes two lists of type Card and check for how many cards are the same. Here is my code: correctCards answer guess = foldr step acc guess where acc = 0 step acc guess | elem (head guess) answer = acc + 1 | otherwise = acc But the types are not match. Can someone tell me where I went wrong? Thanks. 回答1: Have a look at foldr 's type: foldr :: (a -> b -> b) -> b -> [a] -> b Now, that means that the

Is there a sense of 'object equality' in Haskell?

a 夏天 提交于 2020-01-04 21:38:02
问题 If I have a singly linked list in Haskell: data LL a = Empty | Node a (LL a) deriving (Show, Eq) I can easily implement methods to insert at the end and at the beginning. But what about inserting after or before a particular element? If I have a LL of Integer , can I make a distinction in Haskell between inserting 4 after a particular node containing a 1 , rather than the first 1 that it sees when processing the list? Node 1 (Node 2 (Node 3 (Node 1 Empty))) I'm curious how an insertAfter

Criterion causing memory consumption to explode, no CAFs in sight

无人久伴 提交于 2020-01-04 15:32:02
问题 Basically I have a simple function call, which when used in conjunction with Criterion, results in the memory consumption exploding. Suppose I have the following program : {-# OPTIONS_GHC -fno-cse #-} {-# LANGUAGE BangPatterns #-} module Main where import Criterion.Main import Data.List num :: Int num = 10000000 lst :: a -> [Int] lst _ = [1,2..num] myadd :: Int -> Int -> Int myadd !x !y = let !result = x + y in result mysum = foldl' myadd 0 main :: IO () main = do print $ mysum (lst ()) Then

How does getArgs work?

我的未来我决定 提交于 2020-01-04 14:02:56
问题 I am trying to understand getArgs in Haskell. Here is what I have: import System.Environment myFunFunction = do args <- getArgs return $ head args What I am getting when I run the function is *Main> myFunFunction *** Exception: Prelude.head: empty list Does this not work the same way as getLine ? Why does it not ask for a command line argument? 回答1: The type of getArgs is IO [String] . When you bind it with <- , as in the OP, the bound symbol ( args ) gets the type [String] , i.e. a list of