落書きノート

ふと自分が気になった事を書いてます

Haskellで不定期練習

OCamlの方が便利かも。今回、繰り返しをやったんですけど、Haskellはちょっとこの辺り不便でした。

import Text.Printf
import Data.List
import Data.Array

main :: IO ()

main = do
  putStrLn "変数a"
  v1 <- getLine
  putStrLn "変数b"
  v2 <- getLine
  putStrLn "変数c"
  v3 <- getLine
  let tl = [read v1 :: Int, read v2 :: Int, read v3 :: Int]
  let ab = listArray (0, 2) $ sort tl
  printf "変数aは%dです。\n" $ ab ! 0
  printf "変数bは%dです。\n" $ ab ! 1
  printf "変数cは%dです。\n" $ ab ! 2



import System.Random

main :: IO ()

f n
  | n == 0 = "グー"
  | n == 1 = "チョキ"
  | n == 2 = "パー"
  
main = do
  a <- (getStdRandom $ randomR (0, 2) :: IO Int)
  putStr $ "コンピュータが生成した手 : "
  putStrLn $ f a



main :: IO ()

f n
  | n >= 3 && n <= 5 = "春"
  | n >= 6 && n <= 8 = "夏"
  | n >= 9 && n <= 11 = "秋"
  | n == 1 || n == 2 || n == 12 = "冬"
  | otherwise = "そんな月はありません。"
                
main = do
  putStrLn "何月ですか"
  v1 <- getLine
  let month = read v1 :: Int
  putStrLn $ f month



import Data.IORef

f :: IORef Bool -> IO ()
main :: IO ()

f flag = do
  t <- readIORef flag
  if t == True then
    return ()
  else do
  putStrLn "整数値"
  v1 <- getLine
  let t = read v1 :: Int
  if t > 0 then
    putStrLn "その値は正です。"
    else if t < 0 then
    putStrLn "その値は負です。"
    else
    putStrLn "その値は0です。"
  putStrLn "もう一度? 1...Yes/0...No"
  v2 <- getLine
  let choose = read v2 :: Int
  if choose == 1 then
    writeIORef flag False
    else
    writeIORef flag True
  f flag
    
main = do
  s <- newIORef False
  f s