Skip to content

Haskell 정리 1 Haskell

Type
Prelude> let r = 25
Prelude> 2 * pi * r
<interactive>:1:9:
Couldn't match `Double' against `Integer'
Expected type: Double
Inferred type: Integer
In the second argument of `(*)', namely `r'
In the definition of `it': it = (2 * pi) * r

r의 type 문제로 error가 발생한다.
pi는 Double로 정의 되고 r이 정수형으로 선언되었으므로

Prelude> let r = 25 :: Double
위와같이 명시한다.

Scope
Prelude> let r = 0
Prelude> let area r = pi * r ^ 2
Prelude> area 5
78.53981633974483

위에 r이 0으로 선언되었지만 함수선언의 r은 위의 r과는 다른 Scope를 갖는다.

함수안의 함수

Prelude> let areaRec l w = l * w
Prelude> let areaSq s = areaRec s s
Prelude> areaSq 5
25

함수내에서 또다른 함수를 호출가능하다.
미리 정의된 areaRec에서 areaSq함수를 재정의 할 수있다.


Lists and tuples
List
Prelude> let num = [1,2,3]
리스트는 위와같이 대괄호와 쉼표로이루어진다.
Prelude> let list = [1,"string"]
하지만 같은 형식이 아니면 에러이다.

리스트를 만들수있는방법은 위방법 이외에
Prelude> 9:8:7:num
[9,8,7,1,2,3]
이라고 하면 이미 만들어진 num이라는 리스트에 앞에 추가된다.
따라서 
Prelude> 1:2:[] 라는 리스트로 만들수있다.

list 안의 list
[[1],[2]]

Tuples
리스트와는 다르게 여러타입의 값을 갖을수있다
(1,True,"some")

Prelude> fst (1, True)
1
Prelude> snd (1, True)
True

fst와 snd는 2-tuple에만 사용할수 있다.

Tuples within tuples
((2,3),[2,3])

File
Prelude> :cd c:\Directory
Prelude> :load *.hs     (:l short)
Prelude> :reload *.hs (:r short)

Prelude> let v = 5
Prelude> v = 10
변수는 재정의는 가능하지만 재할당은 불가하다.

조건표현

if

ifFunc x =
if x < 0 
then -1
else if x > 0
then  1
else 0

case
funcCase x =
case x of
0 -> "zero"
1 -> "one"
2 -> "two"
__-> "else"
case는 다음 형식으로도 기술할수있다.
funcCase x =
case x of { 0 -> "zero"; 1 -> "one"; 2 -> "two"; __-> "else"}
매개변수에 대한 함수정의
f 0 = 1
f 1 = 5
f 2 = 2
f _ = -1

square x = x^2

*Main> square (f 1)
25
*Main> square (f 2)
4
*Main> f (square 1)
5
*Main> f (square 2)
-1

위와 같은 식으로 사용이 가능하다.

Let Binding
예를 들기 위해 근의 공식을 이용한다.
roots a b c =
((-b + sqrt(b*b - 4*a*c)) / (2*a),
(-b - sqrt(b*b - 4*a*c)) / (2*a))

위의 식에서 sqrt(b*b - 4*a*c) 식은 중복사용되었다.
따라서 중복문제를 해결하기위해 다음과 같이 기술할 수 있다.

roots a b c =
let disc = sqrt (b*b - 4*a*c)
in ((-b + disc) / (2*a),
(-b - disc) / (2*a))

(2*a) 식도 한번에 쓰려면 

roots a b c =
let disc = sqrt (b*b - 4*a*c)
twice_a = 2*a
in ((-b + disc) / twice_a,
(-b - disc) / twice_a)

Type basics 
Using the interactive :type command
Characters and strings

Prelude> :type 'H' (Short :t)
'H' :: Char

string을 평가
Prelude> :t "Hello World"
"Hello World" :: [Char]

Boolean values
Prelude> :t True
True :: Bool
Prelude> :t False
False :: Bool

Numeric types
Prelude> :t 5
5 :: (Num t) => t

Functional types
not True = False
not False = True

이런식으로 응용 가능하다
valueDoesntExist name = not (valueExists name)

unlines, unwords 예제
Prelude> unlines ["Bacon", "Sausages", "Egg"]
"Bacon\nSausages\nEgg\n"
Prelude> unwords ["Bacon", "Sausages", "Egg"]
"Bacon Sausages Egg"

putStrLn 을 이용한 출력
Prelude> putStrLn (unlines ["Bacon", "Sausages", "Egg"])
Bacon
Sausages
Egg
Prelude> putStrLn (unwords ["Bacon", "Sausages", "Egg"])
Bacon Sausages Egg

chr, ord 예제
chr :: Int -> Char
ord :: Char -> Int
두 function의 정의이다.
Prelude> :module Data.Char (Short :m)
Prelude Data.Char> chr 97
'a'
Prelude Data.Char> chr 98
'b'
Prelude Data.Char> ord 'c'
99

출처 : http://en.wikibooks.org/w/index.php?title=Haskell/Print_version&printa

이 글과 관련된 글
  1. [2010/03/13] 염산맛황산의 미투데이 - 2010년 3월 12일 by 머리엉덩이 (0)
Tag :

Leave Comments

T-NAVI