From: Marcin 'Qrczak' Kowalczyk Date: 2002-08-26T19:11:44+09:00 Subject: Re: A Repeat: New Language After Ruby? Fri, 23 Aug 2002 05:57:05 +0900, William Djaja Tjokroaminata pisze: > That's why I really hesitate to start learning Haskell. It seems > Haskell can only be interfaced with C through some intermediary > called "Greed Card" and not directly as between C and Ruby. You don't have to use GreenCard (which is kind of obsolete). You can write glue code by hand; most of glue is on the Haskell side. The language offers the ability to call C functions directly from Haskell, provided that you have arguments ready in Haskell types corresponding to C types: foreign import "fnmatch" unsafe fnmatchC :: Ptr CChar -> Ptr CChar -> CInt -> IO CInt and offers a library for converting basic types and managing C memory. Here is how fnmatch might be wrapped in Haskell: pattern and string are Haskell strings converted to Ptr CChar values pointing to temporary buffers, and flags is a list of values instead of a bitmask: fnmatch :: String -> String -> [MatchFlag] -> Bool fnmatch pat str flags = ( unsafePerformIO $ withCString pat $ \patPtr -> withCString str $ \strPtr -> fnmatchC patPtr strPtr flagsInt) /= 0 where flagsInt = (flag FnmFileName (#const FNM_FILE_NAME)) .|. (flag FnmPeriod (#const FNM_PERIOD)) .|. (flag FnmNoEscape (#const FNM_NOESCAPE)) .|. (flag FnmLeadingDir (#const FNM_LEADING_DIR)) .|. (flag FnmCaseFold (#const FNM_CASEFOLD)) flag cons bit = if cons `elem` flags then bit else 0 The #const construct is not real Haskell and this file must be run through hsc2hs preprocessor which replaces them with integer literals corresponding to these C constants. You can convert Haskell functions to C function pointers (requires nonportable magic from the Haskell compiler to build code on the heap) and directly access C variables (i.e. obtain the pointer to the variable from Haskell and use Haskell functions to read and write through it). -- __("< Marcin Kowalczyk \__/ qrczak@knm.org.pl ^^ http://qrnik.knm.org.pl/~qrczak/