码迷,mamicode.com
首页 > 其他好文 > 详细

[Learning You a Haskell for Great Goods!] chapter 01 starting out

时间:2014-09-24 02:36:05      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   strong   for   sp   art   cti   on   

Installation

under CentOS/Fedora

# yum install ghc

 

Version

[root@bogon1 haskell]# ghc -v

Glasgow Haskell Compiler, Version 7.0.4, for Haskell 98, stage 2 booted by GHC version 7.0.4

 

Change prompt

echo :set prompt "ghci> " > ~/.ghci

 

Command

# Boolean

ghci> True && False

False

 

# call function, get successor of 8

ghci> succ 8

9

 

# load scripts

## echo doubleMe x = x + x > baby.hs

 

ghci> :l baby.hs

[1 of 1] Compiling Main             ( baby.hs, interpreted )

Ok, modules loaded: Main.

 

ghci> doubleMe 9

18

 

# Concatenation between list and string

ghci> [1,2,3,4] ++ [9,10,11,12]

[1,2,3,4,9,10,11,12]

 

or

 

ghci > ‘A‘: "Small cat"

"A Small cat"

 

or

 

ghci> 5:[1,2]

[5,1,2]

 

# List operation

 

## get the element that‘s index = 3

ghci> "123456" !! 3

‘4‘

 

## comparing

ghci> [3,2,1] > [2,1,0]
True

## head vs tail, init vs last

ghci> head [5,4,3,2,1]
5

ghci> tail [5,4,3,2,1]
[4,3,2,1]

ghci> last [5,4,3,2,1]
1

ghci> init [5,4,3,2,1]
[5,4,3,2]

## length

ghci> length [5,4,3,2,1]
5

## is null or not

ghci> null [1,2,3]
False
ghci> null []
True

## reverse list

ghci> reverse [5,4,3,2,1]
[1,2,3,4,5]

## take the first N elements

ghci> take 3 [5,4,3,2,1]
[5,4,3]
ghci> take 1 [3,9,3]
[3]
ghci> take 5 [1,2]
[1,2]
ghci> take 0 [6,6,6]
[]

## drop the first N elements

ghci> drop 3 [8,4,2,1,5,6]
[1,5,6]
ghci> drop 0 [1,2,3,4]
[1,2,3,4]
ghci> drop 100 [1,2,3,4]
[]

## element in list or not

ghci> 4 `elem` [1 ,4 ]
True

or

ghci> elem 4 [1 , 4]
True

## range

ghci> [1..4]
[1,2,3,4]

 

## replicate

ghci> replicate 3 10
[10,10,10]

## List Comprehension

ghci> [x * 2 | x <- [1..10] ]
[2,4,6,8,10,12,14,16,18,20]

ghci> [x*2 | x <- [1..10], x *2 >= 12]

[12,14,16,18,20]

 

 

 

 

 

 

Note

doubleSmallNumber‘ is valid as a method name

[Learning You a Haskell for Great Goods!] chapter 01 starting out

标签:io   os   ar   strong   for   sp   art   cti   on   

原文地址:http://www.cnblogs.com/abramsz/p/3989631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!