小编Mic*_*ard的帖子

数据记录的类约束

我有一个data类型:

data BuildException a = KillBuild JobID a Stage
                      | FailBuild JobID a Stage
                      | CancelBuild JobID a Stage
                      | StopBuild JobID a Stage
                         deriving Typeable
Run Code Online (Sandbox Code Playgroud)

其中a必须有一个Foo类的实例.我记得读过(在RWH中,也许)虽然可能在data定义中有类约束,但这是不可取的.那么这样做的正确方法是什么?

haskell types

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

有人可以解释这种类型错误"无法匹配预期类型`IO()'与实际类型`a0 - > c0'"

这是我得到的类型错误,其次是相关代码.这可能是因为函数组合的使用不正确,如果是这样,我想解释一下如何解决这个问题.如果这是别的什么,我也想谈谈这个.

frameworkDaemon.lhs:125:5:
Couldn't match expected type `IO ()' with actual type `a0 -> c0'
In the expression: popJobState . popProcessConfig . readJobFile
In an equation for `makeJobState':
    makeJobState world
      = popJobState . popProcessConfig . readJobFile
      where
          readJobFile
            = do { let ...;
                   .... }
            where
                getFileContents (x : xs) = readFile (ixiaRoot ++ "/" ++ (show x))
          popProcessConfig = undefined
          popJobState = undefined
Run Code Online (Sandbox Code Playgroud)

失败,模块加载:无.

 makeJobState :: JobState -> IO ()
 makeJobState world =
   popJobState . popProcessConfig . readJobFile -- f …
Run Code Online (Sandbox Code Playgroud)

haskell

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

有人可以为我阐明类型安全的用途吗?

我正在使用类型安全来消毒字符串,但我希望更多的用途明确表达.

haskell types

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

有人可以向我解释我应该如何修复这种类型的签名?

这是代码,我试着让类型推断找出函数的类型.代码编译时,它在运行时失败.

Ambiguous type variables `b0', `m0' in the constraint:
  (PersistBackend b0 m0) arising from a use of `isFree'
Probable fix: add a type signature that fixes these type variable(s)
In the expression: isFree testDay
In an equation for `it': it = isFree testDay
Run Code Online (Sandbox Code Playgroud)

:t isFree

isFree :: PersistBackend b m => C.Day -> b m Bool

>isFree day = do
   match <- selectList [TestStartDate ==. day,
                        TestStatus !=. Passed,
                        TestStatus !=. Failed] []
   if (L.null match) then (liftIO $ return …
Run Code Online (Sandbox Code Playgroud)

haskell types persistent yesod

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

fold function keep switching var locations

I want to write a function which takes a list of integers and returns a list where every element is negative.

negate :: [Int] -> [Int]
negate xs = foldl (\x xs -> (abs x * (-1)) : xs) [] xs
Run Code Online (Sandbox Code Playgroud)

This function negate all the array objects but also reverse the locations of all variables in the array. What make this function reverse the locations?

recursion haskell fold

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

Haskell无点样式,表达式中无功能

我一直在尝试采用一些简单的功能,并将其转换为无点样式进行练习。我开始是这样的:

zipSorted x y = (zip . sort) y $ sort x --zipSorted(x, y) = zip(sort(y), sort(x))
Run Code Online (Sandbox Code Playgroud)

并最终将其转换为

zipSorted = flip (zip . sort) . sort
Run Code Online (Sandbox Code Playgroud)

(我不确定这是否是最好的方法,但确实可行)

现在,我试图不让它依赖于进一步降低这种表达zip,并sort在所有。换句话说,我正在寻找此功能:(如果我的词汇没有误,我认为它是一个组合器)

P(f, g, x, y) = f(g(y), g(x))
Run Code Online (Sandbox Code Playgroud)

sort出现两次但只传递一次的事实提示我应该使用应用函子运算符,<*>但由于某种原因我不知道怎么做。

根据我的理解,(f <*> g)(x) = f(x, g(x))因此,我尝试以这种形式重新编写第一个无点表达式:

flip (zip . sort) . sort
(.) (flip $ zip . sort) sort
(flip (.)) sort $ flip (zip . sort)
(flip (.)) sort $ flip $ (zip .) …
Run Code Online (Sandbox Code Playgroud)

haskell combinators pointfree

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

Haskell Newbie-发生检查:无法构造无限类型:a〜[a]

我试图做一个带有3个参数的函数“ tokenize”。main String,应该在自己的String中的字符字符串,以及要从字符串中删除的chars字符串。

tokenize :: String -> String -> String -> [String]
tokenize [] imp remm = []
tokenize str imp remm =   let chr = (head str) in
                          if elem chr imp then ([chr] : (tokenize (tail str) imp remm))
                          else if (elem chr  remm ) then (tokenize (tail str) imp remm)
                          else chr: (tokenize (tail str) imp remm)
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

Occurs check: 
cannot construct the infinite type: a ~ [a]
Expected type: [a]
Actual type: [[a]]
Run Code Online (Sandbox Code Playgroud)

haskell

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

使用折叠的总距离

我想得到给定列表的总距离,其中包含Floats元组。我必须保证少于2个元素的列表将输出0.0

我到目前为止所做的是:

distancia :: [(Float,Float)] -> Float
distancia [] = 0.0
distancia [(_,_)] = 0.0
distancia (x:y:xs) = foldl(\(xa,ya)(xb,yb) -> sqrt(((xa-xb)**2)+((ya-yb)**2))) 0 xs
Run Code Online (Sandbox Code Playgroud)

所以我期望的输出是

ghci> distancia [(0,0), (0,0), (1,0), (1,10)]
11.0
ghci> distancia [(1,1), (3,4)]
3.6055512
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

t3_fc42035.hs:9:22: error:
    * Couldn't match expected type `Float'
                  with actual type `(Float, Float)'
    * In the expression:
        foldl
          (\ (xa, ya) (xb, yb) -> sqrt (((xa - xb) ** 2) + ((ya - yb) ** 2)))
          0
          xs
      In an equation for `distancia': …
Run Code Online (Sandbox Code Playgroud)

haskell tuples fold

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

Haskell Pascal的三角形

我必须创建一个包含两个数字(n和k)并返回其二项式系数的递归函数。

我必须使用pascal :: Int-> Int-> Int

老实说,我不明白哪里出了问题,谢谢您的帮助!

pascal :: Int -> Int -> Int
pascal n k
      | k == 0 = 1
      | n == n = 1
      | k > n  = 0
      | otherwise = pascal(n-1)(k-1) + pascal(n-1) + k
Run Code Online (Sandbox Code Playgroud)

以下错误是:

main.hs:7:40: error:
    • Couldn't match expected type ‘Int’ with actual type ‘Int -> Int’
    • Probable cause: ‘pascal’ is applied to too few arguments
      In the second argument of ‘(+)’, namely ‘pascal (n - 1)’
      In …
Run Code Online (Sandbox Code Playgroud)

haskell

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

Haskell 函数中的非穷举模式

我需要实现在列表头部插入两个元素的函数,但我得到

Exception: <interactive>:7:5-41: Non-exhaustive patterns in function addTwoElements
Run Code Online (Sandbox Code Playgroud)

该函数的代码如下

addTwoElements a b [xs]= a : b : [xs]
Run Code Online (Sandbox Code Playgroud)

提前致谢

haskell

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

标签 统计

haskell ×10

types ×3

fold ×2

combinators ×1

persistent ×1

pointfree ×1

recursion ×1

tuples ×1

yesod ×1