“Haskell”的版本间的差异

来自wrc's Wiki
跳到导航 跳到搜索
(建立内容为“Category:Haskell Haskell 是一种函数式编程语言。 == 笔记 == === <code>let</code> 与 <code>where</code> === <code>let</code> 是一个表达式…”的新页面)
 
 
(未显示同一用户的2个中间版本)
第3行: 第3行:
  
 
== 笔记 ==
 
== 笔记 ==
 +
 +
=== printf debugging ===
 +
 +
<syntaxhighlight lang=haskell>
 +
import Debug.Trace
 +
 +
dbg :: Show a => String -> a -> a
 +
dbg msg x = trace (msg ++ " " ++ show x) x
 +
</syntaxhighlight>
 +
 +
使用方法:把变量 <code>x</code> 换成 {{code|haskell|2=(dbg "x =" x)}}。
  
 
=== <code>let</code> 与 <code>where</code> ===
 
=== <code>let</code> 与 <code>where</code> ===
第16行: 第27行:
 
</syntaxhighlight>
 
</syntaxhighlight>
 
三个 guard 都可使用 <code>z</code>。用 <code>let</code> 无法达到以上的效果。<ref>https://stackoverflow.com/q/4362328/10974106</ref><ref>[https://www.cse.iitb.ac.in/~as/fpcourse/haskell98_tutorial/patterns.html A Gentle Introduction to Haskell] 4.5 Lexical Scoping and Nested Forms</ref>
 
三个 guard 都可使用 <code>z</code>。用 <code>let</code> 无法达到以上的效果。<ref>https://stackoverflow.com/q/4362328/10974106</ref><ref>[https://www.cse.iitb.ac.in/~as/fpcourse/haskell98_tutorial/patterns.html A Gentle Introduction to Haskell] 4.5 Lexical Scoping and Nested Forms</ref>
 +
 +
== Hlint ==
 +
 +
=== Ignore warnings ===
 +
 +
在 <code>.hlint.yaml</code> 中写:
 +
<syntaxhighlight lang=yaml>
 +
- ignore: {name: Eta reduce}
 +
</syntaxhighlight>
 +
 +
<ref>https://neilmitchell.blogspot.com/2019/01/ignoring-hlint.html</ref>
 +
 +
== 另见 ==
 +
 +
* [[Functor, Applicative 和 Monad]]
  
 
== 外部链接 ==
 
== 外部链接 ==

2021年10月28日 (四) 13:28的最新版本

Haskell 是一种函数式编程语言。

笔记

printf debugging

import Debug.Trace

dbg :: Show a => String -> a -> a
dbg msg x = trace (msg ++ " " ++ show x) x

使用方法:把变量 x 换成 (dbg "x =" x)

letwhere

let 是一个表达式,而 where 不是。where 用于给多个 guard 创建 binding,如下:

f x y
  | y > z  = ...
  | y == z = ...
  | y < z  = ...
  where
    z = x * x

三个 guard 都可使用 z。用 let 无法达到以上的效果。[1][2]

Hlint

Ignore warnings

.hlint.yaml 中写:

- ignore: {name: Eta reduce}

[3]

另见

外部链接

参考资料