如何在Haskell中的函数内声明变量的类型?

时间:2013-05-14 17:19:58

标签: haskell

这是Defining variables inside a function Haskell

提出的问题的重新设定

我有一个函数,其开头看起来像这样:

recursiveLs :: FilePath -> IO [FilePath]
recursiveLs dir =
   do
       folderExists <- doesDirectoryExist dir
       if folderExists
          then ...

问题是,在动作中分配folderExists之前,如何明确声明{{1}}的类型?

1 个答案:

答案 0 :(得分:7)

好吧,让我们尝试在ghci

中做你想做的事
> (a :: Integer) <- return 10

<interactive>:2:7:
    Illegal type signature: `Integer'
      Perhaps you intended to use -XScopedTypeVariables
    In a pattern type-signature

所以,我们应该启用该编译指示。

> :set -XScopedTypeVariables

再试一次

> (a :: Integer) <- return 10
a :: Integer

现在我们a等于10,即Integer

> a
10
it :: Integer

此外,我相信您忘记了=函数中的recursiveLs,应该有类似recursiveLs dir = do ...

的内容