Haskell树木地图

时间:2018-06-06 03:43:15

标签: haskell

public partial class Form1 : Form
{
    private Model m_modelObj;
    public Form1(Model modelObj)
    {
        InitializeComponent();

        m_modelObj = modelObj;

        List<Tabby> tabbyList = m_modelObj.TabbyList;
        List<Siamese> siameseList = m_modelObj.SiameseList;
        List<Husky> huskyList = m_modelObj.HuskyList;
        List<Chiwawa> chiwawaList = m_modelObj.ChiwawaList;

       //tree code

        //add husky list
        TreeNode node8 = null;
        foreach (var item in huskyList)
        {
            node8 = new TreeNode(item.name);       
        }

        TreeNode[] husky = new TreeNode[] { node8 };

        //add chiwawa list
        TreeNode node9 = null;
        foreach (var item in chiwawaList)
        {
            node9 = new TreeNode(item.name);
        }

        TreeNode[] chiwawa = new TreeNode[] { node9 };

        //dog breed
        TreeNode node2 = new TreeNode("Husky", husky);
        TreeNode node3 = new TreeNode("Chiwawa", chiwawa);
        TreeNode[] dog = new TreeNode[] { node2, node3 };

        //dog parent
        TreeNode treeNode = new TreeNode("Dogs", dog);
        treeView1.Nodes.Add(treeNode);

        //add tabby list
        TreeNode nodes = null;

        foreach (var item in tabbyList)
        {
            nodes = new TreeNode(item.name);
        }

        TreeNode[] tabby = new TreeNode[] { nodes };

        //add siamese list
        TreeNode node7 = null;
        foreach (var item in siameseList)
        {
            node7 = new TreeNode(item.name);
        }

        TreeNode[] siamese = new TreeNode[] { node7 };

        //cat breed
        TreeNode node4 = new TreeNode("Siamese", siamese);
        TreeNode node5 = new TreeNode("Tabby", tabby);
        TreeNode[] cat = new TreeNode[] { node4, node5 };

        //cat parent
        treeNode = new TreeNode("Cats", cat);
        treeView1.Nodes.Add(treeNode);
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void addDetailsBtn_Click(object sender, EventArgs e)
    {
        string animalType = comboBoxAnimalType.SelectedItem.ToString();
        this.Hide();
        PetInfoForm aPetInfoForm = new PetInfoForm(animalType, m_modelObj);
        aPetInfoForm.Closed += (s, args) => this.Close();
        aPetInfoForm.Show();
    }
}

如何定义一个像map一样运行但在树上工作的函数?新功能的typeignature将是:

data Tree a = Empty | Node a (Tree a) (Tree a)

map :: (a -> b) -> [a] -> [b]

甚至可以为map创建通用类型类吗?

1 个答案:

答案 0 :(得分:11)

Prelude中已存在类型类:Functor

class Functor f where
    fmap :: (a -> b) -> f a -> f b

要实现它,您可以使用DeriveFunctor语言扩展,以便GHC只需deriving子句即可为您实现:

{-# LANGUAGE DeriveFunctor #-}

data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Functor)

mapTree :: (a -> b) -> Tree a -> Tree b
mapTree = fmap

或者你可以手动实现它:

data Tree a = Empty | Node a (Tree a) (Tree a)

mapTree :: (a -> b) -> Tree a -> Tree b
mapTree _ Empty = Empty
mapTree f (Node x l r) = Node (f x) (mapTree f l) (mapTree f r)

instance Functor Tree where
    fmap = mapTree