# Pastebin uckM5m9r -- | allocates a string of size 'n' of repeating @'a'@ characters createStuff :: CInt -- ^ n -> IO (Maybe (ForeignPtr CChar)) createStuff n = do traverse (newForeignPtr p_free) . mf =<< c_makeStuff n where mf p | p == nullPtr = Nothing | otherwise = Just p main = do -- Let's read a number from input... putStrLn "Enter a nonnegative integer: " n <- read <$> getLine -- use this number with the C function a couple times traverse f =<< createStuff n traverse f =<< createStuff (n * 2) -- note that f does not deallocate its argument -- that is done automatically by the GC later -- withForeignPtr is used to "downgrade" ForeignPtr to a Ptr where f fPtr = withForeignPtr fPtr (\p -> peekCString p >>= putStrLn)