我的站点

一个系统软件工程师的随手涂鸦

Date: 十二月 8, 2015

Haskell笔记 (7)—— 类型

(1)

Haskell requires type names to start with an uppercase letter, and variable names must start with a lowercase letter.

举例如下:

> let C = "foo"

<interactive>:20:5: Not in scope: data constructor ‘C’
> let c = "foo"
> c
"foo"
> :type c
c :: [Char]

(2)

The combination of :: and the type after it is called a type signature.

举例如下:

> :type 'a'
'a' :: Char

:: Char就被称之为type signature

 

Haskell笔记 (6)—— 字符串

(1)

The empty string is written “”, and is a synonym for [].

举例如下:

> "" == []
True

(2)

Since a string is a list of characters, we can use the regular list operators to construct new strings.

举例如下:

> 'a':"bc"
"abc"
> "foo" ++ "bar"
"foobar"

 

Haskell笔记 (5)—— ghci

(1)

ghci stores the result of the last expression into a variable whose name is “it”. (This isn’t a Haskell language feature; it’s specific to ghci alone.)

举例如下:

> "foo"
"foo"
> it ++ "bar"
"foobar"
> it
"foobar"

(2)可以使用:set <option>:unset <option>来设置和取消一些选项。以打印计算的结果类型为例:

> :set +t
> fst ('a', 'b')
'a'
it :: Char
> :unset +t
> fst ('a', 'b')
'a'

(3)在ghci中定义变量与源文件中定义变量不一样,需要在变量前加上let

ghci> let a = 10
ghci> b = 100

<interactive>:9:3: parse error on input ‘=’

 

Powered by WordPress & Theme by Anders Norén