2
votes

I try to use haskell function in my Python program using FFI.

My function is like f :: String -> String

anyone can help me?

i have another function f2 :: [(Double,Double,Double)] -> ((Double,Double,Double),(Double,Double,Double))

edit:

i found some information here: https://github.com/nh2/call-haskell-from-anything

i know how to call function like fib :: Int -> Int in Python

ex.

module Example where

import Foreign.C.Types

fibonacci :: Int -> Int
fibonacci n = fibs !! n
    where fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

fibonacci_hs :: CInt -> CInt
fibonacci_hs = fromIntegral . fibonacci . fromIntegral

foreign export ccall fibonacci_hs :: CInt -> CInt

but i don't know how to make funciton :: String -> String callable in Python

2
What kind of FFI? How do you do it?Niklas B.
A first step is to change the list into a contiguous data type such as Vector Double and pass a pointer and length.gspr
@gspr: I think the library that OP is using works on a slightly higher level of abstraction, so that a list is probably easier to handleNiklas B.
Some of these functions might be helpful: haskell.org/ghc/docs/latest/html/libraries/base/…Neil Forrester

2 Answers

1
votes

Just as you need to wrap your fib function on Int to a function on CInt, you similarly need to wrap your function on String as a function on CString: http://www.haskell.org/ghc/docs/latest/html/libraries/base/Foreign-C-String.html

1
votes

Here are some options for calling Haskell from Python

There is a open source project HaPy which provides a nice and easy to use binding. Char and String Haskell types are supported here.

There is also the ctypes.cdll.LoadLibrary method with an example here. You will need to incorporate sclv's answer with CString.

See this answer for an example on how to use CString