我正在关注如何使用monadic形式的yesod书籍示例.我的getRootR动作几乎是从书中逐字记录下来的.我得到了一个编译器错误,剥离了quasiquote但仍然得到了错误.下面是我的错误消息,代码是原样,然后是我想要的getRootR.关于问题可能是什么的任何输入将非常感激.
ghci Rocko.hs
...几个"包装"消息传递到......
Rocko.hs:67:5:
Couldn't match type `handler'
with `GGHandler
Scheduler
Scheduler
(Data.Enumerator.Iteratee Data.ByteString.Internal.ByteString IO)'
`handler' is a rigid type variable bound by
the type signature for getRootR :: handler RepHtml at Rocko.hs:65:1
Expected type: handler RepHtml
Actual type: GGHandler
Scheduler
Scheduler
(Data.Enumerator.Iteratee Data.ByteString.Internal.ByteString IO)
RepHtml
Expected type: handler RepHtml
Actual type: GHandler Scheduler Scheduler RepHtml
In the return type of a call of `defaultLayout'
In the expression:
defaultLayout
(addHtml
((Text.Blaze.Internal.preEscapedText . Data.Text.pack)
"<p>Result: </p>")) …Run Code Online (Sandbox Code Playgroud) 我简化了有问题的功能.我在monad中构建一个列表时遇到了麻烦.我怀疑是一个优先问题.
newtype Boundary = MkBoundary Integer
testFunc :: [Boundary] -> [Maybe Integer]
testFunc (MkBoundary x:xs)
| (even x) = Just x : testFunc xs
| otherwise = Nothing : testFunc xs
testFunc _ = []
Run Code Online (Sandbox Code Playgroud)
这按预期工作.但我需要在monad中工作.我将在这个例子中使用IO
testFunc :: [Boundary] -> IO [Maybe Integer]
testFunc (MkBoundary x:xs)
| (even x) = return $ Just x : testFunc xs
| otherwise = return $ Nothing : testFunc xs
testFunc _ = []
Run Code Online (Sandbox Code Playgroud)
无论我如何操纵优先权,这都会破坏.
test.hs:6:35:
Couldn't match expected type `[Maybe Integer]'
with actual …Run Code Online (Sandbox Code Playgroud) 这是 stack.yaml 节
包:
- location:
git: https://github.com/TwitterFriends/lsh.git
commit: 57d57f4209e56f526c0eca023907015935c26071
extra-dep: true
Run Code Online (Sandbox Code Playgroud)
我将包添加到 cabal 文件中
当我尝试构建时出错
While constructing the BuildPlan the following exceptions were encountered:
-- While attempting to add dependency,
Could not find package lsh in known packages
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
当前项目在这里找到
如何将函数的输出作为另一个函数的输入传递.
例如,我有这两个功能
collatz :: (Integral a) => a -> [a]
collatz 1 = [1]
collatz n
|even n = n:collatz (n `div` 2)
|odd n = n:collatz (n*3 + 1)
Run Code Online (Sandbox Code Playgroud)
而我的另一个功能
length' [] = 0
length' (x:xs) = 1 + length' xs
Run Code Online (Sandbox Code Playgroud)
我想计算列表的长度,这是从我的collatz函数输出的.
最后我想完全计算这个
numLongChains :: Int
numLongChains = length (filter isLong (map collatz [1..100]))
where isLong xs = length xs > 15
Run Code Online (Sandbox Code Playgroud)
但一步一步.
我试图了解功能的类型并能够对其进行解释。
两个功能:
insert :: t -> Bool -> ([t],[t]) -> ([t],[t])
insert a True (b,c) = (a:b,c)
insert a False (b,c) = (b,a:c)
partition :: (t -> Bool) -> [t] -> ([t],[t])
partition p [] = ([],[])
partition p (x : xs) = insert x (p x) (partition p xs)
Run Code Online (Sandbox Code Playgroud)
据我有限的知识,我认为插入功能:
insert 是类型t,它接受两个参数bool和一个类型t的两个列表的元组之一,并返回两个类型t的列表的元组。
partition 是类型t的元组,它返回布尔值,并且将类型t的列表作为其参数,并返回两个类型t的列表的元组。
这是正确的思考方式还是我做错了?我一直在关注一些教程,这是到目前为止我所做的。
我刚刚制作了一个脚本,它从 TSV 文件中获取一些值并以不同的方式格式化它们。该脚本如下所示:
{-# LANGUAGE OverloadedStrings, QuasiQuotes #-}
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
tsvToPat tsv = T.unlines $ map (makePat . (T.replace "-" " ") . head . (T.splitOn "\t")) (T.lines tsv)
main :: IO ()
main = do
pantone <- TIO.readFile "../data/maps/pantone/pantone.tsv"
xkcd <- TIO.readFile "../data/maps/xkcd/rgb.txt"
jaffer <- TIO.readFile "../data/maps/jaffer/master.tsv"
TIO.putStr $ tsvToPat pantone
TIO.putStr $ tsvToPat xkcd
TIO.putStr $ tsvToPat jaffer
makePat :: T.Text -> T.Text
makePat pat = T.concat [ "{\"label\":\"COLOR\",\"pattern\":[{\"lower\":\""
, …Run Code Online (Sandbox Code Playgroud) 鉴于Perl 5不符合BNF,我对如何思考这个问题感到茫然.有人可以提出一些建议让我以正确的方式考虑这个问题吗?
我正在为JIRA编写JSON服务,并且我遇到了与Haskell命名空间冲突的要求.我有这个记录
data Assignee = Assignee {name :: Text} deriving Generic
instance ToJSON Assignee
Run Code Online (Sandbox Code Playgroud)
这是由JIRA想要的,不幸的是它想要一个不同的对象相同的字段.
data Reporter = Reporter {name :: Text} deriving Generic
instance ToJSON Reporter
Run Code Online (Sandbox Code Playgroud)
我看到几个选项:
手动创建JSON对象,但我从此记录中创建它:
data Fields = Fields
{ project :: HashMap Key Project
, summary :: Text
, issuetype :: HashMap Name Task
, versions :: [HashMap Name Text]
, description :: Text
, assignee :: Assignee
} deriving (Generic)
Run Code Online (Sandbox Code Playgroud)手工制作这个想法给了我一些感觉.如果我必须,我会.
所以,我现在的问题是,如果没有比我提出的方式更好的方法,那么这些是最好的行动方案?
我想为Vertex编写基本实现。
data Point a = Point a a
class XY c where
x :: c a -> a
y :: c a -> a
class XY c => Vertex c where
translate :: c a -> c a -> c a
scale :: a -> c a -> c a
rotate :: a -> c a -> c a
instance XY Point where
x (Point first second) = first
y (Point first second) = second
instance Vertex Point where
translate …Run Code Online (Sandbox Code Playgroud) 我编写了第一个计算素数的程序。但是它运行得很慢,我不知道为什么。我用Java编写了类似的代码,对于n = 10000,Java程序无需花费任何时间,而Haskell程序则需要2分钟。
import Data.List
main = do
print "HowManyPrimes? - OnlyInteger"
inputNumber <- getLine
let x = (read inputNumber :: Int)
print (firstNPrimes x)
-- prime - algorithm
primeNumber:: Int -> Bool
primeNumber 2 = True
primeNumber x = primNumberRec x (div x 2)
primNumberRec:: Int -> Int -> Bool
primNumberRec x y
|y == 0 = False
|y == 1 = True
|mod x y == 0 = False
|otherwise = primNumberRec x (y-1)
-- prime numbers till …Run Code Online (Sandbox Code Playgroud)