小编For*_*eld的帖子

如果启用了 OverloadedStrings 扩展,则函数长度“a”不会编译

如果“ {-# LANGUAGE OverloadedStrings #-} ”包含在源文件的顶部或 package.yaml 中(我使用的是堆栈),则

length "a"  -- does not compile anymore.
Run Code Online (Sandbox Code Playgroud)

但是自定义功能length'工作正常

length' :: [a] -> Int 
length' xs = sum [1 | _ <- xs] 
Run Code Online (Sandbox Code Playgroud)

Data.String是进口的 - 我认为问题就在那里,但是,我有兴趣看看,如果有人有类似的问题。

堆栈和 GHC 版本:2.3.1 版,Git 修订版 x86_64 hpack-0.33.0,ghc-8.8.3

我正在使用 mac osx,但在 Linux 和 Windows 中也有同样的错误。

这是堆栈产生的错误:

/Users/admin1/Haskell/PROJECTS/orig1/src/Lib.hs:13:29: error:
    • Ambiguous type variables ‘t0’,
                               ‘a0’ arising from the literal ‘"a"’
      prevents the constraint ‘(IsString (t0 a0))’ from being solved.
      Probable fix: use a type annotation …
Run Code Online (Sandbox Code Playgroud)

haskell

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

哈斯克尔。匹配模式问题。无法输入带有空列表“print $ note1 []”的函数的 IO 值 - 编译失败

哈斯克尔。匹配模式问题。无法输入空列表函数的 IO 值

print $ note1 []
Run Code Online (Sandbox Code Playgroud)

无法编译,但在 ghci 中工作正常?!另外,print $ note1 [1]工作正常,编译罚款了。只有空列表的问题:

print $ note1 []
Run Code Online (Sandbox Code Playgroud)

(注意我是 Haskell 的新手)我有一个匹配的模式函数

note1          :: (Show a) => [a] -> String
note1 []       = "Empty"
note1 (x:[])   = "One"
Run Code Online (Sandbox Code Playgroud)

print $ note1 []无法编译,但在 ghci 解释器中完美运行?!

我在 MacOS 上使用 stack 2.3.1 和 ghc 8.8.3。

这是编译器产生的编译错误。

    /Users/admin1/Haskell/PROJECTS/orig1/src/Lib.hs:18:13: error:
    • Ambiguous type variable ‘a0’ arising from a use of ‘note1’
      prevents the constraint ‘(Show a0)’ from being solved.
      Probable fix: use a …
Run Code Online (Sandbox Code Playgroud)

haskell

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

标签 统计

haskell ×2