変な処理があれば修正してください。

*ループ1 [#a13940a0]
[[問題文>練習問題#eb2c4338]]
 times 0 action = return ()
 times n action = action >> times (n-1) action
 
 main = do
     5 `times` putStrLn "Hello World!"
別解
 main = putStr $ "おっぱい!" >> "Hello World!\n"

*ループ2 [#ob0d5bdb]
[[問題文>練習問題#eb2c4338]]
 import System.Environment
 
 times 0 action = return ()
 times n action = action >> times (n-1) action
 
 main = do
     x <- getArgs
     (read (x !! 0)) `times` putStrLn "Hello World!"

*FizzBuzz [#f4c6d1f7]
[[問題文>練習問題#t52e5a48]]
 main = do
     mapM_ putStrLn (map fizzBuzz [1..100])
 
 fizzBuzz :: Integer -> String
 fizzBuzz n
     | n `mod` 15 == 0 = "FizzBuzz"
     | n `mod`  5 == 0 = "Buzz"
     | n `mod`  3 == 0 = "Fizz"
     | otherwise       = show n

*素数列挙 [#ld4986f8]
[[問題文>練習問題#o8db2119]]
 primesTo m = 2 : sieve [3, 5..m] where
     sieve [] = []
     sieve (p:xs) = p : sieve (xs `minus` [p*p, p*p+2*p..m])
 
 minus (x:xs) (y:ys) = case (compare x y) of
     LT -> x : minus xs (y:ys)
     EQ ->     minus xs ys
     GT ->     minus (x:xs) ys
 minus xs _ = xs
 
 main = do
     print $ primesTo (floor 1e6)

*うるう年判定 [#rc63b4a5]
[[問題文>練習問題#i0d67516]]
 isleap :: Integer -> Bool
 isleap y
     | y `mod` 400 == 0 = True
     | y `mod` 100 == 0 = False
     | y `mod`   4 == 0 = True
     | otherwise        = False
 
 main = do
     putStrLn "西暦を入力してください"
     x <- getLine
     putStrLn (x ++ "年はうるう年" ++ if isleap(read(x)) then "です" else "ではありません" )

*フィボナッチ数列 [#mf294cb0]
[[問題文>練習問題#p59f794f]]
 import System.Environment
 
 fibo = 0 : 1 : zipWith (+) fibo (tail fibo)
 
 main = do
     x <- getArgs
     putStrLn (show (fibo !! read (x !! 0)))

*累乗1 [#b0e3fd20]
[[問題文>練習問題#x6e30de3]]
 pow :: Integer -> Integer -> Integer
 pow _ 0 = 1
 pow 0 _ = 0
 pow a b = a * pow a (b-1)
 
 main = do
     putStrLn (show (pow 2 10))

*累乗2 [#w1eaeb0e]
[[問題文>練習問題#x6e30de3]]
 pow :: Integer -> Integer -> Integer
 pow 0 _ = 0
 pow _ 0 = 1
 pow a b = pow' 1 a b
     where
     pow' x a 0 = x
     pow' x a b
         | even b = pow' x (a*a) (div b 2)
         | otherwise = pow' (x*a) a (b-1)
 
 main = do
     putStrLn (show (pow 2 1000007))

*クイックソート [#w101147e]
[[問題文>練習問題(アルゴリズム編)#q8e0119e]]
 quicksort :: (Ord a) => [a] -> [a]
 quicksort [] = []
 quicksort (x:xs) =
     let smallerSorted = quicksort[a | a<-xs, a <= x]
         biggerSorted  = quicksort[a | a<-xs, a >  x]
      in smallerSorted ++ [x] ++ biggerSorted
 
 main :: IO()
 main = print $ quicksort [5, 7, 9, 2, 1, 3, 8, 4, 6, 0]

*世界のナベアツ問題 [#p9107c97]
[[問題文>練習問題#t52a5a48]]
 nabeatsu :: Integer -> String
 nabeatsu x = if mod x 3 == 0 || elem '3' s then "Aho" else s where s = show x
 
 main :: IO ()
 main = putStrLn . unwords $ map nabeatsu [1..100]

*Caesar暗号解読 [#v9844940]
[[問題文>練習問題#h54a0395]]
 import Data.List (isInfixOf)
  
 next :: String -> Char -> Char
 next (x:xs) = \c -> if x == c then head xs else next xs c
 next _ = id
  
 analyze :: String -> String
 analyze = head . filter (isInfixOf keyword) . iterate (map $ next chars)
     where chars   = "-abcdefghijklmnopqrstuvwxyz .,-"
           keyword = "person"
  
 main :: IO ()
 main =  putStrLn $ analyze encoded
     where encoded = "qdq-gi.q-a ziatmxxitmdqibtqi-ustbi ri.qmoqrcxi.qbubu zir -ibtq\
                     \i-qp-qaai ripmymsqkir -ibtqi-qy dmxi ri.cnxuoi rruoumxakir -ib\
                     \tqiqzmobyqzbkii-q.qmxi -imyqzpyqzbi rixmeaki -puzmzoqai -i-qsc\
                     \xmbu zaimzpir -i btq-iymbbq-a;iz -iatmxximzgi.q-a zinqiuzimzgi\
                     \emgipuao-uyuzmbqpimsmuzabir -ia. za -uzsiacotiimi.qbubu zj"

*数当てゲーム [#u56f4eea]
[[問題文>練習問題#d082e883]]
 import System.IO
 import System.Random
 import Control.Monad
  
 game :: Int -> IO ()
 game x = do
     us <- getLine
     case reads us of
         [(u, _)] -> when (x /= u) $ do
             putStrLn . ("too "++) . (++"!") $ if x < u then "big" else "small"
             game x
         _ -> putStrLn "invalid input!" >> game x
  
 main :: IO ()
 main = do hSetBuffering stdout NoBuffering
           putStrLn "input a number!"
           getStdGen >>= game . fst . randomR (1,100)
           putStrLn "right!"

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS