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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With