算法学社
記錄難忘的征途
posts - 141,comments - 220,trackbacks - 0

前奏:

    为毛要写这个呢... 因为本沙茶除了做算法题和会那么一丁丁丁丁点的Linux基本操作就什么都不会了...
    所以... 不知道为毛就要学这个了... 神经病发作?
    但是figo说要学那就一定学了..........................

Haskell的安装

    地址在这里,下面简述一下本沙茶在安装的时候遇到的问题(系统为Ubuntu 10.04)。
    安装之后(之前就不说了,看文档都会吧...)。运行gchi会遇到一个问题
    
    解决方法在这里

Simple Arithmetic

    运行gchi
    下面测试一些数学表达式吧
 1 ghci>4/3
 2 1.3333333333333333
 3 ghci> 1+1
 4 2
 5 ghci>1000 *10000000
 6 10000000000
 7 ghci>34 ^ 28
 8 7610438437126150739469436175738091335581696
 9 ghci> 2 --2
10 2
11 ghci>2-(-2)
12 4
13 ghci>(-) 2 2
14 0

   需要注意的一些地方:
      1. 整数/整数 自动转为浮点数...
1 ghci>(4/2) ^2
2 4.0
3 ghci>2 ^ 2
4 4
      上面是证据...
      2. 负数要加括号
      3. 算数操作符可以当做函数使用(以后再讨论)
      4. 自动处理高精度

Boolean Logic, Operators, and Value Comparisons

      逻辑与 &&
      逻辑或 ||
      逻辑非 not
 1 ghci> True && False
 2 False
 3 ghci> True || False
 4 True
 5 ghci> not True || False
 6 False
 7 ghci> not True || True
 8 True
 9 ghci> not (True || True)
10 False
      注意not的修饰范围...
      与C语言不同的是: 其他类型不可以自动转化为逻辑类型, 这一点和Java是一样的...
      比较运算符有 > < >= <= 和/= 也就是不等于
 1 ghci> True && False
 2 False
 3 ghci> True || False
 4 True
 5 ghci> not True || False
 6 False
 7 ghci> not True || True
 8 True
 9 ghci> not (True || True)
10 False
     

Operator Precedence and Associativity

      用info可以查看运算符的优先级
 1 ghci>:info (+)
 2 class Num a where
 3   (+) :: a -> a -> a
 4   
 5       -- Defined in `GHC.Num'
 6 infixl 6 +
 7 ghci>:info (*)
 8 class Num a where
 9   
10   (*) :: a -> a -> a
11   
12       -- Defined in `GHC.Num'
13 infixl 7 *
14 ghci>:info (^)
15 (^) :: (Num a, Integral b) => a -> b -> a     -- Defined in `GHC.Real'
16 infixr 8 ^

Lists and Operators on Lists

      和python不同,Haskell的数组里面存放的元素类型必须都是相同的!

 1 ghci>[1,2,3,4]
 2 [1,2,3,4]
 3 ghci>[1,2,3,4,"figo"]
 4 
 5 <interactive>:77:2:
 6     No instance for (Num [Char])
 7       arising from the literal `1'
 8     Possible fix: add an instance declaration for (Num [Char])
 9     In the expression: 1
10     In the expression: [1, 2, 3, 4, .]
11     In an equation for `it': it = [1, 2, 3, .]
12 ghci>["figo","hanfei19910905"]
13 ["figo","hanfei19910905"]
14 
      可以通过枚举来定义数组
 1 ghci>[1,4,8..32]
 2 
 3 <interactive>:84:7: parse error on input `..'
 4 ghci>[1,2..10]
 5 [1,2,3,4,5,6,7,8,9,10]
 6 ghci>[1,4..10]
 7 [1,4,7,10]
 8 ghci>[1,4..9]
 9 [1,4,7]
10 ghci>[10,9..1]
11 [10,9,8,7,6,5,4,3,2,1]
      合并两个数组
1 ghci>[[1,1],[2,2]] ++ [[1,1,1]]
2 [[1,1],[2,2],[1,1,1]]
3 ghci>["figo"] ++ ["string"]
4 ["figo","string"]
      字符串,也就是字符数组
1 ghci>'a':"bc"
2 "abc"
3 ghci>['f','i','g','0']
4 "fig0"
      
posted on 2012-05-15 14:28 西月弦 阅读(1662) 评论(4)  编辑 收藏 引用 所属分类: 读书笔记(Haskell)

FeedBack:
# re: Real World Haskell 读书笔记(一) Getting Started
2012-05-15 16:50 | Ladace
这种确实比较好~  回复  更多评论
  
# re: Real World Haskell 读书笔记(一) Getting Started
2012-05-16 09:00 | gin
干什么用的。。嘿嘿。。我习惯python了。。  回复  更多评论
  
# re: Real World Haskell 读书笔记(一) Getting Started
2012-05-16 09:13 | 西月弦
函数式编程@gin
  回复  更多评论
  
# re: Real World Haskell 读书笔记(一) Getting Started
2012-05-16 17:03 | gin
搜噶,不错,嘿嘿,那好好学@西月弦
  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理