R 从Mathematica调用Haskell DLL

R 从Mathematica调用Haskell DLL,r,haskell,dll,wolfram-mathematica,R,Haskell,Dll,Wolfram Mathematica,在寻找从Mathematica调用Haskell DLL的方法时,我偶然发现了以下内容,对于GNU R: (对我来说)这是一个很好的例子,我通过这个咒语使它在Mathematica上工作,而无需修改: Needs["NETLink`"] hsStart = DefineDLLFunction["HsStart", "c:\\temp\\SumRoots.dll", "void", {}]; hsEnd = DefineDLLFunction["HsEnd", "c:\\temp\\SumRoo

在寻找从Mathematica调用Haskell DLL的方法时,我偶然发现了以下内容,对于GNU R:

(对我来说)这是一个很好的例子,我通过这个咒语使它在Mathematica上工作,而无需修改:

Needs["NETLink`"]
hsStart = DefineDLLFunction["HsStart", "c:\\temp\\SumRoots.dll", "void", {}];
hsEnd = DefineDLLFunction["HsEnd", "c:\\temp\\SumRoots.dll", "void", {}];
sroot = DefineDLLFunction["sumRootsR", "c:\\temp\\SumRoots.dll", "double", {"int*", "double[]", "double*"}];
hsStart[];
resulta = 0;
lista = {9, 3.5, 5.58, 64.1, 12.54};
sroot[Length[lista], lista, resulta];
resulta
18.7805
hsEnd[];
然后我尝试修改示例以返回一个双精度向量,而不是单个值:
Haskell中的代码:

-- SumRoots.hs

{-# LANGUAGE ForeignFunctionInterface #-}
module SumRoots where

import Foreign

foreign export ccall acumSumR :: Ptr Int -> Ptr Double -> Ptr Double -> IO ()

acumSum :: [Double] -> [Double]
acumSum xs = scanl (\x y -> x+y) 0 xs

acumSumR :: Ptr Int -> Ptr Double -> Ptr Double -> IO ()
acumSumR n xs result = do
                       n <- peek n
                       xs <- peekArray n xs
                       pokeArray result $ acumSum xs
Mathematica中的代码:

Needs["NETLink`"]
hsStart = DefineDLLFunction["HsStart", "c:\\temp\\SumRoots.dll", "void", {}];
hsEnd = DefineDLLFunction["HsEnd", "c:\\temp\\SumRoots.dll", "void", {}];
acums = DefineDLLFunction["acumSumR", "c:\\temp\\SumRoots.dll", "double[]", {"int*", "double[]", "double[]"}];
hsStart[];
lista = {9, 3.5, 5.58, 64.1, 12.54};
NETBlock@Module[{n, resultb}, n = Length[lista]+1; 
  resultb = NETNew["System.Double[]", n]; acums[n, lista, resultb]; 
  NETObjectToExpression[resultb]]

{0., 0., 0., 0., 0., 0.}
结果应该是{0.0,9.0,12.5,18.08,82.18,94.72}。我不确定这个问题是否属于Mathematica或Haskell领域,但这里的帖子基于GNU R调用有效的事实:

dyn.load("C:/temp/SumRoots.dll") 
.C("HsStart")
acumSum <- function(input)
{
return(.C("acumSumR", n=as.integer(length(input)), xs=as.double(input), result=as.double(rep(0,length(input)+1)))$result)
}
input <- c(9,3.5,5.58,64.1,12.54)
acumSum(input)
动态加载(“C:/temp/sumrots.dll”) .C(“HsStart”)
acumSum建议您访问mathematica.stackexchange.com。谢谢您的建议。这是一个链接,以防任何人需要找到它。。
dyn.load("C:/temp/SumRoots.dll") 
.C("HsStart")
acumSum <- function(input)
{
return(.C("acumSumR", n=as.integer(length(input)), xs=as.double(input), result=as.double(rep(0,length(input)+1)))$result)
}
input <- c(9,3.5,5.58,64.1,12.54)
acumSum(input)