I currently have a function with definition:
f :: [Int] -> Int -> [[Int]]
And have two variables of type IO [Int] and IO Int. I want to pass these variables to this function f. I am able to do this when it is just one variable being passed to the function, but when it is 2 I can't get it to work.
You can write a do block:
f' :: IO [[Int]]
f' = do
x <- val1
y <- val2
return (f x y)
with val1 :: IO [Int] and val2 :: IO Int the values you want to pass as parameters.
or in an applicative style:
f' :: IO [[Int]]
f' = f <$> val1 <*> val2
or we can make use of liftA2 :: (a -> b -> c) -> f a -> f b -> f c:
f' :: IO [[Int]]
f' = liftA2 f val1 val2
You should use <- in do notation and run the function on the unwrapped values.
-- Implementations left out
xsFn :: IO [Int]
xFn :: IO Int
main = do
xs <- xsFn
x <- xFn
-- We've unwrapped the values, so now xs :: [Int] and x :: Int
let result = f xs x -- result :: [[Int]]
-- do something with result
return ()
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