[Haskell-cafe] trees and pointers

Sergey Mironov ierton at gmail.com
Thu Jul 15 05:28:01 EDT 2010


2010/7/15 Serguey Zefirov <sergueyz at gmail.com>:
> 2010/7/14 Sergey Mironov <ierton at gmail.com>:
>> Hi cafe! I have a question of C-to-Haskell type:)
>>
>> Imagine web application wich allows users to browse some shared
>> filesystem located at the server.
>> Application stores every users's position within that filesystem
>> (current directory or file).
>>
>> In C this can be implemented with the help of following data types:
>>
>> Any ideas?
>
> Use IORef. ;)
>
> PS
> MVar is better, actually.
>

Somehow I forgot about them:) Code will turn into something like

data TreeNodeData = File | Dir (IORef TreeNode)
data TreeNode = TreeNode {
        next :: Maybe (IORef TreeNode),
        prev :: Maybe (IORef TreeNode),
        up :: Maybe (IORef TreeNode), -- missed it in original C example
        payload :: TreeNodeData
	}

data User = User {
	position :: IORef TreeNode,
	-- ...
	}

It really should work! (we don't take multithreading issues into
account for now)
Slightly annoying thing is that 1-to-1 mapping from C to Haskell also
forces programmer to perform C-like low-level pointer linking.

-- 
Thanks,
Sergey


More information about the Haskell-Cafe mailing list