Skip to main content
 首页 » 编程设计

python之Python FFI 中的 Haskell 函数

2025年05月04日752php

我尝试在使用 FFI 的 Python 程序中使用 haskell 函数。

我的函数就像 f::String -> String

谁能帮帮我?

我有另一个函数 f2::[(Double,Double,Double)] -> ((Double,Double,Double),(Double,Double,Double))

编辑:

我在这里找到了一些信息:https://github.com/nh2/call-haskell-from-anything

我知道如何在 Python 中调用类似 fib::Int -> Int 的函数

例如

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 

但我不知道如何在 Python 中使 funciton::String -> String 可调用

请您参考如下方法:

正如您需要将 Int 上的 fib 函数包装到 CInt 上的函数一样,您同样需要将函数包装在 String 上作为 CString 的函数:http://www.haskell.org/ghc/docs/latest/html/libraries/base/Foreign-C-String.html