Haskell表达式中允许换行的位置在哪里?

时间:2015-06-12 19:10:37

标签: haskell coding-style

背景

大多数样式指南建议将行长度保持在79个字符或更少。在Haskell中,缩进规则意味着表达式经常需要用新行分解。

问题:

在表达式中,放置新行是合法的吗?

这是在某处记录的吗?

扩展问题:我看到GHC在报告错误时格式化我的代码,因此有人已经想出如何自动化打破长线的过程。是否有一个实用程序,我可以将haskell代码放入并让它吐出格式良好的代码?

1 个答案:

答案 0 :(得分:5)

您可以在表达式的词法标记之间的任何位置放置换行符。但是,对换行符可能有多少缩进有限制。简单的经验法则是将下一行缩进以从包含表达式的行的右侧开始。除此之外,还有一些风格:

  • 如果要缩进定义var uniqAuthors = Object.keys(uniqs); var uniqFullQuote = Object.keys(fullQuote); var quoteCount = exportedQuotes.author.length; //Loop through all quotes and assign all quotes to a unique author::Each author has many quotes for(var i = 0; i < uniqFullQuote.length; i++){ for(var j = 0; j < exportedQuotes.author.length; j++){ //If the author in the unique list is equal to the author in the duplicate list if(uniqFullQuote[i] == exportedQuotes.author[j]){ //if an author has not had a quote attributed to its name if(fullQuote[exportedQuotes.author[j]] == null){ //assign the author an array with the current quote at the 0 index fullQuote[exportedQuotes.author[j]] = [exportedQuotes.quote[j]] } else { //if an author already has a quote assigned to its name then just add the current quote to the authors quote list fullQuote[exportedQuotes.author[j]].push(exportedQuotes.quote[j]) } } } } 中显示的表达式,缩进name = expression符号的右侧是一种很好的方式。

  • 如果要缩进显示在=绑定或列表理解的右侧的表达式,则缩进do符号的右侧是一种很好的方式。

权威文档可能是Haskell 98 Report(词法结构Chapter 2),但我个人认为这种材料不易阅读。