際際滷

際際滷Share a Scribd company logo
Haskell
Study
4. syntax in functions
Pattern matching
 襷れ広 襯 襷     蟲ロ 螳ロ 蟲覓語.  襷れ広 企 一危郁
螳語  伎 覈螻, 一危郁   蠏 一危郁 企 伎 襷豢 覿企   讌
誤 螻殊 蟇一工. Haskell  一危一 伎 磯 襦 るジ  螳 覲語牡襯
螳讌  給.
f :: (Integral a, Num b) => a -> b
f 0 = 0
f 1 = 1
f n = f (n-1) + f (n-2)
Prelude> f 10
55
 螳  襷れ広 伎 朱慨豺 襯 蟲ロ 所 讌蟯朱 蟲  給.
Pattern matching
 襷れ広 覿 谿襦襦 企れる 蟆. 讀, 伎 磯ジ  蟲 螳 襦
るゴ覃 蟆郁骸  殊.   襯 覺.
f :: (Integral a) => a -> String
f n = "must run this function!"
f 0 = "its 0"
f2 :: (Integral a) => a -> String
f2 0 = "its 0"
f2 n = "must run this function!"
   伎 磯ジ  語 襷 覦蠖 訖伎襷 蟆郁骸螳  殊.
Pattern matching
*Main> f 0
must run this function!
*Main> f 1
must run this function!
*Main> f2 0
its 0
*Main> f2 1
must run this function!
 螳  f 蟆曙 f n  螳 襾殊 企伎覃伎, 覈 語  n企朱 伎
焔渚覩襦  f 0 願讌 螳讌 覈詩螻 螳 ろ. 蠏碁  襷れ広   覈(
轟) 伎  螻, 覲企 朱 伎 れ 蟆 燕 蟆 譬給.
Pattern matching
 襷れ広 ろ  給.  蟆曙 瑚 覦.
foo :: Int -> Int
foo 0 = 0
foo 1 = 1
foo 2 = 2
*Main> f 0
0
*Main> f 4
*** Exception: test.hs:(2,1)-(4,7): Non-exhaustive patterns in
function f
 襷れ広   覈 譬襯 伎 襷れ広  蟆 蟆曙 貊伎 .
Pattern matching
   襷れ広 螳ロ.  螳 覯″ 螳  襯 螳企慨譯.
addVector :: (Num a) => (a,a) -> (a,a) -> (a,a)
addVector a b = (fst a + fst b, snd a + snd b)
覓朱    讌襷,  貊螳 譬  蟾.
addVector :: (Num a) => (a,a) -> (a,a) -> (a,a)
addVector (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)
企  襷れ広 伎伎 螳 3螳 (碁Μ) 轟  襯 蟶朱伎る 襯 燕
 給.
Pattern matching
first :: (a,b,c) -> a
first (x, _, _) = x
second :: (a,b,c) -> b
second (_, y, _) = y
third :: (a,b,c) -> c
third (_, _, z) = z
 襷れ広 企 豺 る 螳 覓企 蟯  蟆曙(覘螳  蟯 蟆曙) _
蠍壱碁ゼ 伎.
Pattern matching
 襷れ広 list comprehension 伎  給.
pairToNum :: (Num a) => [(a,a)] -> [a]
pairToNum xs = [a+b | (a,b) <- xs]
*Main> pairToNum [(3,4), (1,2), (5,7), (6,3)]
[7,3,12,9]
Pattern matching
螻 觜訣蟆 襴ろ語  襷れ広 伎  給. 觜 襴ろ([]), cons 一一(:)
煙 伎伎 伎 企 襴ろ瑚 螳語  襯   給.  襷れ広 伎伎
 覯 head 襯 蟲企.
head' :: [a] -> a
head' [] = error "Can't call head on empty list."
head' (x:_) = x
error  壱 襯 覦る . 覈視 貅伎れ  語  螳牛
覲企ゼ 蠍  襦蠏碁 譯曙  .
Pattern matching
るジ 覈 螳讌   覯 危エ覺.
length' :: (Num b) => [a] -> b
length' [] = 0
length' (_:xs) = 1 + length' xs
sum' :: (Num a) => [a] -> a
sum' [] = 0
sum' (x:xs) = x + sum' xs
Pattern matching
 覦 企 一危 豌願 覈  蟆曙 @襯 伎  給.
headFirst :: (Show a) => [a] -> String
headFirst [] = "empty string"
headFirst all@(x:_) = show all ++ "'s first element is " ++ show x
*Main> headFirst [1,2,3,4]
"[1,2,3,4]'s first element is 1"
*Main> headFirst "abcd"
""abcd"'s first element is 'a'"
一 覓語
	yesNo
Bool 螳 襯 語襦 覦 蠏 螳 True覃 "Yes", False覃 "No"襯 襴危 襯 襷れ 覺.
襯 れ yesNo (True || False)  "Yes"襯 襴危伎 .
	trueman
Bool 螳 襴ろ碁ゼ 語襦 覦 True  螳襯 襴危 襯 襷れ 覺. 襯 れ
trueman [True,True,False,False,True] 3 襴危伎 .
	listToPairs
list襯 語襦 覦 螳 襯  螳 襦 讌 讌 伎 襴ろ碁ゼ 襴危 襯 襷れ
覺.  螳螳  螳 蟆曙 襷讌襷  覯襴暑. 襯 れ listToPairs [1,2,3,4,5] 
[(1,2), (3,4)]襯 襴危伎狩.
Guard
 襷れ広 一危郁 轟 伎 襷譟燕 讌襯  覦覯 覦覃 螳 一危郁 轟 譟郁唄
