Haskell 16æ¥ç® ã é¢æ°åæã¨é¨åé©ç¨ ã
Haskell 16æ¥ç®ã
é¢æ°åæ
é¢æ°åæ(function composition)ã¨ã¯ãè¤æ°ã®é¢æ°ãåæãã¦æ°ããé¢æ°ãä½ããã¨ã§ãã
ä¾ãã°ãæååã«å«ã¾ããè¡æ°ãæ°ããé¢æ°ãnumberOfLinesã¯$æ¼ç®åã使ã£ã¦è¨è¿°ããã¨æ¬¡ã®ããã«æ¸ãã¾ãã
numberOfLines :: String -> Int numberOfLines cs = length $ lines cs
ä¸è¨ã®é¢æ°ã¯é¢æ°åæã使ãã¨æ¬¡ã®ããã«ãæ¸ãã¾ãã
numberOfLines :: String -> Int numberOfLines = length . lines
(.)é¢æ°ã¯ãï¼ã¤ã®é¢æ°ãåæããé¢æ°ã§ãã
ãåæãããã¨ã¯ãã2ã¤ã®é¢æ°ãé çªã«é©ç¨ããæ°ããé¢æ°ãä½ããã¨ãããã¨ã§ãä¾ãã°(length . lines)ã¯ããå¼æ°ã«linesé¢æ°ãé©ç¨ãããã®å¤ã«lengthé¢æ°ãé©ç¨ããé¢æ°ãã¨ãªãã¾ãã
é¨åé©ç¨
é¨åé©ç¨ã¨ã¯
Haskellã§ã¯é¢æ°ã«ãã¹ã¦ã®å¼æ°ãåæã«æ¸¡ããªãã¨ãæ§ããªãããã«ãªã£ã¦ãã¾ãã
ä¾ãã°ãå¼æ°ãï¼ã¤ã¨ãé¢æ°ã«1ã¤ã ãå¼æ°ã渡ãã¦ããã¦ãæ®ãã®2ã¤ããã¨ãã渡ããã¨ãã§ããã®ã§ãã
ãã®ããã«ãå¼æ°ãä¸åº¦ã«ãã¹ã¦æ¸¡ãããä¸é¨ã ã渡ãã¦ãããã¨ãé¨åé©ç¨(partical application)ã¨è¨ãã¾ãã
é¨åé©ç¨ã®ä¾ã¨ãã¦ã次ã®addThreeé¢æ°ããã£ãã¨ãã¾ãã
addThree::Int -> Int -> Int -> Int addThree i j k = i + j + k
é¢æ°ã®æå³ã¯èãããå¼æ°ã®æ°ã«ã ã注ç®ãã¾ããaddThreeé¢æ°ã¯3ã¤ã®å¼æ°ãã¨ãé¢æ°ã§ãã
ããã§ãããã¦å¼æ°ã1ã¤ã ã渡ãã¦ã¿ã¾ãã
addThree 5
ãã®ããã«ç¸æããå¼æ°ã足ããªãããªã©ã¨ããã¨ã©ã¼ã«ã¯ãªãã¾ãããããã¯Haskellã§ã¯æ£ããå¼ãªã®ã§ãã
(addThree 5)ã®å¤ã¯ããaddThreeé¢æ°ã®ç¬¬1å¼æ°ã«5ã渡ãã¦å¼æ°ã1ã¤æ¸ãããé¢æ°ãã¨ãªãã以ä¸ã®ç¡åé¢æ°ã¨åãã§ãã
\j k -> 5 + j + k
(addThree 5)ã®å¤ã§ããé¢æ°ã«ãããã«å¼æ°ã渡ãã¦ã¿ã¾ãã
(addThree 5) 6
((addThree 5) 6)ã®å¤ã¯ä»¥ä¸ã®ç¡åé¢æ°ãåãã§ãã
\k -> 5 + 6 + k
æå¾ã«((addThree 5) 6)ã«ãã1ã¤å¼æ°ã渡ãã¦ã¿ã¾ãã
((addThree 5) 6) 7
(((addThree 5) 6) 7)ã®å¤ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
5 + 6 + 7
ã¤ã¾ãã(((addThree 5) 6) 7)ã®å¤ã¯18ã§ãããã®çµæã¯(addThree 5 6 7)ã®ããã«å¼æ°ãã¾ã¨ãã¦æ¸ããå ´åã¨å¤ããã¾ããã
Haskellã§ã®é¢æ°é©ç¨
Haskellã§ã¯(addThree 5 6 7)ã®ããã«ãã¹ã¦ã®å¼æ°ãä¸åº¦ã«æ¸ããå ´åã§ããé¨åé©ç¨ã使ã£ã¦(((addThree 5) 6) 7)ã¨æ¸ãã¦ããã®ã ã¨è§£éããã¾ããã¤ã¾ã(take 3 [1, 2, 3, 4])ã¨è²·ãã¦ã((take 3) [1, 2, 3, 4])ã¨è§£éããã(replicate 5 'a')ã¨æ¸ãã¦ã((replicate 5) 'a')ã¨è§£éããã¦ãã¾ãã
Haskellã«ã¯çã®æå³ã§2ã¤ä»¥ä¸ã®å¼æ°ãã¨ãé¢æ°ã¯ç¡ããé¢æ°ã¯ã©ãã1ã¤ã ãå¼æ°ãã¨ã£ã¦é¢æ°ãè¿ãé¢æ°ã¨ãªãã¾ãã
é¢æ°ãè¿ãé¢æ°ã¯é«éé¢æ°ã¨ãªãã¾ãã®ã§ãHaskellã§ã¯2å¼æ°ä»¥ä¸ã®é¢æ°ã¯ã©ããé«éé¢æ°ã¨ãããã¨ã«ãªãã¾ãã
ã»ã¯ã·ã§ã³
2é æ¼ç®åã«å¯¾ãã¦ãé¨åé©ç¨ã使ããã¨ãã§ãã¾ããé¨åé©ç¨ã使ãå¼æ°ã1ã¤ã ã渡ãã2é æ¼ç®åã®ãã¨ãç¹ã«ã»ã¯ã·ã§ã³(section)ã¨è¨ããæ®éã®é¨åé©ç¨ã¨ã¯ç¹æ®ãªæ©è½ãç¨æããã¦ãã¾ãã
以ä¸ã®é¢æ°ã®ä¾ãè¦ã¦ã¿ã¾ãã
increment :: Int -> Int increment n = n + 1
incrementã¯å¼æ°ã«1ã足ãé¢æ°ã§ãããã®é¢æ°ã¯ã»ã¯ã·ã§ã³ã使ãã¨æ¬¡ã®ããã«ãæ¸ãã¾ãã
increment :: Int -> Int increment n = (+ 1) n
ã(+ 1)ããã»ã¯ã·ã§ã³ã§ããæ°å¤ã®ãã©ã¹1ã§ã¯ããã¾ããã
ãªããæ¬å¼§ãå«ãã¦ã»ã¯ã·ã§ã³ã®çºãæ¬å¼§ãçç¥ããäºã¯ã§ãã¾ããã
ã¾ãã(+ 1)ã¯æ¼ç®åã®å·¦å´ã渡ãã¦ããªãã»ã¯ã·ã§ã³ã§ãããããã®éã«ãæ¼ç®åã®å³å´ã渡ãã¦ããªãã»ã¯ã·ã§ã³ãä½ãã¾ãã
increment :: Int -> Int increment n = (1 +) n
ã(1 +)ããã»ã¯ã·ã§ã³ã§ããä»åãæ¬å¼§ã¯çç¥ã§ãã¾ããã
é¢æ°(1 + n)ã«å¼æ°ã¨ãã¦nãä¸ãã¦ã(1 +) nãã¨ããã¨ãã1 + nãã®å¤ãå¾ããã¾ãã
ã»ã¯ã·ã§ã³ã¨(-)é¢æ°
Haskellã«ã¯åé
æ¼ç®åã®ã+ãã¯ããã¾ãããåé
æ¼ç®åã®ã-ãã¯ããã¾ãã
ãã®ãããã(-1)ããæããã¦æ°å¤ã-1ããªã®ããããã¨ã(n - 1)ã®å·¦è¾ºã渡ãã¦ããªãã»ã¯ã·ã§ã³ãªã®ããè¦ãç®ããã¯æç½ã§ãªãã¨ãããã¨ã«ãªã£ã¦ãã¾ãã¾ããã
Haskellã§ã¯ã(-1)ã¯å¸¸ã«æ°å¤ã-1ãã¨è§£éããã¾ãã(- 1)ã®ããã«æ°åã®éã«ã¹ãã¼ã¹ãå
¥ãã¦ãããã¯ãæ°å¤ã¨ãã¦è§£éããã¾ãã(n - 1)ã®å·¦è¾ºã渡ãã¦ããªãã»ã¯ã·ã§ã³ã«å¯¾å¿ããé¢æ°ãä½ãã«ã¯ãsubtracté¢æ°ã使ã£ã¦(subtract 1)ã¨æ¸ãå¿
è¦ãããã¾ãã
é¨åé©ç¨ã®å¿ç¨(1) ----é«éé¢æ°ã¨é¨åé©ç¨
é¨åé©ç¨ã®å
¸åçãªå¿ç¨ä¾ãè¦ã¦ããã¾ãã
ã¾ããé«éé¢æ°ã¨é¨åé©ç¨ãçµã¿åããã使ãæ¹ã§ãã次ã®ä¾ã¯mapé¢æ°ã¨ã»ã¯ã·ã§ã³ã使ã£ã¦ãªã¹ãã®å
¨è¦ç´ ã«7ã足ãé¢æ°ã®ä¾ã§ãã
map (+ 7) [1, 2, 3, 4, 5]
(+ 7)ãã»ã¯ã·ã§ã³ã§ããå¼æ°ã«7ã足ãé¢æ°ãã§ãããã®é¢æ°ãmapé¢æ°ã§ãªã¹ãã®åè¦ç´ ã«é©ç¨ããã®ã§ãä¸è¨ã®å¼ã¯ãªã¹ãã®å ¨è¦ç´ ã«7ã足ããã¨ã«ãªãã¾ãããã®å¤ã¯[8, 9, 10, 11, 12]ã§ãã
次ã¯filteré¢æ°ã使ã£ã¦ãæååãã'\r'ã ããåãé¤ãä¾ã§ãã
filter (\= '\r') "aaa\r\nbbb\r\nccc\r\nddd\r\neee\r\n"
ã\=ãã¯ã==ãã®å¦å®ã§ããCãJavaã§ããã!=ãã«å½ããã¾ãã
(\= '\r')ã¯ãå¼æ°ã'\r'ã§ãªããã°Trueãè¿ãé¢æ°ãã¨ãªãããfilter (\= '\r') csãã¯csã®ãã¡'\r'以å¤ã®æåãæãåºãã¾ããéã«è¨ãã¨ãcsãã'\r'ã ããåãé¤ãã¾ãã
é¨åé©ç¨ã®å¿ç¨(2) ----å¤æ°ã®åæ¸
次ã¯é¨åé©ç¨ã使ã£ã¦å®ç¾©ããå¤æ°ãåæ¸ããä¾ãè¦ã¦ããã¾ãã
次ã®é¢æ°ã®ä¾
zipLineNumber :: [String] -> [(Int, String))] zipLineNumber xs = zip [1..] xs
ãã®é¢æ°ã¯ãé¨åé©ç¨ã使ãã¨æ¬¡ã®ããã«æ¸ãæãããã¾ãã
zipLineNumber :: [String] -> [(Int, String))] zipLineNumber = zip [1..]
ãã®ããã«æ¸ãæããããçç±ã¯ã次ã®ããã«å®ç¾©ãå¤å½¢ããã¨åããããããªãã¾ãã
zipLineNumber xs = zip [1..] xs â zipLineNumber xs = (zip [1..]) xs â zipLineNumber xs = f xs where f = zip [1..]
ããæ¸ãæãã¦ã¿ãã¨ãzipLineNumberé¢æ°ã¯é¢æ°fã®å¥åã«ãããªããã¨ãåããã¾ãã
次ã®ããã«æ¸ãæãã¦ã¿ã¾ãã
zipLineNumber xs = f xs â zipLineNumber = f
ããã§fãzip [1..]ã«å±éããã°ãæåã®å½¢ã«ãªãã¾ãã
zipLineNumber = zip [1..]
ä»æ¥ã®ã¨ããã¯ããã¾ã§ã
é¢é£è¨äº
[Haskell][プログラミング言語][関数型言語]Haskell 15日目 〜 値としての関数 〜
[Haskell][プログラミング言語][関数型言語]Haskell 14日目 〜 定義と束縛 〜
[Haskell][プログラミング言語][関数型言語]Haskell 13日目
[Haskell][プログラミング言語][関数型言語]Haskell 12日目
[Haskell][プログラミング言語][関数型言語]ふつうのHaskellプログラミング
- ä½è : éæ¨å³°é,å±±ä¸ä¼¸å¤«
- åºç社/ã¡ã¼ã«ã¼: ã½ãããã³ã¯ã¯ãªã¨ã¤ãã£ã
- çºå£²æ¥: 2006/06/01
- ã¡ãã£ã¢: åè¡æ¬
- è³¼å ¥: 25人 ã¯ãªãã¯: 314å
- ãã®ååãå«ãããã° (320件) ãè¦ã