Question

I have studied some Haskell programming language and now I found out that it's possible to call Haskell functions from C programs. During my Haskell studies, I created a word frequency counter with Haskell and I would like to try to call that function from a C program, but I don't know how to do it. I found these two websites on haskell.org:

Calling Haskell from C

Foreign C types (Haskell module)

Despite that, I'm a bit lost which types to use. My haskell programs is a pipeline of following functions:

putStr . unlines . map testF . sortedTree

where my own functions

  • testF is type of testF :: Show a => ([Char],a) -> [Char]
  • sortedTree is type of sortedTree :: (Num a, Ord a) => [Char] -> [([Char],a)]

I'm quite sure that I need to convert the types of each function to C types, instead of converting just the function that calls the pipeline. The type of the "main" function is

fileFreq :: [Char] -> IO ()

Besides all this, I'm using a Haskell binary tree, that isn't prelude type.

Here is the whole Haskell code:

module WordCounter where

import List
import Char
import Foreign.C.Types

data BTree a = Tip | BNode a (BTree a) (BTree a) deriving Show

insertFreq x Tip = BNode (x,1) Tip Tip
insertFreq x (BNode (q,p) l r)  | (map toLower x)==(map toLower q)  = BNode (q, p+1) l r
                | otherwise                 = BNode (q,p) l (insertFreq x r)

tlist :: BTree a -> [a]
tlist Tip = []
tlist (BNode x l r) = concat [tlist l, [x], tlist r]

sortedTree x = sortBy (\(x,y) (p,q) -> compare q y) (tlist (foldr insertFreq Tip (words x)))

testF (x, n) = concat (x : ":" : " \t\t\t " : show n : [])

concord = putStr . unlines . map testF . sortedTree

fileFreq filename = do { text <- readFile filename; concord text }

Can anyone guide me a bit with this?

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top