襷譟燕 讌襯  覦覯.  螳 if ~ else if螳 企  蟲譟一 觜訣 手
螳覃 . 一 襯 覺.
bmiTell :: (Floating a) => a -> String
bmiTell bmi
	 | bmi <= 18.5 = "You are underweight."
	 | bmi <= 25.0 = "you are normal."
	 | bmi <= 30.0 = "you are fat."
	 | otherwise = "you are very fat."
螳 語 れ 危(|) 朱Μ  伎 . 覿 襦 語螳
譟郁唄 襷譟燕讌 誤 れ 襷譟燕る 蟇郁鍵 企麹 貊襯 ろ.
Guard
螳 覲危   螳  譴 螳 螳 譟郁唄螻 蟇郁鍵 企麹 貊襯 危.
譟郁唄  譴    碁危(indent) 譟一 . 螳 危 豺螳 襦
碁危瑚 るゴる 貉危 螳 覦.
覲危 螳 襷 襷讌襷 otherwise. otherwise  True襦  朱, るジ
螳螳 豌襴讌 覈詩 譟郁唄 豌襴蠍  譟伎.
願骸 螳襯 螳  蟆曙, 螳 襷譟燕 譟郁唄  蟆曙  れ 伎朱 願.
れ 伎 譟伎讌 る 襷谿螳讌襦 瑚 覦. 蠏碁Μ螻 螳襯    譴 覈
貊襯   蟯讌襷, 螳煙 伎蠍 覓語 覲襦 豢豌襷 覦  蟆 螳れ.
max' :: (Ord a) => a -> a -> a
max' a b | a > b = a | otherwise = b
where
where  企  企 覦覲旧朱 一企   煙  .  bmiTell
襯 where襯 伎 企慨蟆給.
bmiTell :: (Floating a) => a -> a -> String
bmiTell weight height
	 | bmi <= 18.5 = "You are underweight."
	 | bmi <= 25.0 = "You are normal."
	 | bmi <= 30.0 = "You are fat."
	 | otherwise = "You are very fat."
	 where bmi = weight / height ^ 2
れ 覈碁願襯 語襦 覦 bmi讌襯 螻壱伎 蟆 螻,   bmi 螻 螻旧
覦覲旧朱 一願鍵 覓語 where  伎給.
where
where 貊 螳煙 譴 訖襷  螳 螻一  覯襷 螻壱企 蟆 襷れ伎朱
ろ  レ蟾讌 螳語給. where  企 伎 企  伎襷  螳ロ.
 where   襷れ広 伎  給.
bmiTell :: (Floating a) => a -> a -> String
bmiTell weight height
	 | bmi <= skinny = You are underweight.
	 | bmi <= normal = You are normal.
	 | bmi <= fat = You are fat.
	 | otherwise = You are very fat.
	 where bmi = weight / height ^ 2
	 (skinny, normal, pat) = (18.5, 25.0, 30.0)
where
where  企 襯 誤  朱, where  企 where 譴豌伎  
螻, 襯 襷   覓碁( 襷れ広, 螳 ) 覈  螳ロ.
calcFibos :: (Integral a) => [a] -> a
calcFibos xs = [fibo x | x <- xs]
	 where fibo 0 = 0
	 fibo 1 = 1
	 fibo n = fibo (n-1) + fibo (n-2)
let in
let in  where 螻 觜訣  . let (bindings) in (expression) 襯 螻
朱, where れ binding る 覦覃 let binding  る 谿伎 給.
cylinder :: (Num a) => a -> a -> a
cylinder r h =
	 let sideArea = 2 * pi * r * h
	 topArea = pi * r^2
	 in sideArea + 2 * topArea
let in  let  覦碁 螳 in れ   給.  where 蟲覓語
蟲譟一 覦覃 let in  if覓語 貊  企讌 煙ロ  給. 讀, let in 
蠏 豌企 螳覃 蟆郁骸 螳 譟伎伎朱 .
let in
 譴  螳 覦碁 螻 矩る 碁語襦(;) 伎.
Prelude> let a = 5 in a * a * a
125
Prelude> let squre x = x * x in (squre 5, squre 6)
(25, 36)
Prelude> let a = 100; b = 200; c = 300; in a + b + c
600
list comprehension let   給.   let 覦碁 襷 .
Prelude> [f x|x<-[0..10],let f 0=0;f 1=1;f n=f (n-1)+f (n-2)]
[0,1,1,2,3,5,8,13,21,34,55]
case expression
case  覈麹 語伎 switch - case 蟲覓瑚骸 觜訣 螳企,  螳ロ 蠍磯レ 螳螻
給. case   願鍵 覓語 螳 螳願, 貊 譴螳   給.
蟲覓語  螳 蟲譟磯ゼ 螳螻 給.
case expression of pattern -> result
pattern -> result
pattern -> result
...
 語  襷れ広螻 蟲ロ . 轟  伎 磯ジ 蟆郁骸 螳 case 
牛   給. る   襷れ広螻朱 るゴ蟆 case  願鍵 覓語
企讌 螳ロ.
case expression
head :: [a] -> a
head xs = case xs of [] -> error "Cant call head on empty list."
(x:_) -> x
descList :: [a] -> String
descList xs = "this List is " ++ case xs of [] -> "Empty List"
[x]-> "Singleton List"
xs -> "Long List"
一 覓語
	maximum
企 襴ろ瑚 譯殊伎  , 襴ろ語 譴 螳  襯 襴危 襯 蟲 覺. 觜
襴ろ語 蟆曙 error 襯 伎 壱 襯 殊狩り 襷れ 覲伎語.
	take
 襴ろ瑚 譯殊伎 , 襴ろ語 豌覿 企 襷殊 襯 殊 襴危 襯
