いろんな言語で宿題ス ..
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"
次ページ最新レス表示スレッドの検索類似スレ一覧話題のニュースおまかせリスト▼オプションを表示暇つぶし2ch
4630日前に更新/314 KB
担当:undef