D for Haskeller : Lesson 1024 「高階関数」

以前の話の続きのような物です。誰も続けてくれないので自分で続けます。
今日は高階関数のお勉強をしましょう。
始めは勿論、皆さんご存知「map」です。

template map(alias f, char[] s){ const char[] map = f!(head!(s)) ~ map!(f, rest!(s)); }
template map(alias f, char[] s : ""){ const char[] map = s; }

「alias funcname」とすることで、関数funcnameを引数として受け取ることが出来ます。
headとrestは以下のような関数です。

template head(char[] s){ const head = s[0]; }
template rest(char[] s){ const rest = s[1 .. length]; }

では早速、mapを実際に使ってみましょう。

template add(char c){ const char add = c + 1; }
const iphf = map!(add, "hoge");

ここで注意点が一つ。addを以下のように書いてはいけません。

template add(char c){ const add = c + 1; } // add はint型に推論されてしまう!

これは、「整数の昇格」が発生するためです。「c + 1」の「1」がint型であるために、上の式は「cast(int)c + 1」という風に解釈されてしまうのです。
さて、今回はここまでです。次回は…次回は何だろう?
Haskellのことを知らないので何を書いたら良いか分かりません!><