襷れ 覺. , 螳 0危 蟆曙 觜 襴ろ([])襯 襴危伎 .
	zip
 螳 襴ろ瑚 譯殊伎   襴ろ語  螳螳 襦 讌讌 襴ろ碁ゼ 襴危 襯
襷れ 覺.
  覈 譴 譟伎 . 企 譟郁 るゴ蟆 燕 .
一 覓語
	quickSort (* hard)
企 襴ろ瑚 譯殊伎  , 蠏 襴ろ碁ゼ quickSort襯 伎  襯 蟲企.
豌 蠍 麹 企れ 覓語 豌豌 螳 れ 螻覩狩企.

More Related Content

What's hot (20)

Haskell study 13
Haskell study 13Haskell study 13
Haskell study 13
Nam Hyeonuk
Haskell study 14
Haskell study 14Haskell study 14
Haskell study 14
Nam Hyeonuk
Selection sort
Selection sortSelection sort
Selection sort
Abdelrahman Saleh
Ad hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsAd hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and Cats
Philip Schwarz
Dictionary
DictionaryDictionary
Dictionary
Pooja B S
Vector
VectorVector
Vector
Joyjit Choudhury
Sorting
SortingSorting
Sorting
Ghaffar Khan
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
faradjpour
Domain Modeling in a Functional World
Domain Modeling in a Functional WorldDomain Modeling in a Functional World
Domain Modeling in a Functional World
Debasish Ghosh
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Jorge V叩squez
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
Philip Schwarz
Point free or die - tacit programming in Haskell and beyond
Point free or die - tacit programming in Haskell and beyondPoint free or die - tacit programming in Haskell and beyond
Point free or die - tacit programming in Haskell and beyond
Philip Schwarz
LMAX Disruptor - High Performance Inter-Thread Messaging Library
LMAX Disruptor - High Performance Inter-Thread Messaging LibraryLMAX Disruptor - High Performance Inter-Thread Messaging Library
LMAX Disruptor - High Performance Inter-Thread Messaging Library
Sebastian Andrasoni
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
District Administration
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
KALAISELVI P
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
Dipayan Sarkar
Operators php
Operators phpOperators php
Operators php
Chandni Pm
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Samuel Fortier-Galarneau
Quick sort
Quick sortQuick sort
Quick sort
Abdelrahman Saleh
Monad as functor with pair of natural transformations
Monad as functor with pair of natural transformationsMonad as functor with pair of natural transformations
Monad as functor with pair of natural transformations
Philip Schwarz
Haskell study 13
Haskell study 13Haskell study 13
Haskell study 13
Nam Hyeonuk
Haskell study 14
Haskell study 14Haskell study 14
Haskell study 14
Nam Hyeonuk
Ad hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and CatsAd hoc Polymorphism using Type Classes and Cats
Ad hoc Polymorphism using Type Classes and Cats
Philip Schwarz
Dictionary
DictionaryDictionary
Dictionary
Pooja B S
Functional programming with haskell
Functional programming with haskellFunctional programming with haskell
Functional programming with haskell
faradjpour
Domain Modeling in a Functional World
Domain Modeling in a Functional WorldDomain Modeling in a Functional World
Domain Modeling in a Functional World
Debasish Ghosh
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Jorge V叩squez
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
Philip Schwarz
Point free or die - tacit programming in Haskell and beyond
Point free or die - tacit programming in Haskell and beyondPoint free or die - tacit programming in Haskell and beyond
Point free or die - tacit programming in Haskell and beyond
Philip Schwarz
LMAX Disruptor - High Performance Inter-Thread Messaging Library
LMAX Disruptor - High Performance Inter-Thread Messaging LibraryLMAX Disruptor - High Performance Inter-Thread Messaging Library
LMAX Disruptor - High Performance Inter-Thread Messaging Library
Sebastian Andrasoni
Presentation on the topic selection sort
Presentation on the topic selection sortPresentation on the topic selection sort
Presentation on the topic selection sort
District Administration
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
KALAISELVI P
Selection Sort - Vipin Ramola
Selection Sort - Vipin RamolaSelection Sort - Vipin Ramola
Selection Sort - Vipin Ramola
Dipayan Sarkar
Operators php
Operators phpOperators php
Operators php
Chandni Pm
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Decorators Explained: A Powerful Tool That Should Be in Your Python Toolbelt.
Samuel Fortier-Galarneau
Monad as functor with pair of natural transformations
Monad as functor with pair of natural transformationsMonad as functor with pair of natural transformations
Monad as functor with pair of natural transformations
Philip Schwarz

Viewers also liked (20)

