これは説明するのが少し難しいですが、私はこの状況に数回ぶつかりました。パラメータの型が静的でない場合、どのようにHaskellステートメントを分割できますか?
work :: String -> IO()
work a = do
input <- lines <$> getContents
sortF <- let f = flip sortByM input
in case a of
"name" -> f (return . id :: FilePath -> IO FilePath)
"time" -> f (getModificationTime :: FilePath -> IO UTCTime)
_ -> f (getModificationTime :: FilePath -> IO UTCTime)
print sortF
sortByM :: (Monad m, Ord a) => (b-> m a) -> [b] -> m [b]
sortByM f x = do
x' <- mapM f x
return $ fst <$> (sortBy (comparing snd) $ zip x x')
を上記のエラーがスローされます:
コードは次のようである理にかなって
• Couldn't match type ‘UTCTime’ with ‘[Char]’
Expected type: String -> IO FilePath
Actual type: FilePath -> IO UTCTime
• In the first argument of ‘f’, namely
‘(getModificationTime :: FilePath -> IO UTCTime)’
In the expression:
f (getModificationTime :: FilePath -> IO UTCTime)
In a case alternative:
"time" -> f (getModificationTime :: FilePath -> IO UTCTime)
を、何とか上記目的を達成する方法はありますか?そうでなければ、私はあまり、保守感じる、下記行う必要があります:
work :: String -> IO()
work a = do
input <- lines <$> getContents
sortF <- case a of
"name" -> flip sortByM input (return . id :: FilePath -> IO FilePath)
"time" -> flip sortByM input (getModificationTime :: FilePath -> IO UTCTime)
_ -> flip sortByM input (getModificationTime :: FilePath -> IO UTCTime)
print sortF
sortByM :: (Monad m, Ord a) => (b-> m a) -> [b] -> m [b]
sortByM f x = do
x' <- mapM f x
return $ fst <$> (sortBy (comparing snd) $ zip x x')
は 'せ、F X = sortByM X input'仕事をしていますか? – melpomene