问题描述
我试图定义任何跨越ghci多行的简单函数,让abs n | n> = 0 = n
|否则= -n
到目前为止,我已经在第一行之后按Enter键:
Prelude>让abs n | n> = 0 = n
前奏> |否则= -n
< interactive>:1:0:输入分析错误'|'
我也曾尝试使用:{
和:}
命令,但我并没有走得太远:
Prelude> :{
未知命令':{'
use:?求助。
我在Linux上使用GHC Interactive版本6.6来测试Haskell 98,我错过了什么?对于守卫(比如你的例子),你可以把它们全部放在一行上,它可以工作(守卫不关心间距)
let abs n | n> = 0 = n |否则= -n
如果你想用你的函数写出多个模式与参数匹配的定义,像这样:
pre code $ fact $ 0 $ 1
fact n = n * fact(n-1)
然后您可以使用分号分隔定义
let {fact 0 = 1;事实n = n * fact(n-1)}
I'm trying to define any simple function that spans multiple lines in ghci, take the following as an example:
let abs n | n >= 0 = n
| otherwise = -n
So far I've tried pressing Enter after the first line:
Prelude> let abs n | n >= 0 = n
Prelude> | otherwise = -n
<interactive>:1:0: parse error on input `|'
I've also attempted to use the :{
and :}
commands but I don't get far:
Prelude> :{
unknown command ':{'
use :? for help.
I'm using GHC Interactive version 6.6 for Haskell 98 on Linux, what am I missing?
for guards (like your example), you can just put them all on one line and it works (guards do not care about spacing)
let abs n | n >= 0 = n | otherwise = -n
if you wanted to write your function with multiple definitions that pattern match on the arguments, like this:
fact 0 = 1
fact n = n * fact (n-1)
then you would use braces with semicolons separating the definitions
let { fact 0 = 1 ; fact n = n * fact (n-1) }
这篇关于如何在跨多行的ghci中定义一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!