[Haskell-beginners] FFI, Foreign.Marshal.Array, etc.

Alexey Beshenov al at beshenov.ru
Wed Nov 26 17:16:17 EST 2008


On Wednesday 26 November 2008 23:32:58 Alexey Beshenov wrote:
> Hi!
>
> I'm playing with the FFI.
> Could anyone help me with a practical example?
>
> I have a function
>
>   int solve_sys (double **a, int n, double b[]);
>
> where a is an array of size n*n and b is an array of size n
> (it contains the needed result if solve_sys returns 0).
>
> How could I wrap it in something like
>
>   solveSys :: [[Double]] -> [Double] -> [Double]
>
> ?

Something like this:

foreign import ccall "linear.h solve_sys" lSolve :: Ptr (Double) -> 
Int -> Ptr (Double) -> IO (Int)

solveSys :: [[Double]] -> [Double] -> IO()
solveSys a b = do
                  aptr <- newArray (concat a)
                  bptr <- newArray b
                  sol <- lSolve aptr n bptr
                  x <- peekArray n bptr
                  free aptr
                  free bptr
                  print x
               where n = length a

Does the trick, but I wonder is there a way to get a function 
returning [Double]...

-- 
Setting Orange, Aftermath 38 YOLD 3174
Alexey Beshenov  http://beshenov.ru/



More information about the Beginners mailing list