Haskell study 0
Haskell study 0Haskell study 0
Haskell study 0
Nam Hyeonuk
[2010 ChangeON] 語 3 - 企碁
[2010 ChangeON] 語 3 - 企碁[2010 ChangeON] 語 3 - 企碁
[2010 ChangeON] 語 3 - 企碁
daumfoundation
Marketer's writing ′語 creative planner
Marketer's writing ′語 creative plannerMarketer's writing ′語 creative planner
Marketer's writing ′語 creative planner
websmedia
3螻 覓碁
3螻 覓碁 3螻 覓碁
3螻 覓碁
Altai2013
[朱語磯覯]豺朱 覦襯 伎
[朱語磯覯]豺朱 覦襯 伎[朱語磯覯]豺朱 覦襯 伎
[朱語磯覯]豺朱 覦襯 伎
一(Editage Korea)
覓語 レ れ 覦覯襦
覓語 レ  れ 覦覯襦覓語 レ  れ 覦覯襦
覓語 レ れ 覦覯襦
讌 豕
GPG 3.8 殊 朱Μ
GPG 3.8 殊 朱ΜGPG 3.8 殊 朱Μ
GPG 3.8 殊 朱Μ
一 蟾
(07.03) 蠍壱レ覲4_伎, る觜覯 (蟾)
(07.03) 蠍壱レ覲4_伎, る觜覯 (蟾)(07.03) 蠍壱レ覲4_伎, る觜覯 (蟾)
(07.03) 蠍壱レ覲4_伎, る觜覯 (蟾)
EducationWebs
07 襦 伎覿
07 襦 伎覿07 襦 伎覿
07 襦 伎覿
humana12
E3 伎手鍵 蟲
E3 伎手鍵 蟲E3 伎手鍵 蟲
E3 伎手鍵 蟲
20110306喝悌梶估估梶悌δメ戟估=悌…=戟蔭估梶
20110306喝悌梶估估梶悌δメ戟估=悌…=戟蔭估梶20110306喝悌梶估估梶悌δメ戟估=悌…=戟蔭估梶
20110306喝悌梶估估梶悌δメ戟估=悌…=戟蔭估梶
Myeongnyun Mission Church
.. 語 蠏碁蟾?
.. 語 蠏碁蟾?.. 語 蠏碁蟾?
.. 語 蠏碁蟾?
Haeyoung Kim
KTH_Detail day_襦覃れ 企伎手鍵_2谿(1)_誤襦語,_蠍
KTH_Detail day_襦覃れ  企伎手鍵_2谿(1)_誤襦語,_蠍KTH_Detail day_襦覃れ  企伎手鍵_2谿(1)_誤襦語,_蠍
KTH_Detail day_襦覃れ 企伎手鍵_2谿(1)_誤襦語,_蠍
KTH, 貅危壱危
伎 蠏碁Μ螻
伎 蠏碁Μ螻 伎 蠏碁Μ螻
伎 蠏碁Μ螻
E4
E4 E4
E4
覈. 朱Μ 螻覦覯 れ
覈. 朱Μ 螻覦覯 れ覈. 朱Μ 螻覦覯 れ
覈. 朱Μ 螻覦覯 れ
Woongseok Yun
炎概 れ 願啓 10覿 蠎 豈蠍磯 13螳讌 ppt
炎概 れ 願啓 10覿  蠎 豈蠍磯 13螳讌 ppt炎概 れ 願啓 10覿  蠎 豈蠍磯 13螳讌 ppt
炎概 れ 願啓 10覿 蠎 豈蠍磯 13螳讌 ppt
讌 覦
Haskell study 0
Haskell study 0Haskell study 0
Haskell study 0
Nam Hyeonuk
[2010 ChangeON] 語 3 - 企碁
[2010 ChangeON] 語 3 - 企碁[2010 ChangeON] 語 3 - 企碁
[2010 ChangeON] 語 3 - 企碁
daumfoundation
Marketer's writing ′語 creative planner
Marketer's writing ′語 creative plannerMarketer's writing ′語 creative planner
Marketer's writing ′語 creative planner
websmedia
3螻 覓碁
3螻 覓碁 3螻 覓碁
3螻 覓碁
Altai2013
[朱語磯覯]豺朱 覦襯 伎
[朱語磯覯]豺朱 覦襯 伎[朱語磯覯]豺朱 覦襯 伎
[朱語磯覯]豺朱 覦襯 伎
一(Editage Korea)
覓語 レ れ 覦覯襦
覓語 レ  れ 覦覯襦覓語 レ  れ 覦覯襦
覓語 レ れ 覦覯襦
讌 豕
GPG 3.8 殊 朱Μ
GPG 3.8 殊 朱ΜGPG 3.8 殊 朱Μ
GPG 3.8 殊 朱Μ
一 蟾
(07.03) 蠍壱レ覲4_伎, る觜覯 (蟾)
(07.03) 蠍壱レ覲4_伎, る觜覯 (蟾)(07.03) 蠍壱レ覲4_伎, る觜覯 (蟾)
(07.03) 蠍壱レ覲4_伎, る觜覯 (蟾)
EducationWebs
07 襦 伎覿
07 襦 伎覿07 襦 伎覿
07 襦 伎覿
humana12
E3 伎手鍵 蟲
E3 伎手鍵 蟲E3 伎手鍵 蟲
E3 伎手鍵 蟲
20110306喝悌梶估估梶悌δメ戟估=悌…=戟蔭估梶
20110306喝悌梶估估梶悌δメ戟估=悌…=戟蔭估梶20110306喝悌梶估估梶悌δメ戟估=悌…=戟蔭估梶
20110306喝悌梶估估梶悌δメ戟估=悌…=戟蔭估梶
Myeongnyun Mission Church
.. 語 蠏碁蟾?
.. 語 蠏碁蟾?.. 語 蠏碁蟾?
.. 語 蠏碁蟾?
Haeyoung Kim
KTH_Detail day_襦覃れ 企伎手鍵_2谿(1)_誤襦語,_蠍
KTH_Detail day_襦覃れ  企伎手鍵_2谿(1)_誤襦語,_蠍KTH_Detail day_襦覃れ  企伎手鍵_2谿(1)_誤襦語,_蠍
KTH_Detail day_襦覃れ 企伎手鍵_2谿(1)_誤襦語,_蠍
KTH, 貅危壱危
伎 蠏碁Μ螻
伎 蠏碁Μ螻 伎 蠏碁Μ螻
伎 蠏碁Μ螻
E4
E4 E4
E4
覈. 朱Μ 螻覦覯 れ
覈. 朱Μ 螻覦覯 れ覈. 朱Μ 螻覦覯 れ
覈. 朱Μ 螻覦覯 れ
Woongseok Yun
炎概 れ 願啓 10覿 蠎 豈蠍磯 13螳讌 ppt
炎概 れ 願啓 10覿  蠎 豈蠍磯 13螳讌 ppt炎概 れ 願啓 10覿  蠎 豈蠍磯 13螳讌 ppt
炎概 れ 願啓 10覿 蠎 豈蠍磯 13螳讌 ppt
讌 覦

