最近老师总是让出一些计算题,于是用haskell写了一个。
-- file: make_math2.hs -- 出n道三个参数的加法 -- 452 + 990 + 130=1572 -- 使用方法: gs n x y -- n 表示需要多少道题 -- x 表示最小值 -- y 表示最大值 -- 比如 gs 10 100 999 表示出10道 加法 -- gs'会过滤掉被10整除的数字,这样难一点 import System.Random import System.IO.Unsafe drawInt :: Int -> Int -> IO Int drawInt x y = getStdRandom (randomR (x,y)) random_list :: (Eq a, Num a) => a -> Int -> Int -> IO [Int] random_list 0 _ _ = return [] random_list n x y = do a <- drawInt x y rest <- (random_list (n-1) x y) return (a : rest) math_practice n x y = zip [1..] $ map trans_formula (mix_list (init_val (n*3) x y )) where mix_list [] = [] mix_list (x:y:z:xs) = (x,y,z) : mix_list xs init_val n x y = unsafePerformIO $ random_list n x y trans_formula (x,y,z) = ((add_str x y z) , (add_str' x y z)) add_str x y z = show x ++ " + " ++ show y ++ " + " ++ show z ++ "= " add_str' x y z = show x ++ " + " ++ show y ++ " + " ++ show z ++ "=" ++ show (x+y+z) math_practice' n x y = zip [1..] $ map trans_formula $ mix_list $ take (n*3) $ filter (\x -> (x `mod` 10) /= 0) $ init_val (n*9) x y where mix_list [] = [] mix_list (x:y:z:xs) = (x,y,z) : mix_list xs init_val n x y = unsafePerformIO $ random_list n x y trans_formula (x,y,z) = ((add_str x y z) , (add_str' x y z)) add_str x y z = show x ++ " + " ++ show y ++ " + " ++ show z ++ "= " add_str' x y z = show x ++ " + " ++ show y ++ " + " ++ show z ++ "=" ++ show (x+y+z) gs n x y = do mapM_ putStrLn [ show a ++ ". " ++ b| (a,(b,c))<-temp_list] putStrLn " " mapM_ putStrLn [ show a ++ ". " ++ c| (a,(b,c))<-temp_list] where temp_list = math_practice' n x y gs' n x y = do mapM_ putStrLn [ b| (a,(b,c))<-temp_list] putStrLn " " where temp_list = math_practice' n x y {-main = do-} {-gs 10 100 999-}
使用的例子
> gs 10 10 99 1. 94 + 79 + 26= 2. 34 + 65 + 73= 3. 88 + 29 + 24= 4. 54 + 93 + 29= 5. 32 + 79 + 82= 6. 24 + 55 + 67= 7. 46 + 16 + 64= 8. 43 + 39 + 84= 9. 38 + 89 + 62= 10. 91 + 51 + 83= 1. 94 + 79 + 26=199 2. 34 + 65 + 73=172 3. 88 + 29 + 24=141 4. 54 + 93 + 29=176 5. 32 + 79 + 82=193 6. 24 + 55 + 67=146 7. 46 + 16 + 64=126 8. 43 + 39 + 84=166 9. 38 + 89 + 62=189 10. 91 + 51 + 83=225