显示多态类型的函数 [英] Show function for polymorphic type

查看:128
本文介绍了显示多态类型的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为多态树类型定义Show函数。任何人都可以帮助我吗?

  import char 

数据树t =
NilT |
Node t(Tree t)(Tree t)

class Mar t其中
maior :: t - >字符串

实例Mar Tree其中
maior(NilT)=a
maior(节点标签)=b

实例Show Tree where
show = maior

非常感谢!






解决方案(由ivanm提供):

  import Char 

数据树t =
NilT |
Node t(Tree t)(Tree t)

class Mar t其中
maior :: t - >字符串

实例Mar(Tree t)其中
maior(NilT)=a
maior(节点标签)=b

实例Show(Tree t)where
show = maior


解决方案

是否有任何特殊原因导致您没有使用派生Show Show Read 类旨在提供真正基本的序列化/反序列化,它通常会生成有效的Haskell代码。 / p>

但是对于你想要的,我认为这个错误会回到你的 Mar 类中。按照定义,该实例是用于 * - > * (例如 Maybe 而不是 Maybe Int )。你可能意思是有实例Mar(Tree t)其中...


I'm trying to define the Show function for the polymorphic Tree type. Could anyone help me?

import Char

data Tree t =
    NilT |
    Node t (Tree t) (Tree t)

class Mar t where
    maior :: t -> String

instance Mar Tree where
    maior (NilT) = "a" 
    maior (Node t a b) = "b"

instance Show Tree where
    show = maior

Thanks a Lot!


Solution (given by ivanm):

import Char

data Tree t =
    NilT |
    Node t (Tree t) (Tree t)

class Mar t where
    maior :: t -> String

instance Mar (Tree t) where
    maior (NilT) = "a" 
    maior (Node t a b) = "b"

instance Show (Tree t) where
    show = maior

解决方案

Is there any particular reason that you aren't using deriving Show? The Show and Read classes are meant to provide really basic serialisation/deserialisation which (usually) produce valid Haskell code.

But for what you want, I think the error is going back to your Mar class. As defined, the instance is for kind * -> * (e.g. Maybe as opposed to Maybe Int). What you probably meant is to have instance Mar (Tree t) where ....

这篇关于显示多态类型的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