Similar to Haskell study 4 (20)

伎 蠍磯蓋 覓碁
伎 蠍磯蓋 覓碁伎 蠍磯蓋 覓碁
伎 蠍磯蓋 覓碁
SeongHyun Ahn
伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶
Han Sung Kim
R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁
Terry Cho
れ 襦蠏碁覦 覓
れ 襦蠏碁覦 覓れ 襦蠏碁覦 覓
れ 襦蠏碁覦 覓
Kwang Yul Seo
Project#2襷 Hwp
Project#2襷 HwpProject#2襷 Hwp
Project#2襷 Hwp
Kimjeongmoo
れ梗殊 ろ 殊
れ梗殊 ろ 殊 れ梗殊 ろ 殊
れ梗殊 ろ 殊
Taeoh Kim
R 襦蠏碁覦-レ 一危 譟一
R 襦蠏碁覦-レ 一危 譟一R 襦蠏碁覦-レ 一危 譟一
R 襦蠏碁覦-レ 一危 譟一
Terry Cho
螻 2
 螻 2 螻 2
螻 2
HyeonSeok Choi
0327.web&ruby&rails
0327.web&ruby&rails0327.web&ruby&rails
0327.web&ruby&rails
覩殊 蟾
=求メ+メ釈過п求+求≡= 20160301
=求メ+メ釈過п求+求≡= 20160301=求メ+メ釈過п求+求≡= 20160301
=求メ+メ釈過п求+求≡= 20160301
Yong Joon Moon
覦襦蠏碁襾碁ゼ れ梗
覦襦蠏碁襾碁ゼ  れ梗覦襦蠏碁襾碁ゼ  れ梗
覦襦蠏碁襾碁ゼ れ梗
Jong Gook Bae
朱 貊襴
朱  貊襴朱  貊襴
朱 貊襴
Park JoongSoo
Erlang梶 求釈= swap メ
Erlang梶 求釈= swap メErlang梶 求釈= swap メ
Erlang梶 求釈= swap メ
Jaejin Yun
RHive tutorial 4: RHive 過求メ 4 - UDF, UDTF, UDAF
RHive tutorial 4: RHive 過求メ 4 - UDF, UDTF, UDAF RHive tutorial 4: RHive 過求メ 4 - UDF, UDTF, UDAF
RHive tutorial 4: RHive 過求メ 4 - UDF, UDTF, UDAF
Aiden Seonghak Hong
Es2015 Simple Overview
Es2015 Simple OverviewEs2015 Simple Overview
Es2015 Simple Overview
Kim Hunmin
れ 焔 危危蠍
れ 焔 危危蠍れ 焔 危危蠍
れ 焔 危危蠍
Yongha Yoo
06
06 06
06
=求メメ釈 20160130
=求メメ釈 20160130=求メメ釈 20160130
=求メメ釈 20160130
Yong Joon Moon
伎 蠍磯蓋 覓碁
伎 蠍磯蓋 覓碁伎 蠍磯蓋 覓碁
伎 蠍磯蓋 覓碁
SeongHyun Ahn
伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶伎 ろ磯 2譯殊姶
伎 ろ磯 2譯殊姶
Han Sung Kim
R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁R 襦蠏碁覦 蠍磯蓋 覓碁
R 襦蠏碁覦 蠍磯蓋 覓碁
Terry Cho
れ 襦蠏碁覦 覓
れ 襦蠏碁覦 覓れ 襦蠏碁覦 覓
れ 襦蠏碁覦 覓
Kwang Yul Seo
Project#2襷 Hwp
Project#2襷 HwpProject#2襷 Hwp
Project#2襷 Hwp
Kimjeongmoo
れ梗殊 ろ 殊
れ梗殊 ろ 殊 れ梗殊 ろ 殊
れ梗殊 ろ 殊
Taeoh Kim
R 襦蠏碁覦-レ 一危 譟一
R 襦蠏碁覦-レ 一危 譟一R 襦蠏碁覦-レ 一危 譟一
R 襦蠏碁覦-レ 一危 譟一
Terry Cho
0327.web&ruby&rails
0327.web&ruby&rails0327.web&ruby&rails
0327.web&ruby&rails
覩殊 蟾
=求メ+メ釈過п求+求≡= 20160301
=求メ+メ釈過п求+求≡= 20160301=求メ+メ釈過п求+求≡= 20160301
=求メ+メ釈過п求+求≡= 20160301
Yong Joon Moon
覦襦蠏碁襾碁ゼ れ梗
覦襦蠏碁襾碁ゼ  れ梗覦襦蠏碁襾碁ゼ  れ梗
覦襦蠏碁襾碁ゼ れ梗
Jong Gook Bae
Erlang梶 求釈= swap メ
Erlang梶 求釈= swap メErlang梶 求釈= swap メ
Erlang梶 求釈= swap メ
Jaejin Yun
RHive tutorial 4: RHive 過求メ 4 - UDF, UDTF, UDAF
RHive tutorial 4: RHive 過求メ 4 - UDF, UDTF, UDAF RHive tutorial 4: RHive 過求メ 4 - UDF, UDTF, UDAF
RHive tutorial 4: RHive 過求メ 4 - UDF, UDTF, UDAF
Aiden Seonghak Hong
Es2015 Simple Overview
Es2015 Simple OverviewEs2015 Simple Overview
Es2015 Simple Overview
Kim Hunmin
れ 焔 危危蠍
れ 焔 危危蠍れ 焔 危危蠍
れ 焔 危危蠍
Yongha Yoo
06
06 06
06
=求メメ釈 20160130
=求メメ釈 20160130=求メメ釈 20160130
=求メメ釈 20160130
Yong Joon Moon

