小编Mic*_*ard的帖子

我的编译器错误是否真的与quasiquote有关,还是更微妙?

我正在关注如何使用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)

haskell yesod

3
推荐指数
1
解决办法
212
查看次数

我的递归列表构造有什么问题?

我简化了有问题的功能.我在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)

monads haskell list

3
推荐指数
1
解决办法
148
查看次数

stack.yaml 不依赖于 github

这是 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)

我究竟做错了什么?

当前项目在这里找到

https://github.com/TwitterFriends/twitter-friend-server

haskell haskell-stack

3
推荐指数
1
解决办法
629
查看次数

将函数的输出作为Haskell中另一个函数的输入传递

如何将函数的输出作为另一个函数的输入传递.

例如,我有这两个功能

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)

我想计算列表的长度,这是从我的collat​​z函数输出的.

最后我想完全计算这个

numLongChains :: Int  
numLongChains = length (filter isLong (map collatz [1..100]))  
    where isLong xs = length xs > 15  
Run Code Online (Sandbox Code Playgroud)

但一步一步.

haskell input output

3
推荐指数
1
解决办法
269
查看次数

了解功能类型

我试图了解功能的类型并能够对其进行解释。

两个功能:

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的列表的元组。

这是正确的思考方式还是我做错了?我一直在关注一些教程,这是到目前为止我所做的。

haskell types functional-programming function semantics

3
推荐指数
3
解决办法
110
查看次数

如何在 Haskell 中重构这些 IO 操作?

我刚刚制作了一个脚本,它从 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)

haskell

3
推荐指数
1
解决办法
67
查看次数

在Haskell中编写Perl代码生成器有哪些步骤?

鉴于Perl 5不符合BNF,我对如何思考这个问题感到茫然.有人可以提出一些建议让我以正确的方式考虑这个问题吗?

haskell code-generation

2
推荐指数
1
解决办法
242
查看次数

你怎么能有两个具有相同字段名称的记录?

我正在为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)

我看到几个选项:

  1. 也许我可以绕过编译器对模板Haskell的抱怨,但是如何?
  2. 我可能根本没有Reporter记录,并在创建故障单后用单独的服务更改记者字段.我知道怎么做,但这是最好的方式吗?
  3. 手动创建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)

手工制作这个想法给了我一些感觉.如果我必须,我会.

所以,我现在的问题是,如果没有比我提出的方式更好的方法,那么这些是最好的行动方案?

json haskell template-haskell

2
推荐指数
2
解决办法
160
查看次数

具有上下文的Haskell数据类型

我想为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)

haskell

2
推荐指数
1
解决办法
109
查看次数

Haskell程序运行非常慢

我编写了第一个计算素数的程序。但是它运行得很慢,我不知道为什么。我用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)

performance haskell functional-programming

2
推荐指数
2
解决办法
96
查看次数