haskell

How to build matrix of zeros using hmatrix?

夙愿已清 提交于 2019-12-29 09:04:44
问题 Trying to use hmatrix, to create a zero marix. For some reason, when I try this on command line, it works: buildMatrix 2 3 (\(r,c) -> fromIntegral 0) However, when I try to do the same thing in my code: type Dim = (Int, Int) buildFull :: Matrix Double -> Vector Int -> Vector Int -> Dim -> Int buildFull matrix basic nonbasic (m, n) = do -- Build mxn matrix of zeroes let f = buildMatrix m n (\(r,c) -> fromIntegral 0) m it fails: Pivot.hs:23:17: Ambiguous type variable `a0' in the constraints:

How to build matrix of zeros using hmatrix?

三世轮回 提交于 2019-12-29 09:04:07
问题 Trying to use hmatrix, to create a zero marix. For some reason, when I try this on command line, it works: buildMatrix 2 3 (\(r,c) -> fromIntegral 0) However, when I try to do the same thing in my code: type Dim = (Int, Int) buildFull :: Matrix Double -> Vector Int -> Vector Int -> Dim -> Int buildFull matrix basic nonbasic (m, n) = do -- Build mxn matrix of zeroes let f = buildMatrix m n (\(r,c) -> fromIntegral 0) m it fails: Pivot.hs:23:17: Ambiguous type variable `a0' in the constraints:

If Something Is Not A List In Haskell

試著忘記壹切 提交于 2019-12-29 08:40:48
问题 How do i check if an object in Haskell is not a list? for instance i want to know if let a = 55 , a is a list or just a number? 回答1: You don't check. You do. But really, what are you trying to do here? If you are trying to ensure your function can only be called with a list Haskell will make sure your function can only be called with a list. If you try to call your function with a non-list, this will cause a compile error. e.g. myFunction :: [a] -> String myFunction [] = "no elements!"

Haskell Time Limit on Evaluation

大兔子大兔子 提交于 2019-12-29 08:27:32
问题 Does anyone know of a function that will allow only a certain amount of time to execute a function. Something with a type signature like this. limited::Int->(a->b)->a->IO (Maybe b) I can't think of how to implement, and I couldn't find it. The reason why I ask is I am going to make a list of all possible Brainfuck programs, and I want to filter out the ones that take too long. 回答1: There's a dedicated function from System.Timeout: timeout :: Int -> IO a -> IO (Maybe a) To have it the way you

Simulating interacting stateful objects in Haskell

混江龙づ霸主 提交于 2019-12-29 08:27:18
问题 I'm currently writing a Haskell program that involves simulating an abstract machine, which has internal state, takes input and gives output. I know how to implement this using the state monad, which results in much cleaner and more manageable code. My problem is that I don't know how to pull the same trick when I have two (or more) stateful objects interacting with one another. Below I give a highly simplified version of the problem and sketch out what I have so far. For the sake of this

How to get ReaderT to work with another monad transformer?

匆匆过客 提交于 2019-12-29 08:06:53
问题 I would like to embed ReaderT into another monad transformer. How do I do this? The example below uses Scotty but I think it would be the same with any other monad. {-# LANGUAGE OverloadedStrings #-} import qualified Web.Scotty import Web.Scotty.Trans import Data.Text.Lazy import Control.Monad.IO.Class (liftIO) import Control.Monad.Trans.Reader import Control.Monad.Trans data Config = Config Text main :: IO () main = do let config = Config "Hello World" -- how to I make this line work?

combining StateT with InputT

前提是你 提交于 2019-12-29 08:03:52
问题 It is a follow-up to this question. I'm trying to combine shell from @ErikR's answer in my InputT loop. main :: IO [String] main = do c <- makeCounter execStateT (repl c) [] repl :: Counter -> StateT [String] IO () repl c = lift $ runInputT defaultSettings loop where loop = do minput <- getLineIO $ in_ps1 $ c case minput of Nothing -> lift $ outputStrLn "Goodbye." Just input -> (liftIO $ process c input) >> loop getLineIO :: (MonadException m) => IO String -> InputT m (Maybe String) getLineIO

Haskell: Trapped in IO monad

不打扰是莪最后的温柔 提交于 2019-12-29 07:40:20
问题 I am trying to parse a file using the parseFile function found in the the haskell-src-exts package. I am trying to work with the output of parseFile which is of course IO, but I can't figure out how to get around the IO. I found a function liftIO but I am not sure if that is the solution in this situation. Here is the code below. import Language.Haskell.Exts.Syntax import Language.Haskell.Exts import Data.Map hiding (foldr, map) import Control.Monad.Trans increment :: Ord a => a -> Map a Int

What's the reason behind cabal (dependency) hell?

吃可爱长大的小学妹 提交于 2019-12-29 07:36:29
问题 How does dependency hell happen in Cabal-install? I read the following at Cabal/Survival - HaskellWiki: 1. What is the difficulty caused by Cabal-install? The main difficulty with Cabal is otherwise known as 'dependency hell', in which the cabal-install does not manage to install a desired package for a reason or another, leading to large amount of manual work. As an example of this difficulty, consider a case where the user wishes to install packages A and B. Both of these work with package

parallel “Folding” in Haskell

余生颓废 提交于 2019-12-29 07:35:09
问题 I have a function with type below: union :: a -> a -> a And a has additivity property. So we can regard union as a version of (+) Say, we have [a] , and want to perform a parallel "folding" , for non-parallel foldling we can do only: foldl1' union [a] But how to perform it in parallel? I can demonstrate problem on Num values and (+) function. For example, we have a list [1,2,3,4,5,6] and (+) In parallel we should split [1,2,3] (+) [4,5,6] [1,2] (+) [3] (+) [4,5] (+) [6] ([1] (+) [2]) (+) ([3]