haskell

REPA: computeS and computeP?

左心房为你撑大大i 提交于 2020-02-25 04:56:47
问题 I am trying this REPA library, and i want to process an image in both ways, parallel and sequentially . I can read the image (with other library, DevIL) and process it with computeP (parallel). Here is the code (is from a example on the wiki of haskell). import Foreign.Ptr import System.Environment import Data.Word import Data.Array.Repa hiding ((++)) import Data.Array.Repa.IO.DevIL import Data.Array.Repa.Repr.ForeignPtr main :: IO () main = do [f] <- getArgs (RGB v) <- runIL $ readImage f

Partially evaluated type in classes

天大地大妈咪最大 提交于 2020-02-24 11:13:41
问题 This is a concrete version of my question asked here. I have an algorithm that produces some output and can produce some auxiliary information, which I may or may not care about in a given case (I call this auxiliary information "annotation" below). I am using Euclid's algorithm for an illustration: class AnnotatedGcd m where actually :: (Integral a) => a -> m a update :: (Integral a) => a -> m a -> m a newtype GcdOnly a = Gcd a deriving Show instance AnnotatedGcd GcdOnly where actually = Gcd

Reordering type parameters in Haskell

↘锁芯ラ 提交于 2020-02-24 11:12:32
问题 I have a question about type parameters that I think is best expressed by an example. This piece of code newtype Triple a b c = T (a,b,c) instance Functor (Triple a b) where fmap f (T (x, y, z)) = T (x, y, (f z)) expresses triples as functors in their third variable. How would I turn them into functors in their second variable? How would I turn actual tuples (not my new type) into a functor? The general question is: suppose I have a parametric type m a b c d e how do I express the parametric

Understanding do notation for simple Reader monad: a <- (*2), b <- (+10), return (a+b)

孤者浪人 提交于 2020-02-24 07:56:05
问题 instance Monad ((->) r) where return x = \_ -> x h >>= f = \w -> f (h w) w import Control.Monad.Instances addStuff :: Int -> Int addStuff = do a <- (*2) b <- (+10) return (a+b) I'm trying to understand this monad by unwiding the do notation, because I think the do notation hides what happens. If I understood correctly, this is what happens: (*2) >>= (\a -> (+10) >>= (\b -> return (a+b))) Now, if we take the rule for >>= , we must understand (*2) as h and (\a -> (+10) >>= (\b -> return (a+b)))

Check, if list is a sublist of another list

风流意气都作罢 提交于 2020-02-24 04:33:33
问题 I'd like to write a function that checks if one list is a sublist of another list. I wrote this, but it is not working, but I need something like this, I guess. Thanks for help. subList :: Eq a => [a] -> [a] -> Bool subList _ [] = False subList [] _ = True subList (x:xs) (y:ys) = x == y = subList xs ys otherwise = subList (x:xs) ys 回答1: Your code is close to working, but just needs some minor changes. As other have said in the comments, you need to include | pattern guards, and remove = from

Check, if list is a sublist of another list

妖精的绣舞 提交于 2020-02-24 04:31:09
问题 I'd like to write a function that checks if one list is a sublist of another list. I wrote this, but it is not working, but I need something like this, I guess. Thanks for help. subList :: Eq a => [a] -> [a] -> Bool subList _ [] = False subList [] _ = True subList (x:xs) (y:ys) = x == y = subList xs ys otherwise = subList (x:xs) ys 回答1: Your code is close to working, but just needs some minor changes. As other have said in the comments, you need to include | pattern guards, and remove = from

「Haskell 学习」一 环境与大致了解

大兔子大兔子 提交于 2020-02-23 11:45:30
感谢《Real World Haskell》在网上的 免费发布 ,可以白嫖学Haskell这个久闻大名的函数式编程语言了。 本文运行于openSUSE Tumbleweed下,运行相关命令时留意。 安装 Linux下想搞和编程相关的事情非常简单,至少比配置游戏要简单。 sudo zypper in ghc ghc即 Glasgow Haskell Compiler ,一个主流的编译器。也支持python式的互动执行(ghci)。笔者运行时该编译器已经是8.0.2版本了。 运行 输入ghci,会进入Haskell的interactive interpreter。大概的运行界面如下: # zuiho @ zuiho-pc in ~ [Time] $ ghci GHCi, version 8.0.2: http://www.haskell.org/ghc/ :? for help Prelude> 根据书中的说法,“The word Prelude in the prompt indicates that Prelude , a standard library of useful functions, is loaded and ready to use. When we load other modules or source files, they will show up in

multiple errors with different positions using megaparsec

对着背影说爱祢 提交于 2020-02-22 06:15:13
问题 I'm going to use megaparsec for parsing a programming language for university project. However, I searched for finding a way to report multiple errors. I know about withRecovery and I saw this issue but I didn't find about the case where errors happen on different positions. for example in this java code : class A { public get() // line 3 column 10 { return x // line 5 column 22 } } There are error1 "expected type at line 3 column 10" and error2 "missing semicolon at line 5 column 22" I know

Scotty monad transformer for per-handler Reader

ⅰ亾dé卋堺 提交于 2020-02-21 12:44:07
问题 In the question Web, Scotty: connection pool as monad reader it is shown how to use ScottyT to embed a Reader monad in the stack to access a static configuration (in that case, a connection pool). I have a similar question, but simpler – or at least I thought so… I want to add a Reader to a single handler (i.e. a ActionT ), not the whole app. I started modifying the program from the question above, but I cannot figure out how to turn an ActionT Text (ReaderT String IO) into the ActionT Text

Haskell - How to transform map sum (map (x:) xss) to map (x+) (map sum xss)

空扰寡人 提交于 2020-02-21 12:05:33
问题 Reading "Thinking Functionally with Haskell" I came across a part of a program calculation that required that map sum (map (x:) xss) be rewritten as map (x+) (map sum xss) Intuitively I know that it makes sense ... if you have some lists that you are going to sum but, before summing, to those same lists you are also going to add one element 'x', then that is the same as taking a list of sums of the origninal lists and adding x's value to each of them. But I would like to know how to transform