私は最近Haskellのを学習し、私はHaskellの中のpythonでの面接のためにした何かを書き換えしようとして始めました。私はキャメルケースの文字列を分離したアンダースコア( "myVariableName" - > "my_variable_name")に変換しようとしていますが、最初の文字が大文字の場合はエラーもスローされます。ここで初心者Haskellの問題(のための...から生じるノーインスタンス)
は、私が持っているものです。
import qualified Data.Char as Char
translate_java :: String -> String
translate_java xs = translate_helper $ enumerate xs
where
translate_helper [] = []
translate_helper ((a, num):xs)
| num == 1 and Char.isUpper a = error "cannot start with upper"
| Char.isUpper a = '_' : Char.toLower a : translate_helper xs
| otherwise = a : translate_helper xs
enumerate :: (Num b, Enum b) => [a] -> [(a,b)]
enumerate xs = zip xs [1..]
私はそれは私が奇妙な方法でこのことについてつもりだ、と私はこれを実装するためのより良い方法についてのアドバイスをしてみたいが、私が好きな、かなり可能性があります実現しますこれをコンパイルすることもできます。ここには私が今いるエラーがあります:
Prelude> :r
[1 of 1] Compiling Main (translate.hs, interpreted)
translate.hs:4:20:
No instance for (Num
(([Bool] -> Bool) -> (Char -> Bool) -> Char -> t))
arising from a use of `translate_helper' at translate.hs:4:20-35
Possible fix:
add an instance declaration for
(Num (([Bool] -> Bool) -> (Char -> Bool) -> Char -> t))
In the first argument of `($)', namely `translate_helper'
In the expression: translate_helper $ enumerate xs
In the definition of `translate_java':
translate_java xs
= translate_helper $ enumerate xs
where
translate_helper [] = []
translate_helper ((a, num) : xs)
| num == 1 and Char.isUpper a
= error "cannot start with upper
"
| Char.isUpper a
= '_' : Char.toLower a : transla
te_helper xs
| otherwise = a : translate_help
er xs
Failed, modules loaded: none.
ここで起こっていることの説明は素晴らしいでしょう。私は本当に "(Bool - > Bool) - >(Char - > Bool) - > Char - > t))がどこから来ているのか分かりません。私はtranslate_helperの型宣言は[(a、b)] - > [a]のようなものだろうと思いますか?
速い応答のおかげで、私は永遠にその機能を見て過ごした、私はこれまでにそれをキャッチしたとは思わない。 1も多型であるという良い説明。 – cgag