Chapter 1. Getting Started
GHC 有三个主要components:
ghc
产生native code 的编译器
ghci
交互式解释器及调试器
runghc
将Haskell 程序作为脚本运行而无需编译
Getting Started with ghci, the Interpreter
“Prelude”提示符表示标准库已经加载并可以使用了。
打印帮助的命令“:?”
改变提示符
:set prompt "ghci> "
加载模块
:module + Data.Ratio
现在可以使用比例数了(或称有理数)
Basic Interaction: Using ghci as a Calculator
ghci 可以做为一个桌面计算器
Simple Arithmetic
infix form
ghci> 2 + 2
4
ghci> 31337 * 101
3165037
ghci> 7.0 / 2.0
3.5
Prefix form 必须确保操作符在括号里面
ghci> 2 + 2
4
ghci> (+) 2 2
4
Haskell 支持整数和浮点数,整数可以任意大
ghci> 313 ^ 15
27112218957718876716220410905036741257
An Arithmetic Quirk: Writing Negative Numbers
负数通常要用括号“()”括起来
ghci> -3
-3
这个例子中我们写的并不是“-3”这个数,而是一个operator – 操作符作用于数字3。“-”操作符是Haskell 中唯一的一个unary operator,不可以将它与infix operators 混在一起:
ghci> 2 + -3 --> 发生错误
正确的写法:
ghci> 2 + (-3)
-1
ghci> 3 + (-(13 * 37))
-478
Haskell 将“*-”视为单个操作符
ghci> 2*-3 -->出错
应该这样写:ghci> 2*(-3)
对负数的这些处理似乎是让人讨厌的,但这是因为Haskell允许在任何时侯创建新的操作符。
Boolean Logic, Operators, and Value Comparisons
Haskell 的布尔值是True 和 False。“&&”是“and”,“||”是“or”
ghci> True && False
False
ghci> False || True
True
Haskell 中零不是false,非零不是true
等于:“==”
小于“<”
大于等于“>=”
不等于运算符“/=”
ghci> 2 /= 3
True
逻辑非运算符“not”
ghci> not True
False
Operator Precedence and Associativity
查看运算符的优先级
:info (+) -->infixl 6 +
infixl 6 + 表示优级级是6,优先级中1 最小,9 最大。
Infixl 表示运行符是左结合的,infixr 表示运算符是右结合的
:info (^) -->infixr 8 ^
Undefined Values, and Introducing Variables
预加载的标准库只定了Pi 这一个常量值。
let 语法只是ghci 支持的,不是普通Haskell 程序支持的
let e = exp 1 --> 定义欧位常数
“^”和“**”两个乘方运算符的区别
“^”只能以整数作为指数,而“**”,能以浮点数作为指数。
(e ** pi) – pi -->19.99909997918947
Dealing with Precedence and Associativity Rules
表达式中尽量多用括号。
Command-Line Editing in ghci
略
Lists
list 列表的写法
[1, 2, 3] -- 注意了,未尾不能有多余的逗号
列表可以任意长
[] –空列表
["foo", "bar", "baz", "quux", "fnord", "xyzzy"]
列表的元素必须有相同的类型
列表的补齐运算符“..”(enumeration notation)
[1..10] -->[1,2,3,4,5,6,7,8,9,10]
这个符号对文本类型无效
先写前两项 + “..” + 最后一项会按前面的“增量”补齐列表
[1.0,1.25..2.0] --> [1.0,1.25,1.5,1.75,2.0]
[1,4..15] --> [1,4,7,10,13]
[10,9..1] --> [10,9,8,7,6,5,4,3,2,1]
Operators on Lists
连接两个列表的运算符“++”
[3,1,3] ++ [3,7] --> [3,1,3,3,7]
[] ++ [False,True] ++ [True] --> [False,True,True]
在表的前端添加一个元素的运算符“:”
1 : [2,3] --> [1,2,3]
1 : [] --> [1]
“:” 的前面必须是一个元素,后面必须是一个列表, [1,2]:3 这种写法是错误的。
Strings and Characters
Haskell 中,string 是字符的列表。
"This is a string."
打印字符串的函数putStrLn
putStrLn "Here's a newline -->/n<-- See?"
--> Here's a newline -->
--> <-- See?
‘a’ 是一个char,”a” 是一个list
string 只是字符的列表
let a = [‘h’,’i’]
a == “hi” --> true
“”””是“[]”的另一种写法
"" == [] -->True
list 的运算符全都可以用于string
'a':"bc" --> "abc"
"foo" ++ "bar" --> "foobar"
注意:”a” 是列表,’a’ 只是一个char。
First Steps with Types
整数(Integer) 是可以任意大的,只和系统的内存大小有关
类型名以大写字母开头,变量名以小写字母开头
:set 命令可以改变ghci 的默入行为
用“type”命令打印类型信息
:type 'a'
让ghci显示类型信息
:set +t -- 这是逆命令:unset +t
'c'
--> 'c'
--> it :: Char -- 其中的it 就代表前面的变量,而且可以在后面使用。(只对ghci 有效)
“xx :: type”表示xx 拥有类型type
整数(Integer) 是可以任意大的,只和系统的内存大小有关。
比例数的写法“a%b”
:m +Data.Ratio -- :m 是:module 的缩写
11 % 29
A Simple Program
打印文本文件的行数
-- a.hs
main = interact wordCount
where wordCount input = show (length (lines input)) ++ "/n"
-- a.txt
a small program
that counts the number of lines
in its input.
runghc a <a.txt
--> 3
注意:代码中的缩进是必须的。