小编dav*_*420的帖子

为什么不这个类型检查呢?

这是玩具示例.s:

{-# LANGUAGE ImpredicativeTypes #-}

import Control.Arrow

data From = From (forall a. Arrow a => a Int Char -> a [Int] String)

data Fine = Fine (forall a. Arrow a => a Int Char -> a () String)

data Broken = Broken (Maybe (forall a. Arrow a => a Int Char -> a () String))

fine :: From -> Fine
fine (From f) = Fine g
  where g :: forall a. Arrow a => a Int Char -> a …
Run Code Online (Sandbox Code Playgroud)

haskell impredicativetypes

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

如何避免模板 Haskell 声明引用中的额外缩进?

我有一个玩具程序:

$ cat a.hs
main = putStrLn "Toy example"
$ runghc a.hs
Toy example
Run Code Online (Sandbox Code Playgroud)

让我们向其中添加一些模板 Haskell:

$ cat b.hs
{-# LANGUAGE TemplateHaskell #-}
id [d|
main = putStrLn "Toy example"
|]
$ runghc b.hs

b.hs:3:0: parse error (possibly incorrect indentation)
Run Code Online (Sandbox Code Playgroud)

那么,让我们修复缩进:

$ cat c.hs
{-# LANGUAGE TemplateHaskell #-}
id [d|
 main = putStrLn "Toy example"
 |]
$ runghc c.hs
Toy example
Run Code Online (Sandbox Code Playgroud)

一个空格就足够了,但我必须缩进两个尾随行。

我可以避免缩进我的大部分模块吗?(我的 Real Modules 不止一行代码。)(而且没有使用{ ; ; }符号?)

我确实希望在引用中捕获所有模块声明 - 在正常代码中我可以替换(...)$ ... …

haskell template-haskell

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

函数`(y*y)<x'应用于两个参数,但其类型`Bool'没有

所以我正在研究问题31.

我写了以下函数,希望确定一个数字是否为素数:

isPrime :: Integer -> Bool

isPrime x = prime x 2
            where
            prime :: Integer -> Integer -> Bool
            prime x y | ((y*y) < x) and ((x `mod` y) /= 0) = prime x (y+1)
                      | ((y*y) >= x) = True
                      | otherwise = False
Run Code Online (Sandbox Code Playgroud)

我的逻辑是创建一个isPrime函数,并在isPrime调用prime中存储一个函数来存储2个参数,我要检查的数字是否为prime(x)和迭代器来检查x的sqrt以下的所有数字并查看它们是否分开x.prime有3名警卫:

| ((y*y) < x) and ((x `mod` y) == 0) = prime x (y+1)
Run Code Online (Sandbox Code Playgroud)

这一行应该说:我传递的数字是否小于x(((y*y) < …

haskell

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

处理Haskell中的列表列表

我是Haskell的初学者,所以我在使用严格类型的东西时会苦苦挣扎,只是想知道是否有人可以帮助我使用我正在尝试构建的函数.基本上,它需要一个列表列表,例如:

[[1,2,3], [7,6,8], [0,3,4]]
Run Code Online (Sandbox Code Playgroud)

并将它们一起添加到一个列表中,通过沿着它的位置数来翻译后面的列表.因此,在示例列表上工作实际上将执行以下操作:

foldl (zipWith +) [] [[1,2,3],[0,7,6,8],[0,0,0,3,4]]
Run Code Online (Sandbox Code Playgroud)

这是我当前的函数(获取类型错误):

    addLists :: [[Integer]] -> [Integer]
    addLists [[]] = []
    addLists [[x]] = [x]
    addLists [x:xs] = zipWith (+) [x] ([0]++ (addLists xs))
Run Code Online (Sandbox Code Playgroud)

haskell list

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

将monad绑定到复合材料(Haskell)

美好的一天.我正在寻求对monads的一些澄清,并使用bind(在进入复合材料时.)请.

所以对于这个例子:

--Monadic parts:
readFile :: String -> IO File
putStr :: String -> IO()
-- Non monadic parts
toMatrix :: String -> CustomMatrix
toString :: CustomMatrix -> String
Run Code Online (Sandbox Code Playgroud)

基本上我懒得读取一个文件(readFile)然后生成一个自定义矩阵,将矩阵转换为字符串输出.然后回来.

fileReading :: String -> IO
fileReading file = putStr(toString . toMatrix . readFile file)
Run Code Online (Sandbox Code Playgroud)

这是我开始通过使用bind >>=来创建一个混乱的时候readFile file.有没有办法我可以继续使用复合材料.,绑定和组合而不会造成难以理解的混乱(不是我真正的目标).

一如既往,感谢任何帮助.谢谢大家.

monads haskell bind composite

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

Haskell-在列表中查找元素并返回其位置

所以我需要将一个函数描述为

invFib :: Integer -> Maybe Integer
Run Code Online (Sandbox Code Playgroud)

取一个整数并在斐波那契序列中查找它(如下面的函数所述)

fibs :: [Integer]
fibs = 0:1:(zipWith (+) fibs (tail fibs)) 
Run Code Online (Sandbox Code Playgroud)

并返回数字示例的索引:

invFib 0 〜> Just 0

invFib 1〜> Just 1Just 2

map invFib [54, 55, 56] 〜> [Nothing,Just 10,Nothing]

invFib (fibs !! 99) 〜> Just 99

我尝试创建一个函数,它获取整数列表并吐出索引,但它仍然失败.有什么想法吗?

这是我试过的功能 -

findNum :: [Integer] -> Integer -> Integer -> Integer
findNum x:xs y z = if x == y
                then z
                else findNum xs y (z+1)
Run Code Online (Sandbox Code Playgroud)

编辑:该函数冻结不在斐波那契序列中的数字,也只在输入1时显示1个值

invFib :: Integer -> …
Run Code Online (Sandbox Code Playgroud)

indexing haskell list fibonacci maybe

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

在正在运行的haskell程序中生成输出

来自(SWI)Prolog我发现很难让Haskell动态提供输出.

最简单的例子,我希望Haskell在每次迭代时打印一些东西:

fac 0 = 1  
fac n = fac ( n-1 ) * n
Run Code Online (Sandbox Code Playgroud)

或者我想从一个永不停止的程序中获得输出...

-- A possible halt statement...  
-- find_primes l 100 = l  
find_primes l n = if ( is_prime l n ) then find_primes nn s else find_primes l s  
where   s = n + 1
nn = n:l

is_prime :: Integral a => [a] -> a -> Bool  
is_prime [] n = True  --print the prime number on the fly    
is_prime (h:t) n = …
Run Code Online (Sandbox Code Playgroud)

monads haskell on-the-fly

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

Haskell合并多个列表

我想编写一个合并函数,它接受多个x排序列表,并通过增量值(从最小到最大)将它们合并到一个排序列表中.我想我可以做两个列表并合并为一个,但无法弄清楚多个列表的基本情况并合并为一个排序.

merge :: [[a]] -> [a]
Run Code Online (Sandbox Code Playgroud)

merge haskell functional-programming

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

hGet的语义

我正在寻找一个功能

foo :: Handle -> ByteString
Run Code Online (Sandbox Code Playgroud)

这将给我缓冲区中的所有内容,如果它是空的则阻止,如果它关闭则返回"".

我有一个应用程序,我正在打电话

Data.ByteString.Char8.hGet handle 1
Run Code Online (Sandbox Code Playgroud)

并且一切都运作良好,但是多次呼叫系统调用有点浪费.不幸的是,如果我将它增加到2,我的应用程序会间歇性挂起,这告诉我hGet等待缓冲区已满(或者至少大于1).

我错过了什么?

haskell

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

模棱两可的发生`Just'

我是一个绝对的初学者.使用emacs通过LYAH.

我目前的设置:

  • Ubuntu 12.04 LTS(使用体验 - 初学者)
  • GNU Emacs 23(使用经验 - 初学者)
    • 能够在haskell主要模式下工作

这里描述的第2点找到难以遵循的指令(带来haskell库).

还需要指导才能启用Scion IDE.

问题:

.hs代码

data Maybe a = Nothing | Just a
Run Code Online (Sandbox Code Playgroud)

在运行代码时,我收到以下错误:

请忽略拼写错误,原贴:

*Main> just "Haha"  
Run Code Online (Sandbox Code Playgroud)

互动>:339:1:不在范围内:'只是'

这是真正的错误(在Tikhon Jelvis的评论之后添加):

*Main> Just "Haha"  
interactive>:341:1:  
    Ambiguous occurrence `Just'  
    It could refer to either `Main.Just',    
                             defined at /home/optimight/baby.hs:89:26  
                          or `Prelude.Just',  
                             imported from `Prelude' at /home/optimight/baby.hs:1:1  
                             (and originally defined in `Data.Maybe')  
Run Code Online (Sandbox Code Playgroud)

emacs haskell ghc ghci haskell-platform

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