Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any standard library analogues:

Tags:

haskell

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)
like image 937
karlicoss Avatar asked Dec 09 '22 07:12

karlicoss


1 Answers

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.

like image 127
interjay Avatar answered Dec 24 '22 11:12

interjay