C++ for Haskeller
http://haskell.g.hatena.ne.jp/mr_konn/20061006/1160138831
Haskellããå
¥ã£ã人ã«ã¯ãcinã¨ãcoutã¨ãå¾®å¦ããã¦ãããªãã ããã¨ãæã£ãã®ã§ãé©å½ã«ã
対象èªè ã¯ãHaskellãããããC++ãããã人ï¼æå³ãã¼ï¼ããã¿ãç解ã§ãã人ã§ãã
C++ã¨ã¯
C++ã¨ã¯ã以ä¸ã®ãããªç¹å¾´ãæã£ãããã°ã©ãã³ã°è¨èªã§ã
- åç §éæï¼ï¼ï¼
- åºç¾è©ä¾¡(occurrence evaluation) ï¼ãªã©ã¨ããããããããåèªãã¤ããï¼
- ãã¿ã¼ã³ãããã§ãã
- å ¨ãç´æçã§ãªãæ§æ
- IOã¢ãããæ¸ãããã®å¤§éã®syntax sugarããã
説æãã¦ããã¨é·ããªãã®ã§ããããããããããå¿
è¦ã«ãªã£ãã¨ãã«èª¬æããããã«ãã¾ãã
C++ã®å
C++ã«ã¯ã次ã®åãããã¾ãã
- æ´æ°
- çå½å¤
- é£æ³é å
ã¾ãããããã説æãï¼ã¯ããã飽ãã¦ããï¼
å¤æ°
飽ãã¦ããã®ã§ããã£ãã¨ããã°ã©ã ãç´¹ä»ãå¤ãååã«æç¸ã§ãã¾ãããªããæ´æ°ãçå½å¤ã¨ãé£æ³é åã§ãæç¸ã®ä»æ¹ãéãã®ã§æ³¨æï¼
int const n = 10;
æ´æ°å¤10ãåånã«æç¸ãã¾ãããªãã'const'ã¯å¿
é ãªã®ã§æ³¨æãï¼é©ãã¹ããã¨ã«ã'int n = 10;'ã¨ããå ´åãIOã¢ãããçæããsyntax sugarã«ãªãã¾ãï¼ããã«ã¤ãã¦ã¯å¾è¿°ï¼ããªãã§ãï¼ï¼
Haskellã§æ¸ãã¨ãããªæã
n :: Int n = 10
ãªããC++ã«ã¯æ®å¿µãªããåæ¨è«ãç¡ãã®ã§ãåãçç¥ãããã¨ã¯ã§ãã¾ããã
ç¶ãã¦ãçå½å¤ã
bool const b = true;
çå½å¤trueãååbã«æç¸ãã¾ãããããconstä»ããªãã¨syntax sugarã«ãªãã®ã§æ³¨æã
æå¾ã«é£æ³é åãé£æ³é åã®æç¸ã®ãããã¯æ´æ°ãçå½å¤ã¨éãã®ã§æ³¨æã
struct AssocArray { static int const n = 10; static int const m = n+10; static bool const b = true; };
åå'AssocArray'ã«é£æ³é
å { n::int=>10, m::int=>20, b::bool=>true } ãæç¸ãã¾ãã
C++ã®ä¸çã§ã¯ãé£æ³é
åã"typeï¼åï¼"ã¨å¼ã³ã¾ãï¼ã¤ã«ããååã§ããï¼é£æ³é
åã¯ãtypeåï¼ååï¼ã«ãªãã¾ãã
é£æ³é åãå®ç¾©ããã¨ããã¨ã§ã
int const z = AssocArray::n + AssocArray::m;
ã¨ãæ¸ãã¾ããï¼ãã¯ãã©ãããç¨èªã使ãã°ãããããããªããæãã¦ï¼åã人ï¼ï¼
å°ãé£æ³é åã®ã¡ã³ããå®ç¾©ããã¨ãã¯ã'static'ãå¿ããªãããã«ãã¾ããããï¼ä½æ ã§ããï¼ç¥ãã¾ãããï¼
struct AssocArray { int const n = 10; int const m = n+10; bool const b = true; };
ããã¯ã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªãã¾ãã
ååã«æç¸ãããé£æ³é åã®å¤ãããã«å¥ã®ååã«æç¸ããã¨ãã¯ãtypedefã使ãã¾ãã
// 'AssocArray'ã«æç¸ããã¦ãå¤ã'AssocArray2'ã¨ããååã«ãæç¸ããã typedef AssocArray AssocArray2;
Haskellã§æ¸ãã¨ã
assocArray2 :: Type assocArray2 = assocArray
ãããªæãã§ãã
é¢æ°
é¢æ°ãæ¸ãã¾ãããï¼é¢æ°ã®æ§æã¯ã©ãããã¦ãã®ã§æ³¨æï¼
- ä½æ ããã¼ã¯ã¼ã'template'ã使ãã¾ã
- è¿ããåã¯ãé£æ³é åã®ã¿ã¨ãªãã¾ã
- 大å¤èªã¿ã«ããã§ãï¼
ããï¼å¿ãè½ã¡ä»ããã¦ï¼ä½æ ãããªã¸ãã¦ããããªä»æ§ã«ãªã£ã¦ãã®ãï¼ããã¯ç¥ã®ã¿ãç¥ãï¼
æ´æ°å ç®ãè¡ãé¢æ°ã¯âã®ããã«ãªãã¾ãã
template <int n, int m> struct add_int { static int const result = n+m; };
ãªããè¿ããå¤ãé£æ³é åã®ã¿ãªã®ã§ãã©ã£ãã§å ç®ããçµæãæ¾ã£ã¦ããããã°ã©ã ãå¿ è¦ã«ãªãã¾ãã
int const n = 10; int const m = 20; typedef result_aarray = add_int<n,m>; // add_int<n,m>ã®è©ä¾¡çµæã'result_aarray'ã«æç¸ int const o = result_aarray::result; // result_aarrayã®ã¡ã³ãresultããå ç®ããçµæ
ãããããã§ããï¼ï¼ããããããªããï¼é¢æ°ã®æ¸ãããã«ã¤ãã¦èª¬æãã¦ããã¾ãããã
template // é¢æ°ã§ãããã¨ã示ããã¼ã¯ã¼ã <int n, int m> // å¼æ°ã¯ n::int, m::int ã®ãµã㤠struct // æ»ãå¤ãé£æ³é åã§ãããã¨ã示ãï¼ã¨ãã£ã¦ããé£æ³é åããè¿ããªããã©â¦ï¼ add_int // é¢æ°å { static int const result = n+m; } // ãã®é¨åããé¢æ°ã®æ»ãå¤ ;
ãã¨ããã¿ã¼ã³ãããã使ãã¾ãããã¨ãã°ãä¸ã®ãadd_intã¯ã
template <int n,int m> struct add_int { static int const result = n+m; }; template <> struct add_int <10,10> { static int const result = 40; };
ãããã£ã¦ããã°ãå¼æ°ãã(10,10)ã ã£ãå ´åã«ãä¸è¬ã®add_intã¨ã¯éãå¤ãè¿ãã¾ãã
int const z = add_int<10,10>::result; // z = 40
ãã¿ã¼ã³ãããã使ãã¨ãã¯ããã¿ã¼ã³ã®ä½ç½®ãå¤ãªã¨ããã«æ¥ãã®ã«æ³¨æã
template <10,10> struct add_int { static int const result = 40; };
ã§ã¯ãªãã§ãï¼
ãããããã§ãNEï¼ï¼ãããããªããï¼ãã£ãããéä¹ã§ãããéä¹ã¯ã
fact n = fact(n-1) * n fact 1 = 1
ãããªæãã§ããï¼C++ã§æ¸ãã¨ããããªãã¾ãâ
template <int n> struct fact { static int const result = fact<n-1>::result * n; }; template <> struct fact<1> { static int const result = 1; };
int const z = fact<4>::result;
ãã¨ã¯ãããããã®ãçµã¿ç«ã¦ã¦ããã°ããªããããããªãã§ãã§ãã¾ããï¼ããããªãããå¶éããããã ããªâ¦ç¢ºãï¼
duck typing
é£æ³é ååï¼typeåï¼ã®å¤ã¯ãå ¨é¨åãåã«ãªãã®ã§ãduck typingã¿ãããªã¢ã¬ã§ãï¼âç²ãã¦ããï¼ãC++ããã°ã©ãã³ã°ã¯ãDuck Typing大好ããªãã³ã¼ã¨ãã±ãããã¨ãã®äººã«ãã¸ãå§ãã
template <int n,int m> struct add_int { static int const result = n+m; }; struct duck_a { static int const hogehoge = 10; }; struct duck_b { static int const hogehoge = add_int<duck_a::hogehoge,4>::result; }; template <typename T> struct get_hogehoge { static int const result = T::hogehoge; }; // é£æ³é å T ã®ä¸ã®åå'hogehoge'ã«æç¸ããã¦ãå¤ãæ¾ã£ã¦ãã extern int a, b; int a = get_hogehoge<duck_a>::result; // a = 10; int b = get_hogehoge<duck_b>::result; // b = 14;
duck_aã¨duck_bã«ç¶æ¿é¢ä¿ãç¡ãã¦ããåå'hogehoge'ãæã£ã¦ããã®ã§ãåãããã«æ±ãã¾ãã
Hello World
以ä¸ã®ç¥èã®å¿ç¨ã¨ãã¦ãHello Worldãæ¸ãã¦ã¿ã¾ãããã
Hello Worldã¯æ¬¡ã®ããã«æ¸ãã¾ãâ
#include <stdio.h> int main( ) { puts("Hello World."); }
ãã¼ãããã£ããï¼
ç¶ãï¼ãããç¡ãï¼
ã®ã¬ã¦ã£ã³ã°
ããããã41å
ç¹ã§ãã£ãã
æ®æ©0ããã 0ã§ã®ãªã®ãªã
次ã¯ä½ããããªã¼ãã®ã¬ã¦ã£ã³ã°2ã¨ãã ãããããã£ã±ããæè¿ãã¯ãªã¢ã§ããããªãã¤ã°ã£ãããã£ã¦ããããªâ¦
â
ãã¼ãä¸ã®ã¡ãã£ã¨ééã£ã¦ãã
- typedef result_aarray = add_int<n,m>; // add_int<n,m>ã®è©ä¾¡çµæã'result_aarray'ã«æç¸ + typedef add_int<n,m> result_aarray; // add_int<n,m>ã®è©ä¾¡çµæã'result_aarray'ã«æç¸
ã§ãããã¼ããä¸å¿ã²ã¨ã¨ããåãããã¨æã£ã¦ããã ãã©â¦
C++ï¼ã¨ããããBoostï¼ã®ç´ æ´ãããã¨ããã¯ãä¸ã§æ¸ãããããªãã¿ããã¿ã§ã¯ãªãã¦ãå®éã«ãããæå¹æ´»ç¨ãã¦ãç¹ã«ããã¨æããï¼é¢é£ããããï¼
åã¯ãã¾ã ãã®ã¬ãã«ã«éãã¦ãªãã®ã§ããªãã¨ãè¨ããªããã©â¦
ã¨ãããããC++ããã°ã©ãããJavaã®Genericsãè¦ãã¨ãã«ãä½ãã使ããã¼ãã¨ããåå¿ãããçç±ãç解ãã¦ãããã§ããC++ã¯æ代é ãã®ãªãã¸ã§ã¯ãæåè¨èªãã¨ãããèããä¿®æ£ãã¦ããã ããã°ããã®ãã¡ãªããã®å½¹ã«ç«ã¤ãããããªãããå½¹ã«ç«ããªããããããªãã§ãã