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

ループ1

問題文

import Data.IORef

-- Define 'while'
while test action = do
    val <- test
    if val then do { action; while test action }
    else return ()

-- Some helpers for use with 'while'
incr ref = modifyIORef ref (+1)
test ref f = do { val <- readIORef ref; return (f val) }

main = do
    ref <- newIORef 0
    while (test ref (< 5))
          (do { putStrLn "Hello World!"; incr ref })

FizzBuzz

問題文

module Main where

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

クイックソート

問題文

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]

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