(i always forget to reply-to-all)<br><br>If you'd like to reference C functions with Strings, one possible way is to<br>use System.Posix.DynamicLinker and the wrapper over libffi that's been<br>uploaded to hackage recently:<br>
<br>[m@monire asdf]$ ghci<br>GHCi, version 6.10.1: <a href="http://www.haskell.org/ghc/">http://www.haskell.org/ghc/</a> :? for help<br>Loading package ghc-prim ... linking ... done.<br>Loading package integer ... linking ... done.<br>
Loading package base ... linking ... done.<br><br>ghci> :m + Foreign.LibFFI<br>ghci> :m + Foreign.Ptr Foreign.Storable<br>ghci> :m + Foreign.C.Types Foreign.C.String<br>ghci> :m + System.Posix.DynamicLinker<br>
<br>ghci> malloc <- dlsym Default "malloc"<br>Loading package unix-2.3.1.0 ... linking ... done.<br>ghci> syscall <- dlsym Default "syscall"<br><br>ghci> :! echo -ne "#include <syscall.h>\n__NR_execve\n" | cpp | tac | grep -E "^[0-9]+$" | head -1 > NOODLES<br>
ghci> nr_execve :: CLong <- (read . head . words) `fmap` readFile "NOODLES"<br>ghci> :! rm -f NOODLES<br><br>ghci> let sizeOfPtrCChar = sizeOf(undefined::Ptr())<br>ghci> argv <- callFFI malloc (retPtr (retPtr retCChar)) [argCSize (2*fromIntegral sizeOfPtrCChar)]<br>
Loading package bytestring-0.9.1.4 ... linking ... done.<br>Loading package libffi-0.1 ... linking ... done.<br><br>ghci> sh <- newCString "/bin/sh"<br>ghci> poke argv sh<br>ghci> poke (argv`plusPtr`sizeOfPtrCChar) 0<br>
<br>ghci> callFFI syscall retCLong [argCLong nr_execve, argPtr sh, argPtr argv, argCInt 0] {-never returns-}<br>sh-3.2$ echo $0<br>/bin/sh<br>sh-3.2$ exit<br>exit<br>[m@monire asdf]$ <br><br>Matt<br><br><div class="gmail_quote">
On Fri, May 29, 2009 at 11:41 AM, Khudyakov Alexey <span dir="ltr"><<a href="mailto:alexey.skladnoy@gmail.com">alexey.skladnoy@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On Friday 29 of May 2009 19:34:44 Patrick LeBoutillier wrote:<br>
> Hi all,<br>
><br>
> Is it possible with Haskell to call a function whose name is contained<br>
> in a String?<br>
> Something like:<br>
><br>
> five = call_func "add" [2, 3]<br>
><br>
> If not, perhaps this is acheivable using FFI?<br>
><br>
</div>Or maybe you are asking for template haskell[1]. With it you can actually<br>
generate function at compile time. It depends on waht you actually need.<br>
<br>
> {-# LANGUAGE TemplateHaskell #-}<br>
> import <a href="http://Language.Haskell.TH" target="_blank">Language.Haskell.TH</a><br>
><br>
> five = $( foldl appE (varE $ mkName "+") [ litE $ integerL 2<br>
> , litE $ integerL 3 ] )<br>
<br>
<br>
[1] <a href="http://haskell.org/haskellwiki/Template_Haskell" target="_blank">http://haskell.org/haskellwiki/Template_Haskell</a><br>
<br>
--<br>
<font color="#888888"> Khudyakov Alexey<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br>