More from Nam Hyeonuk (17)

Next 蟆 れ 襦 殊企
Next 蟆 れ 襦 殊企Next 蟆 れ 襦 殊企
Next 蟆 れ 襦 殊企
Nam Hyeonuk
Haskell study 15
Haskell study 15Haskell study 15
Haskell study 15
Nam Hyeonuk
Haskell study 11
Haskell study 11Haskell study 11
Haskell study 11
Nam Hyeonuk
Haskell study 10
Haskell study 10Haskell study 10
Haskell study 10
Nam Hyeonuk
Haskell study 7
Haskell study 7Haskell study 7
Haskell study 7
Nam Hyeonuk
Multi thread
Multi threadMulti thread
Multi thread
Nam Hyeonuk
Memory & object pooling
Memory & object poolingMemory & object pooling
Memory & object pooling
Nam Hyeonuk
Database
DatabaseDatabase
Database
Nam Hyeonuk
Exception&log
Exception&logException&log
Exception&log
Nam Hyeonuk
Iocp advanced
Iocp advancedIocp advanced
Iocp advanced
Nam Hyeonuk
Iocp 蠍磯蓋 蟲譟 危
Iocp 蠍磯蓋 蟲譟 危Iocp 蠍磯蓋 蟲譟 危
Iocp 蠍磯蓋 蟲譟 危
Nam Hyeonuk
Tcp ip & io model
Tcp ip & io modelTcp ip & io model
Tcp ip & io model
Nam Hyeonuk
Effective c++ chapter 1,2
Effective c++ chapter 1,2 Effective c++ chapter 1,2
Effective c++ chapter 1,2
Nam Hyeonuk
蟲覓瑚骸 覩碁( 覩碁蟾讌)
蟲覓瑚骸 覩碁( 覩碁蟾讌)蟲覓瑚骸 覩碁( 覩碁蟾讌)
蟲覓瑚骸 覩碁( 覩碁蟾讌)
Nam Hyeonuk
Gpg 1.1
Gpg 1.1Gpg 1.1
Gpg 1.1
Nam Hyeonuk
Stl vector, list, map
Stl vector, list, mapStl vector, list, map
Stl vector, list, map
Nam Hyeonuk
Age Of Empires II : Age Of Kings Postmotem
Age Of Empires II : Age Of Kings PostmotemAge Of Empires II : Age Of Kings Postmotem
Age Of Empires II : Age Of Kings Postmotem
Nam Hyeonuk
Next 蟆 れ 襦 殊企
Next 蟆 れ 襦 殊企Next 蟆 れ 襦 殊企
Next 蟆 れ 襦 殊企
Nam Hyeonuk
Haskell study 15
Haskell study 15Haskell study 15
Haskell study 15
Nam Hyeonuk
Haskell study 11
Haskell study 11Haskell study 11
Haskell study 11
Nam Hyeonuk
Haskell study 10
Haskell study 10Haskell study 10
Haskell study 10
Nam Hyeonuk
Haskell study 7
Haskell study 7Haskell study 7
Haskell study 7
Nam Hyeonuk
Memory & object pooling
Memory & object poolingMemory & object pooling
Memory & object pooling
Nam Hyeonuk
Exception&log
Exception&logException&log
Exception&log
Nam Hyeonuk
Iocp advanced
Iocp advancedIocp advanced
Iocp advanced
Nam Hyeonuk
Iocp 蠍磯蓋 蟲譟 危
Iocp 蠍磯蓋 蟲譟 危Iocp 蠍磯蓋 蟲譟 危
Iocp 蠍磯蓋 蟲譟 危
Nam Hyeonuk
Tcp ip & io model
Tcp ip & io modelTcp ip & io model
Tcp ip & io model
Nam Hyeonuk
Effective c++ chapter 1,2
Effective c++ chapter 1,2 Effective c++ chapter 1,2
Effective c++ chapter 1,2
Nam Hyeonuk
蟲覓瑚骸 覩碁( 覩碁蟾讌)
蟲覓瑚骸 覩碁( 覩碁蟾讌)蟲覓瑚骸 覩碁( 覩碁蟾讌)
蟲覓瑚骸 覩碁( 覩碁蟾讌)
Nam Hyeonuk
Stl vector, list, map
Stl vector, list, mapStl vector, list, map
Stl vector, list, map
Nam Hyeonuk
Age Of Empires II : Age Of Kings Postmotem
Age Of Empires II : Age Of Kings PostmotemAge Of Empires II : Age Of Kings Postmotem
Age Of Empires II : Age Of Kings Postmotem
Nam Hyeonuk

