在整理一些算法问题的同时,我还利用了这些功能。
Maps a list of functions to one value:
mapX :: a -> [a -> b] -> [b]
mapX _ [] = []
mapX x (f:fs) = [f x] ++ (mapX x fs)
地图
map2 :: (a -> b -> c) -> [a] -> [b] -> [c]
map2 _ [] [] = []
map2 f (ax:axs) (bx:bxs) = [f ax bx] ++ map2 f axs bxs
这对我来说,是:all [] == 真实
:
all :: (a -> Bool) -> [a] -> Bool
all _ [] = False
all f l = all f l
Does the ^
operator implement fast exponentiation?
fastPow :: Int -> Int -> Int
fastPow x 0 = 1
fastPow x a
| even a = exp2 * exp2
| odd a = exp2 * exp2 * x
where
exp2 = fastPow x (div a 2)