# 第一章 Hello World, Etc.

1 How does the Haskell system respond to the following command?

a "Natubmar"

b "tanbuRam"

c "Nambutar"

d natubmaR

HASKELL COMMAND • "frame" == reverse "emarf"

a True

b False

c Yes

d assigns emarf, reversed, to frame

3 And this one?

HASKELL COMMAND • "toh oot" == (reverse "too hot")

a True

b False

c Yes

d no response — improper command

4 And, finally, this one?

HASKELL COMMAND • reverse ("too hot" == "to hoot")

a True

b False

c Yes

d no response — improper command

======================================================================

=======================================================================

1 d

"Rambutan"实际上就是['R', 'a', 'm', 'b', 'u', 't', 'a', 'n']，String是[Char]的语法糖

reverse函数是真正多态的，参数不仅可能是字符串，还可以是任意的List，reverse [1,2,3]的结果等于[3,2,1]

reverse                 :: [a] -> [a]
#ifdef USE_REPORT_PRELUDE
reverse                 =  foldl (flip (:)) []
#else
reverse l =  rev l []
where
rev []     a = a
rev (x:xs) a = rev xs (x:a)
#endif



2 a

data Bool = False | True



3 a

reverse是函数function，"too hot"是操作数operation，函数运算是最优先的，

reverse "ELBA" == "ABLE"   与   (reverse "ELBA") == "ABLE"是等价的

4 d

reverse函数后面只能跟一个列表，("too hot" == "to hoot")这个值是一个布尔值False，reverse函数需要的参数是List，所以reverse False会在Haskell中报错。

《Two Dozen Short Lessons in Haskell》学习（一）Hello World

《Two Dozen Short Lessons in Haskell》学习（二）Definitions

《Two Dozen Short Lessons in Haskell》学习（四）List Comprehensions

《Two Dozen Short Lessons in Haskell》学习（五）Function Composition and Currying

《Two Dozen Short Lessons in Haskell》学习（六）Patterns of Computation – Composition, Folding, and Mapping

《Two Dozen Short Lessons in Haskell》学习（七）- Types

《Two Dozen Short Lessons in Haskell》学习（八）- Function Types, Classes, and Polymorphism

《Two Dozen Short Lessons in Haskell》学习（九）- Types of Curried Forms and Higher Order Functions

《Two Dozen Short Lessons in Haskell》学习（十）- Private Definitions — the where-clause

《Two Dozen Short Lessons in Haskell》学习（十一）- Tuples

《Two Dozen Short Lessons in Haskell》学习（十二） 数值相关的类

《Two Dozen Short Lessons in Haskell》学习（十三）迭代及重复的常规模式

《Two Dozen Short Lessons in Haskell》学习（十四）截断序列和惰性求值

《Two Dozen Short Lessons in Haskell》学习（十五）- Encapsulation — modules

《Two Dozen Short Lessons in Haskell》学习（十六）- Definitions with Alternatives

《Two Dozen Short Lessons in Haskell》学习（十七） - 模块库

《Two Dozen Short Lessons in Haskell》学习（十八） - 交互式键盘输入和屏幕输出

《Two Dozen Short Lessons in Haskell》学习（十九) - 文件输入与输出

《Two Dozen Short Lessons in Haskell》学习（二十）- 分数

《Two Dozen Short Lessons in Haskell》学习（二十一）- 在形式参数中使用模式匹配

《Two Dozen Short Lessons in Haskell》学习（二十二）- 递归

《Two Dozen Short Lessons in Haskell》（二十四）代数类型

posted @ 2012-11-01 08:18  申龙斌的程序人生  阅读(1468)  评论(0编辑  收藏  举报