Skat Engine und AI auf Haskell Basis
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 line
897B

  1. module Utils where
  2. import System.Random
  3. import Text.Read
  4. shuffle :: StdGen -> [a] -> [a]
  5. shuffle g xs = shuffle' (randoms g) xs
  6. shuffle' :: [Int] -> [a] -> [a]
  7. shuffle' _ [] = []
  8. shuffle' (i:is) xs = let (firsts, rest) = splitAt (1 + i `mod` length xs) xs
  9. in (last firsts) : shuffle' is (init firsts ++ rest)
  10. chunksOf :: Int -> [a] -> [[a]]
  11. chunksOf n [] = []
  12. chunksOf n xs = take n xs : chunksOf n (drop n xs)
  13. query :: Read a => String -> IO a
  14. query s = do
  15. putStrLn s
  16. l <- fmap readMaybe getLine
  17. case l of
  18. Just x -> return x
  19. Nothing -> query s
  20. remove :: (a -> Bool) -> [a] -> (a, [a])
  21. remove pred xs = foldr f (undefined, []) xs
  22. where f c (old, cs) = if pred c then (c, cs) else (old, c : cs)
  23. filterMap :: (a -> Bool) -> (a -> b) -> [a] -> [b]
  24. filterMap pred f as = foldr g [] as
  25. where g a bs = if pred a then f a : bs else bs