シフトが高階関数かどうか知りたい。
chartoInt :: Char -> Int
chartoInt c = ord c
Inttochar :: Int -> Char
Inttochar n = chr n
shift :: Int -> Char -> Char
shift n c = Inttochar (chartoInt c + n)
シフトが高階関数かどうか知りたい。
chartoInt :: Char -> Int
chartoInt c = ord c
Inttochar :: Int -> Char
Inttochar n = chr n
shift :: Int -> Char -> Char
shift n c = Inttochar (chartoInt c + n)
これらの関数はいずれも関数をパラメーターとしてとらないため、これらの関数はいずれも高階関数ではありません。
shift
のパラメータはn
(an Int
) とc
(a Char
) です。どちらも関数ではありません。
(また、次のInttochar
ようにする必要がありますinttochar
: Haskell の関数名は大文字で始めることはできません。)
あなたのような高階関数は次のshift
とおりです。
higherShift :: (Int -> Char) -> Int -> Char -> Char
higherShift f n c = f (chartoInt c + n)
shift = higherShift inttochar -- same as your original shift
または、おそらくもっと便利です:
anotherHigherShift :: (Int -> Int) -> Char -> Char
anotherHigherShift f c = inttochar (f (chartoInt c))
shift n = anotherHigherShift (+n) -- same as your original shift
の型シグネチャを次のanotherHigherShift
ように読むことができます。
Int
を返しますInt
)Char
Char
(+n)
の省略形です\m -> m + n
。
です。
シフトは高次関数です。
shift :: Int -> (Char -> Char) -- The long prototype.
getInt
と return 関数のgetChar
とreturnChar
です。
PSあなたは書くべきinttochar
です。
非公式のルールがあります: 関数の型を見てください。(必要に応じて [1]) 中括弧が含まれている場合、それは高階関数です。
[1] 省略すると型が変わるという意味で。
そして、この観点から、最初の回答から関数のタイプと関数を見てみましょう。それは簡単です。