갈루아의 반서재


미란 리포바카(Miran Lipovača)의  <가장 쉬운 하스켈 책 Learn You a Haskell for Great Good!>의 내용을 바탕으로 하스켈 학습내용을 기록해봅니다. 먼저 아나콘다 가상환경에서 하스켈을 설치하고 구동하는 부분입니다.


가장 쉬운 하스켈 책
국내도서
저자 : 미란 리포바카(Miran Lipovaca) / 황반석역
출판 : 비제이퍼블릭 2014.02.25
상세보기


1. 아나콘다 가상환경 구축 및 활성화

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
root@localhost:~# conda info --envs
# conda environments:
#
root                  *  /root/anaconda
root@localhost:~# conda create -n envghci python=3.5 anaconda
Fetching package metadata: ....
Solving package specifications: .
Package plan for installation in environment /root/anaconda/envs/envghci:
 
The following packages will be downloaded:
 
    package                    |            build
    ---------------------------|-----------------
    xz-5.2.2                   |                0         644 KB
    python-3.5.2               |                0        17.2 MB
    ------------------------------------------------------------
                                           Total:        17.8 MB
 
The following NEW packages will be INSTALLED:
 
    anaconda:   custom-py35_0
    openssl:    1.0.2h-1
    pip:        8.1.2-py35_0
    python:     3.5.2-0
    readline:   6.2-2
    setuptools: 23.0.0-py35_0
    sqlite:     3.13.0-0
    tk:         8.5.18-0
    wheel:      0.29.0-py35_0
    xz:         5.2.2-0
    zlib:       1.2.8-3
 
Proceed ([y]/n)? y
 
Fetching packages ...
xz-5.2.2-0.tar 100|################################| Time: 0:00:01 409.50 kB/s
python-3.5.2-0 100|################################| Time: 0:00:03   4.50 MB/s
Extracting packages ...
[      COMPLETE      ]|###################################################| 100%
Linking packages ...
[      COMPLETE      ]|###################################################| 100%
#
# To activate this environment, use:
# $ source activate envghci
#
# To deactivate this environment, use:
# $ source deactivate
root@localhost:~# source activate envghci
discarding /root/anaconda/bin from PATH
prepending /root/anaconda/envs/envghci/bin to PATH
(envghci)root@localhost:~# sudo apt-get install haskell-platform


2. 하스켈 패키지 설치

윈도우 https://www.haskell.org/platform/windows.html

리눅스 https://www.haskell.org/platform/linux.html

OSX https://www.haskell.org/platform/mac.html


3. 시작하기

1) 간단한 연산


(envghci)root@localhost:~# ghci
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
Prelude> 2+15
17
Prelude> 49*100
4900
Prelude> 1892-1472
420
Prelude> 5/2
2.5
Prelude> (50*100)-4999
1
Prelude> 50*100-4999
1
cs

음수는 산술식에 사용되는 곳마다 괄호로 감싸는 것이 최선의 방법이다.


2) 불대수 (Boolean algebra)

&& : 논리곱 (불 and)

|| : 논리합 (불 or)

not : 부정연산자

1
2
3
4
5
6
7
8
9
10
11
12
13
Prelude> True && False
False
Prelude> True && True
True
Prelude> False || True
True
Prelude> not False
True
Prelude> not (True && False)
True
 
 
 
cs


3) 비교 연산

== : 두 개의 값(동일한 타입이어야 함)이 서로 같은지 검사

/= : 두 개의 값이 서로 같지 않은지 검사

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Prelude> 5 == 5
True
Prelude> 1 == 0
False
Prelude> 5 /= 5
False
Prelude> 5 /= 4
True
Prelude> "hello" == "hello"
True
Prelude>
Prelude> 5 == "hello"
 
<interactive>:19:1:
    No instance for (Num [Char]) arising from the literal ‘5’
    In the first argument of ‘(==)’, namely ‘5’
    In the expression: 5 == "hello"
    In an equation for ‘it’: it = 5 == "hello"
Prelude> 5 /= "hello"
 
<interactive>:20:1:
    No instance for (Num [Char]) arising from the literal ‘5’
    In the first argument of ‘(/=)’, namely ‘5’
    In the expression: 5 /= "hello"
    In an equation for ‘it’: it = 5 /= "hello"
Prelude>
 
 
cs