Haskell study 4

  • 2. Pattern matching 襷れ広 襯 襷 蟲ロ 螳ロ 蟲覓語. 襷れ広 企 一危郁 螳語 伎 覈螻, 一危郁 蠏 一危郁 企 伎 襷豢 覿企 讌 誤 螻殊 蟇一工. Haskell 一危一 伎 磯 襦 るジ 螳 覲語牡襯 螳讌 給. f :: (Integral a, Num b) => a -> b f 0 = 0 f 1 = 1 f n = f (n-1) + f (n-2) Prelude> f 10 55 螳 襷れ広 伎 朱慨豺 襯 蟲ロ 所 讌蟯朱 蟲 給.
  • 3. Pattern matching 襷れ広 覿 谿襦襦 企れる 蟆. 讀, 伎 磯ジ 蟲 螳 襦 るゴ覃 蟆郁骸 殊. 襯 覺. f :: (Integral a) => a -> String f n = "must run this function!" f 0 = "its 0" f2 :: (Integral a) => a -> String f2 0 = "its 0" f2 n = "must run this function!" 伎 磯ジ 語 襷 覦蠖 訖伎襷 蟆郁骸螳 殊.
  • 4. Pattern matching *Main> f 0 must run this function! *Main> f 1 must run this function! *Main> f2 0 its 0 *Main> f2 1 must run this function! 螳 f 蟆曙 f n 螳 襾殊 企伎覃伎, 覈 語 n企朱 伎 焔渚覩襦 f 0 願讌 螳讌 覈詩螻 螳 ろ. 蠏碁 襷れ広 覈( 轟) 伎 螻, 覲企 朱 伎 れ 蟆 燕 蟆 譬給.
  • 5. Pattern matching 襷れ広 ろ 給. 蟆曙 瑚 覦. foo :: Int -> Int foo 0 = 0 foo 1 = 1 foo 2 = 2 *Main> f 0 0 *Main> f 4 *** Exception: test.hs:(2,1)-(4,7): Non-exhaustive patterns in function f 襷れ広 覈 譬襯 伎 襷れ広 蟆 蟆曙 貊伎 .
  • 6. Pattern matching 襷れ広 螳ロ. 螳 覯″ 螳 襯 螳企慨譯. addVector :: (Num a) => (a,a) -> (a,a) -> (a,a) addVector a b = (fst a + fst b, snd a + snd b) 覓朱 讌襷, 貊螳 譬 蟾. addVector :: (Num a) => (a,a) -> (a,a) -> (a,a) addVector (x1, y1) (x2, y2) = (x1 + x2, y1 + y2) 企 襷れ広 伎伎 螳 3螳 (碁Μ) 轟 襯 蟶朱伎る 襯 燕 給.
  • 7. Pattern matching first :: (a,b,c) -> a first (x, _, _) = x second :: (a,b,c) -> b second (_, y, _) = y third :: (a,b,c) -> c third (_, _, z) = z 襷れ広 企 豺 る 螳 覓企 蟯 蟆曙(覘螳 蟯 蟆曙) _ 蠍壱碁ゼ 伎.
  • 8. Pattern matching 襷れ広 list comprehension 伎 給. pairToNum :: (Num a) => [(a,a)] -> [a] pairToNum xs = [a+b | (a,b) <- xs] *Main> pairToNum [(3,4), (1,2), (5,7), (6,3)] [7,3,12,9]
  • 9. Pattern matching 螻 觜訣蟆 襴ろ語 襷れ広 伎 給. 觜 襴ろ([]), cons 一一(:) 煙 伎伎 伎 企 襴ろ瑚 螳語 襯 給. 襷れ広 伎伎 覯 head 襯 蟲企. head' :: [a] -> a head' [] = error "Can't call head on empty list." head' (x:_) = x error 壱 襯 覦る . 覈視 貅伎れ 語 螳牛 覲企ゼ 蠍 襦蠏碁 譯曙 .
  • 10. Pattern matching るジ 覈 螳讌 覯 危エ覺. length' :: (Num b) => [a] -> b length' [] = 0 length' (_:xs) = 1 + length' xs sum' :: (Num a) => [a] -> a sum' [] = 0 sum' (x:xs) = x + sum' xs
  • 11. Pattern matching 覦 企 一危 豌願 覈 蟆曙 @襯 伎 給. headFirst :: (Show a) => [a] -> String headFirst [] = "empty string" headFirst all@(x:_) = show all ++ "'s first element is " ++ show x *Main> headFirst [1,2,3,4] "[1,2,3,4]'s first element is 1" *Main> headFirst "abcd" ""abcd"'s first element is 'a'"
  • 12. 一 覓語 yesNo Bool 螳 襯 語襦 覦 蠏 螳 True覃 "Yes", False覃 "No"襯 襴危 襯 襷れ 覺. 襯 れ yesNo (True || False) "Yes"襯 襴危伎 . trueman Bool 螳 襴ろ碁ゼ 語襦 覦 True 螳襯 襴危 襯 襷れ 覺. 襯 れ trueman [True,True,False,False,True] 3 襴危伎 . listToPairs list襯 語襦 覦 螳 襯 螳 襦 讌 讌 伎 襴ろ碁ゼ 襴危 襯 襷れ 覺. 螳螳 螳 蟆曙 襷讌襷 覯襴暑. 襯 れ listToPairs [1,2,3,4,5] [(1,2), (3,4)]襯 襴危伎狩.
  • 13. Guard 襷れ広 一危郁 轟 伎 襷譟燕 讌襯 覦覯 覦覃 螳 一危郁 轟 譟郁唄 襷譟燕 讌襯 覦覯. 螳 if ~ else if螳 企 蟲譟一 觜訣 手 螳覃 . 一 襯 覺. bmiTell :: (Floating a) => a -> String bmiTell bmi | bmi <= 18.5 = "You are underweight." | bmi <= 25.0 = "you are normal." | bmi <= 30.0 = "you are fat." | otherwise = "you are very fat." 螳 語 れ 危(|) 朱Μ 伎 . 覿 襦 語螳 譟郁唄 襷譟燕讌 誤 れ 襷譟燕る 蟇郁鍵 企麹 貊襯 ろ.
  • 14. Guard 螳 覲危 螳 譴 螳 螳 譟郁唄螻 蟇郁鍵 企麹 貊襯 危. 譟郁唄 譴 碁危(indent) 譟一 . 螳 危 豺螳 襦 碁危瑚 るゴる 貉危 螳 覦. 覲危 螳 襷 襷讌襷 otherwise. otherwise True襦 朱, るジ 螳螳 豌襴讌 覈詩 譟郁唄 豌襴蠍 譟伎. 願骸 螳襯 螳 蟆曙, 螳 襷譟燕 譟郁唄 蟆曙 れ 伎朱 願. れ 伎 譟伎讌 る 襷谿螳讌襦 瑚 覦. 蠏碁Μ螻 螳襯 譴 覈 貊襯 蟯讌襷, 螳煙 伎蠍 覓語 覲襦 豢豌襷 覦 蟆 螳れ. max' :: (Ord a) => a -> a -> a max' a b | a > b = a | otherwise = b
  • 15. where where 企 企 覦覲旧朱 一企 煙 . bmiTell 襯 where襯 伎 企慨蟆給. bmiTell :: (Floating a) => a -> a -> String bmiTell weight height | bmi <= 18.5 = "You are underweight." | bmi <= 25.0 = "You are normal." | bmi <= 30.0 = "You are fat." | otherwise = "You are very fat." where bmi = weight / height ^ 2 れ 覈碁願襯 語襦 覦 bmi讌襯 螻壱伎 蟆 螻, bmi 螻 螻旧 覦覲旧朱 一願鍵 覓語 where 伎給.
  • 16. where where 貊 螳煙 譴 訖襷 螳 螻一 覯襷 螻壱企 蟆 襷れ伎朱 ろ レ蟾讌 螳語給. where 企 伎 企 伎襷 螳ロ. where 襷れ広 伎 給. bmiTell :: (Floating a) => a -> a -> String bmiTell weight height | bmi <= skinny = You are underweight. | bmi <= normal = You are normal. | bmi <= fat = You are fat. | otherwise = You are very fat. where bmi = weight / height ^ 2 (skinny, normal, pat) = (18.5, 25.0, 30.0)
  • 17. where where 企 襯 誤 朱, where 企 where 譴豌伎 螻, 襯 襷 覓碁( 襷れ広, 螳 ) 覈 螳ロ. calcFibos :: (Integral a) => [a] -> a calcFibos xs = [fibo x | x <- xs] where fibo 0 = 0 fibo 1 = 1 fibo n = fibo (n-1) + fibo (n-2)
  • 18. let in let in where 螻 觜訣 . let (bindings) in (expression) 襯 螻 朱, where れ binding る 覦覃 let binding る 谿伎 給. cylinder :: (Num a) => a -> a -> a cylinder r h = let sideArea = 2 * pi * r * h topArea = pi * r^2 in sideArea + 2 * topArea let in let 覦碁 螳 in れ 給. where 蟲覓語 蟲譟一 覦覃 let in if覓語 貊 企讌 煙ロ 給. 讀, let in 蠏 豌企 螳覃 蟆郁骸 螳 譟伎伎朱 .
  • 19. let in 譴 螳 覦碁 螻 矩る 碁語襦(;) 伎. Prelude> let a = 5 in a * a * a 125 Prelude> let squre x = x * x in (squre 5, squre 6) (25, 36) Prelude> let a = 100; b = 200; c = 300; in a + b + c 600 list comprehension let 給. let 覦碁 襷 . Prelude> [f x|x<-[0..10],let f 0=0;f 1=1;f n=f (n-1)+f (n-2)] [0,1,1,2,3,5,8,13,21,34,55]
  • 20. case expression case 覈麹 語伎 switch - case 蟲覓瑚骸 觜訣 螳企, 螳ロ 蠍磯レ 螳螻 給. case 願鍵 覓語 螳 螳願, 貊 譴螳 給. 蟲覓語 螳 蟲譟磯ゼ 螳螻 給. case expression of pattern -> result pattern -> result pattern -> result ... 語 襷れ広螻 蟲ロ . 轟 伎 磯ジ 蟆郁骸 螳 case 牛 給. る 襷れ広螻朱 るゴ蟆 case 願鍵 覓語 企讌 螳ロ.
  • 21. case expression head :: [a] -> a head xs = case xs of [] -> error "Cant call head on empty list." (x:_) -> x descList :: [a] -> String descList xs = "this List is " ++ case xs of [] -> "Empty List" [x]-> "Singleton List" xs -> "Long List"
  • 22. 一 覓語 maximum 企 襴ろ瑚 譯殊伎 , 襴ろ語 譴 螳 襯 襴危 襯 蟲 覺. 觜 襴ろ語 蟆曙 error 襯 伎 壱 襯 殊狩り 襷れ 覲伎語. take 襴ろ瑚 譯殊伎 , 襴ろ語 豌覿 企 襷殊 襯 殊 襴危 襯 襷れ 覺. , 螳 0危 蟆曙 觜 襴ろ([])襯 襴危伎 . zip 螳 襴ろ瑚 譯殊伎 襴ろ語 螳螳 襦 讌讌 襴ろ碁ゼ 襴危 襯 襷れ 覺. 覈 譴 譟伎 . 企 譟郁 るゴ蟆 燕 .
  • 23. 一 覓語 quickSort (* hard) 企 襴ろ瑚 譯殊伎 , 蠏 襴ろ碁ゼ quickSort襯 伎 襯 蟲企. 豌 蠍 麹 企れ 覓語 豌豌 螳 れ 螻覩狩企.