开发者

Are there any standard library analogues:

While coding some algorithm problems, I've used these functions, and I wonder if there are any standard library analogues implementing their functionality:

Maps a list of functions to one value:

mapX :: a -> [a -> b] -> [b]
mapX _ [] = []
mapX x (f:fs) = [f x] ++ (mapX x fs)

Maps a binary function to two lists:

map2 :: (a -> b -> c) -> [a] -> [b] -> [c]
map2 _ [] [] = []
map2 f (ax:axs) (bx:bxs) = [f ax bx] ++ map2 f axs bxs

To me, it's kinda weird that all [] =开发者_运维百科= True :(

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)


Maps a list of functions to one value:

map ($x) fs

$ is the function application operator, so ($x) is a function that applies its argument to x.

Maps a binary function to two lists:

zipWith. You can find this even if you don't know the function name by searching for the type signature on Hoogle.

Does ^ operator implement fast exponentiation?

Yes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