いろんな言語で宿題ス ..
[2ch|▼Menu]
64:Haskellで
09/05/31 16:20:18
import Data.List
import Control.Monad

data Position = Shoe | Skirt | Chest | Hair
  deriving (Show, Eq, Ord)

solve :: [String]
solve = do
  -- ちょうど一万円札・千円札・百円玉・十円玉があるわ。今からこのお金を・・・
  let money = [10000, 1000, 100, 10]
  -- 髪の中、胸のポケット、スカートのポケット、靴の中に・・・それぞれ一つずつ隠します
  (position, content) <- mappings money [Hair, Chest, Skirt, Shoe]
  -- 一万円札は髪の中
  guard $ position 10000 == Hair
  -- 千円札は十円玉より上の位置
  guard $ position 1000 > position 10
  -- スカートには胸の10倍のお金が入っている
  guard $ content Skirt == 10 * content Chest
  -- それでは、全てのお金の位置は?
  let disp n = show (position n) ++ "(" ++ show n ++ ")"
  return $ unwords $ map disp money

main = putStr $ unlines solve

mappings :: (Eq a, Eq b) => [a] -> [b] -> [(a -> b, b -> a)]
mappings xs ys = do
  ys' <- permutations ys
  return (make xs ys', make ys' xs)
  where
    make from to = \value -> case lookup value (zip from to) of
      Just r -> r
      Nothing -> error "lookup failed"


次ページ
続きを表示
1を表示
最新レス表示
スレッドの検索
類似スレ一覧
話題のニュース
おまかせリスト
▼オプションを表示
暇つぶし2ch

4630日前に更新/314 KB
担当:undef