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

Installation

under CentOS/Fedora

# yum install ghc

Version

[[email protected] 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

时间: 2024-11-08 14:44:24

[Learning You a Haskell for Great Goods!] chapter 01 starting out的相关文章

[Learning You a Haskell for Great Goods!] Introduction

Compiler Glasgow Haskell Compiler (GHC) http://hackage.haskell.org/platform/ Command # load scripts ghci> :l hello.hs # run scripts

chapter 01

hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.

Chapter 01:创建和销毁对象

<一>考虑用静态工厂方法代替构造器 下面是Boolean类的一个简单示例: public final class Boolean implements java.io.Serializable, Comparable<Boolean> { public static final Boolean TRUE = new Boolean(true); public static final Boolean FALSE = new Boolean(false); public static

CCJ PRML Study Note - Chapter 1.2 : Probability Theory

Chapter 1.2 : Probability Theory Chapter 1.2 : Probability Theory Christopher M. Bishop, PRML, Chapter 1 Introdcution Chapter 1.2 : Probability Theory 1. Uncertainty 2. Example discussed through this chapter 3. Basic Terminology 3.1 Probability densi

[C3] Andrew Ng - Neural Networks and Deep Learning

About this Course If you want to break into cutting-edge AI, this course will help you do so. Deep learning engineers are highly sought after, and mastering deep learning will give you numerous new career opportunities. Deep learning is also a new "s

学好数学能让程序员的水平更高

I've been working for the past 15 months on repairing my rusty math skills, ever since I read a biography of Johnny von Neumann. I've read a huge stack of math books, and I have an even bigger stack of unread math books. And it's starting to come tog

【转】程序员怎样学数学

I've been working for the past 15 months on repairing my rusty math skills, ever since I read a biography of Johnny von Neumann. I've read a huge stack of math books, and I have an even bigger stack of unread math books. And it's starting to come tog

[转] 程序员怎样学数学

Source:http://article.yeeyan.org/view/pluto/2365 --------------------------------------------------------------------- 读后感: 高中的时候数学成绩还不错,150分的卷子基本能保持在135以上.但是总感觉我的数学思维和数学修养仍然没什么提高.NUAA自招失败的经历让我彻底发现了这一点.大一学了一年的高数,又被繁杂的公式折磨得死去活来. 总感觉真正的数学不应该是这样的.但是真正的数

程序与数学 转自网络

I've been working for the past 15 months on repairing my rusty math skills, ever since I read a biography of Johnny von Neumann. I've read a huge stack of math books, and I have an even bigger stack of unread math books. And it's starting to come tog