- 追加された行はこの色です。
- 削除された行はこの色です。
変な処理があれば修正してください。
*ループ1 [#a13940a0]
[[問題文>練習問題#eb2c4338]]
times 0 action = return ()
times n action = action >> times (n-1) action
main = do
5 `times` putStrLn "Hello World!"
*ループ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]]
module Main where
main = do
mapM_ putStrLn (map fizzBuzz [1..100])
main :: IO()
main = printAll $ map fizzBuzz [1..100]
where
printAll [] = return ()
printAll (x:xs) = putStrLn x >> printAll xs
fizzBuzz :: Integer -> String
fizzBuzz n | n `mod` 15 == 0 = "FizzBuzz"
| n `mod` 5 == 0 = "Buzz"
| n `mod` 3 == 0 = "Fizz"
| otherwise = show n
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)
*クイックソート [#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]