如何在跨多行的ghci中定义一个函数? [英] How to define a function in ghci across multiple lines?

查看:99
本文介绍了如何在跨多行的ghci中定义一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

我试图定义任何跨越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中定义一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