トップ
新規
単語検索
ヘルプ
Haskell
をテンプレートにして作成
開始行:
* cat by foldM [#o471dc62]
[[foldM:http://www.haskell.org/ghc/docs/latest/html/libra...
import Control.Monad(foldM)
import System.Environment(getArgs)
-- readFile :: FilePath -> IO String
-- getArgs :: IO [String]
-- foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a
freadAppend :: String -> FilePath -> IO String
freadAppend x f = do
str <- readFile f
return $ x++str
main :: IO ()
main = do
files <- getArgs
str <- foldM freadAppend "" files
putStrLn str
*sumOfLen by foldM [#c27d28fa]
長さを表すリストをとって、その総和を返す関数を作ってみま...
sumOfLen = foldM add 0
where
add a b = if b < 0 then
Nothing
else
Just $ a + b
*Main> sumOfLen [3,4,5]
Just 12
*Main> sumOfLen [3,-4,5]
Nothing
*Main>
こういう感じの使いかたであってるんでしょうか?
終了行:
* cat by foldM [#o471dc62]
[[foldM:http://www.haskell.org/ghc/docs/latest/html/libra...
import Control.Monad(foldM)
import System.Environment(getArgs)
-- readFile :: FilePath -> IO String
-- getArgs :: IO [String]
-- foldM :: Monad m => (a -> b -> m a) -> a -> [b] -> m a
freadAppend :: String -> FilePath -> IO String
freadAppend x f = do
str <- readFile f
return $ x++str
main :: IO ()
main = do
files <- getArgs
str <- foldM freadAppend "" files
putStrLn str
*sumOfLen by foldM [#c27d28fa]
長さを表すリストをとって、その総和を返す関数を作ってみま...
sumOfLen = foldM add 0
where
add a b = if b < 0 then
Nothing
else
Just $ a + b
*Main> sumOfLen [3,4,5]
Just 12
*Main> sumOfLen [3,-4,5]
Nothing
*Main>
こういう感じの使いかたであってるんでしょうか?
ページ名: