【问题标题】:How to program haskell with ghci?如何用 ghci 编程 haskell?
【发布时间】:2014-08-26 08:12:35
【问题描述】:

我一直在阅读一些材料,在这里我有一个问题: 我看到一段代码是这样的:

>getNthElem 1 xs = head xs
>getNthElem n [] = error "'n' is greater than the length of the list"
>getNthElem n xs
>    | n < 1     = error "'n' is non-positive"
>    | otherwise = getNthElem (n-1) (tail xs)

我应该在 ghci 中输入完全相同的所有这些行,还是应该创建一个 .hs 文件并将它们放入,然后将其加载到 ghci 中?

【问题讨论】:

  • 我之前都试过了。前者遇到了ghci不识别“=”的错误;而后一种,“getNthElem”出现三次不是没有意义吗?
  • 确实有道理。尝试从一些更基本的语句开始你的 Haskell 冒险。

标签: haskell ghci


【解决方案1】:

有两种方法可以做到这一点:

  1. 通过将标志设置为在 ghci 中使用多行模式:

     Prelude> :set +m
     Prelude> let getNthElem 1 xs = head xs
     Prelude|     getNthElem n [] = error "error1"
     Prelude|     getNthElem n xs
     Prelude|                | n < 1 = error "error2"
     Prelude|                | otherwise = getNthElem (n-1) (tail xs)
     Prelude|
     Prelude>       
    
  2. 创建一个文件并将其作为模块加载,以访问其中定义的类型和函数

    Prelude> :l myModule.hs
    

    和文件内容:

    getNthElem :: Int -> [a] -> a
    getNthElem 1 xs = head xs
    getNthElem n [] = error "'n' is greater than the length of the list"
    getNthElem n xs
                   | n < 1     = error "'n' is non-positive"
                   | otherwise = getNthElem (n-1) (tail xs)
    

我建议使用第二个选项,因为在 GHCI 中的多行模式下很容易弄乱缩进。另外,请养成在开始定义函数体之前添加类型签名的习惯。

【讨论】:

  • 我试过 :set +m 但仍然收到错误。我应该怎么做?谢谢你。 :)
  • 顺便说一句,如何在 cmets 中使用代码样式键入代码?
  • 如果您将let 放在单独的行上,则不需要缩进其余部分(因为GHC 的NondecreasingIndentation 扩展名),这可能更容易复制/粘贴。 (不过,这不适用于识字的&gt;s。)
  • @ØrjanJohansen 感谢您提供的信息!
【解决方案2】:

你可以写在 1 行:

> let getNthElem 1 xs = head xs; getNthElem n [] = error "'n' is greater than the length of the list"; getNthElem n xs | n < 1 = error "'n' is non-positive" | otherwise = getNthElem (n-1) (tail xs)

不要忘记用分号代替换行符,并在开头添加let字。

你也可以使用多线制度:

> :{
| let getNthElem 1 xs = head xs
|     getNthElem n [] = error "'n' is greater than the length of the list"
|     getNthElem n xs
|       | n < 1     = error "'n' is non-positive"
|       | otherwise = getNthElem (n-1) (tail xs)
| :}
>

【讨论】:

    【解决方案3】:

    最简单的方法是创建一个名为 e.g. example.hs 然后在命令行启动ghci并加载文件

    $ ghci
    GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
    Prelude> :load example.hs
    [1 of 1] Compiling Main      ( example.hs, interpreted )
    Ok, module loaded: Main.
    *Main> 
    

    或者,您可以在启动 ghci 时加载文件

    $ ghci example.hs
    GHCi, version 7.6.3: http://www.haskell.org/ghc/  :? for help
    [1 of 1] Compiling Main      ( example.hs, interpreted )
    Ok, module loaded: Main.
    *Main> 
    

    请注意,每行开头的 &gt; 表示您的文件是 literate Haskell 文件,即它应该具有扩展名 *.lhs 而不是 *.hs。您应该将文件重命名为 *.lhs 或删除每行开头的 &gt;

    【讨论】:

    • 我还处于起步阶段,所以我不知道什么是 literate haskell 文件。还是谢谢!
    • @user3928256 literate 文件中的默认文本是注释,而不是源代码,您必须指定源代码在文件中的实际位置。通常会看到一个文字文件,其中的 cmets 比代码多得多,它可以让您混合不同的格式,例如Markdown 和 Haskell,或 ReST 和 Python,因此文档生成器可以更轻松地制作格式良好的文档。在 Haskell literate 文件中,惯例是在每行代码的开头使用 &gt;,或者您可以使用 LaTeX 样式并将代码块包装在 \begin{code}-\end{code} 标签中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 2011-12-18
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多