小编And*_*ewC的帖子

来自Haskell中[String]的模式匹配

我正在学习函数式编程的入门课程,我们使用Haskell.练习的一部分是为输入字符串编写解析器.

但是我无法解决以下错误,或者得到实际发生的情况.

Parser.hs:29:71:
Couldn't match expected type `String' with actual type `Char'
In the first argument of `readPoint', namely `start'
In the expression: readPoint start
In the expression:
  (readLines track, readPoint start, readLine finish)
Run Code Online (Sandbox Code Playgroud)

错误源自此行:

readTrack str = parseTrack (lines str) where
    parseTrack (start : finish : track) = (readLines track, readPoint start, readLine finish)
Run Code Online (Sandbox Code Playgroud)

我期望发生的是输入字符串被拆分成一个行列表,然后传递给parseTrack.然后,parseTrack将使用模式匹配来命名列表中的前两个字符串(行)以及其余的字符串.

然而,我认为正在发生的是,finish是列表中的顶级元素,并且start从该字符串分配了顶部char.

我真的很想知道如何解决这个问题以及实际发生的事情.

非常感谢!

Parser.hs

module Parser where

import Types

readFloat :: String -> Float
readFloat str = case reads str of
    [] -> error …
Run Code Online (Sandbox Code Playgroud)

haskell list

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

使GHC接受主模块的主要功能不是IO()

是否有某种方式(旗帜或黑客)使GHC接受主要功能的签名不是主模块IO ()?对于Fay,主要功能具有类型Fay (),但如果模块为Main(或模块名称省略),GHC不接受此功能.

haskell ghc

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

模板元编程的Haskell变体

我是Haskell的新手.鉴于Haskell的整个前提是函数将始终返回相同的值,我希望有一些方法可以在编译时计算常量的fibonacci值,就像我可以在C++中使用模板元编程一样,但我可以看不出怎么做.有办法吗?

haskell template-haskell

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

Cabal无可挽回地破碎; 我接下来可以尝试什么?

我无法使用cabal安装或更新软件包,事实上,我收到的消息是Cabal"无法使用".我被困在Cabal-1.10.

所以,我有想法手动安装一些软件包.

首先我尝试'目录':但不,这需要Distribution.Simple报告为'缺失'.

可以通过手动安装新版本的Cabal来解决该问题:1.16.0.1.

但不,因为无法找到'目录'而失败.

因此要安装'目录'我必须有Cabal; 安装Cabal我必须有'目录'.

这是递归,但不是我们想要的方式!

除了完全卸载和重新安装Haskell平台之外,还有其他方法可以解决这种循环依赖吗?

(顺便说一句,我正在运行Windows 7)

haskell cabal

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

如何组合一次字符串中的字符?

我已经在互联网上搜索了一段时间来解决这个问题.我需要在Haskell中创建一个函数,它从输入字符串中创建所有可能的组合,而不会重复.

例:

combination "new"
Run Code Online (Sandbox Code Playgroud)

将作为输出:

["new","nwe","enw","ewn","wne","wen"]  
Run Code Online (Sandbox Code Playgroud)

有人可以帮我这个吗?

haskell

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

什么是获得列表中间元素的最有效方法?

我想得到一个列表,但最后和第一个元素.什么是最有效的方式?

middle = init . tail
Run Code Online (Sandbox Code Playgroud)

要么:

middle = tail . init
Run Code Online (Sandbox Code Playgroud)

在丢弃n-first元素和n-last元素的情况下?

haskell functional-programming

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

Int to Float转换:Num [Int]没有实例

我需要使用该map函数来获得例如pence到磅的转换.抱歉这个愚蠢的问题..但我是初学者.

del :: Int -> Float
del x =  ( fromIntegral x ) / 100

pounds :: [Int] -> [Float]
pounds = map del 
Run Code Online (Sandbox Code Playgroud)

我收到这个错误..

*Main> pounds 45
<interactive>:90:8:
    No instance for (Num [Int])
      arising from the literal `45'
    Possible fix: add an instance declaration for (Num [Int])
    In the first argument of `pounds', namely `45'
    In the expression: pounds 45
    In an equation for it': it = pounds 45
Run Code Online (Sandbox Code Playgroud)

floating-point int haskell

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

如何计算函数的总和?

我有一个文件(points.txt)定义了一些笛卡尔坐标:

A 1.0 2.2
B 2.1 3.0
C 3.5 4.0
D 4.0 5.0
Run Code Online (Sandbox Code Playgroud)

我有第二个文件(routes.txt),其路由根据points.txt中的点定义.

route1 ACDB
route2 ABC
Run Code Online (Sandbox Code Playgroud)

我需要找到每条路线的长度.到目前为止,我计算了两点之间的距离,如下所示:

type Point = (String, Float, Float)

distance_points :: IO ()
distance_points = do s <- readFile "pontos.txt"
                      putStr "Fom: "
                      p1 <- getLine
                      putStr "To: "
                      p2 <- getLine
                      print ( distance (search_point p1 (map words (lines s))) (search_point p2 (map words (lines s))))

search_point :: String -> [[String]] -> Point
search_point pt ([p,c1,c2]:xs) = if pt == p then (p, …
Run Code Online (Sandbox Code Playgroud)

haskell

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

Haskell中不完整的隐式类型推断

让我们考虑以下示例:

data A = A{x::Int} deriving(Show)

instance Func_f (A -> String) where
    f _ = "ala"

class Func_f a where
    f :: a

main :: IO ()
main = do 
    let 
        a = A 5
        x = f a 
    print 5
Run Code Online (Sandbox Code Playgroud)

用.编译 ghc -XFlexibleInstances main.hs

(我试过了-XExtendedDefaultRules,但没有任何进展)

为什么在编译时会出现错误?:

main.hs:25:21:
    No instance for (Func_f (A -> t0)) arising from a use of `f'
    The type variable `t0' is ambiguous
    Possible fix: add a type signature that fixes these type variable(s) …
Run Code Online (Sandbox Code Playgroud)

haskell types overloading type-inference typeclass

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

以有效的方式从列表中获取最后一个元素

在Haskell中获取列表的最后一个元素的最有效方法是什么?

示例:getLastElement [1,2,3,4]应该返回4.

据我所知,last [1,2,3,4]由于Haskell挖掘列表导致列表长度O(n)在哪里的效率,因此效率不高n.

haskell

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