Hi,

In these cases, I write the explicitly recursive version (e.g. getList1) and look for a pattern (e.g. getList2).

getList1 :: Ptr List -> IO [Ptr Item]
getList1 l = c_get_first_item l >>= go
   where
      go e
         | e == nullPtr = return []
         | otherwise    = do
               next <- c_get_next_item l e
               es   <- go next
               return (e:es)



getList2 :: Ptr List -> IO [Ptr Item]
getList2 l = c_get_first_item l >>= unfoldrM go
   where
      go e
         | e == nullPtr = return Nothing
         | otherwise    = do
               next <- c_get_next_item l e
               return (Just (e,next))

I hope it helps
Sylvain



2016-02-11 12:02 GMT+01:00 PICCA Frederic-Emmanuel <frederic-emmanuel.picca@synchrotron-soleil.fr>:
Hello,

I am playing with FFI and I need to extract from a  C API a list of pointers.

I have two methods available

c_get_first_item :: Ptr List -> IO (Ptr Item)
c_get_next_item :: Ptr List -> Ptr Item -> IO (Ptr Item)

I would like to obtain a [Ptr Item]

I try to used whileM but I did not find how to inject the first item in the loop.

whileM (return . ( /= nullPtr)) (c_get_next_item list item)

the c_get_next_item return a nullPtr when there is no remaining item.

what is the haskell way in order to extract a list of pointer using these C methods ?

thanks for your help

Frederic
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners