Variables
Variables: Local Definition
使用 let ... in
语法,或者 where
语法。where
后置变量定义,let ... in
则是前置。
示例:
1 | solve :: Int -> Int -> [Int] -> String |
Pattern Matching
Pattern Matching
定义函数的时候,可以根据参数的不同,触发不同分支。
1 | greet :: String -> String -> String |
Guards
Condition Guards
和 Pattern Matching 类似,用 Condition 代替具体的值进行 Matching.
示例:
1 | describe :: Int -> String |
当然,Guards 和 Pattern Matching 也可以一起用。
1 | guessAge :: String -> Int -> String |
case ... of
我觉得
case ... of
语法也算 Pattern Matching 的一部分
Recursion
- Helper Function: arguments of the helper function are variables you update in your loop; Tail Recursion Optimization
Haskell programs often use the apostrophe to name helper functions and alternative versions of functions.
Haskell 常用数据结构
List / [a]
1 | head :: [a] -> a -- returns the first element |
实用函数
show :: Any -> String
: 将任何东西变成可以输出的字符串