在Haskell中创建坐标类 [英] Creating a Coordinate class in Haskell

查看:136
本文介绍了在Haskell中创建坐标类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以成对的方式将两个多态元组一起添加。 (一个元组中的第一个元素的类型应该与第二个元素中的第一个元素的类型相同,同样也是第二个元素的类型)
这是我的代码:

模块Main其中

类协调一个位置

createCoordinate :: a

getFirst ::(a, b) - > a

getSecond ::(a,b) - > b

addCoordinates ::(a,b) - > (a,b)→> (a,b)

实例坐标()其中

createCoordinate =()

getFirst(a,b)= a

getSecond(a,b)= b

addCoordinates ab =(getFirst a + getFirst b,getSecond a + getSecond b)

所以,问题出在我的addCoordinates函数上。我想知道是否有人可以为我提供任何有关如何执行该功能的帮助。
谢谢! :)

解决方案

您可能想要一个数据类型,而不是类:

  data坐标ab =坐标{getFirst :: a,getSecond :: b} 
派生(等式,Ord,显示)



您的函数将会变成:

$ $ $ $ $ $ $> createCoordinate :: a - > b - >坐标b
createCoordinate a b =坐标b b
$ b addCoordinates ::(数字a,数字b)=>协调b - >协调b - >坐标ab
addCoordinates(坐标a1 b1)(坐标a2 b2)=坐标(a1 + a2)(b1 + b2)

请注意, a b 可以是任何类型,但 addCoordinates 只适用于 Num 的实例,因为我们希望应用 + 给他们。你不需要类型类来定义 Coordinate



类型类将允许你定义可以被初始化的东西例如:

  class DefaultInitializable a其中
defaultInit :: a

然后我们可以让 Int 这个类的一个实例:

  instance DefaultInitializable Int其中
defaultInit = 0

只要它的参数也是实例,我们就可以让 Coordinate 一个实例:

  instance(DefaultInitializable a,DefaultInitializable b)=> DefaultInitializable(坐标b)其中
defaultInit =坐标默认默认


I'm attempting to add two polymorphic tuples together in a pairwise manner. (The types of the first element in one tuple should be the same as the first in the second and likewise for the second element) Here's my code:

module Main where

class Coordinate a where

    createCoordinate :: a

    getFirst :: (a,b) -> a

    getSecond :: (a,b) -> b

    addCoordinates :: (a,b) -> (a,b) -> (a,b)

instance Coordinate () where

    createCoordinate = ()

    getFirst (a,b) = a

    getSecond (a,b) = b

    addCoordinates a b = (getFirst a + getFirst b, getSecond a + getSecond b)

So, the problem is with my addCoordinates function. I was wondering if anyone could offer me any help with how to go about implementing the function. Thanks! :)

解决方案

You probably want a data type, not a class:

data Coordinate a b = Coordinate { getFirst :: a, getSecond :: b }
    deriving (Eq, Ord, Show)

Your functions would then become:

createCoordinate :: a -> b -> Coordinate a b
createCoordinate a b = Coordinate a b

addCoordinates :: (Num a, Num b) => Coordinate a b -> Coordinate a b -> Coordinate a b
addCoordinates (Coordinate a1 b1) (Coordinate a2 b2) = Coordinate (a1+a2) (b1+b2)

Note that a and b can be of any type, but addCoordinates only works if they are instances of Num, because we wish to apply + to them. You do not need a typeclass to define Coordinate.

A typeclass would allow you to define things that can be initialized to default values for instance:

class DefaultInitializable a where
    defaultInit :: a

We can then make Int an instance of this class:

instance DefaultInitializable Int where
    defaultInit = 0

And we can make Coordinate an instance as long as its parameters are also instances:

instance (DefaultInitializable a, DefaultInitializable b) => DefaultInitializable (Coordinate a b) where
    defaultInit = Coordinate default default

这篇关于在Haskell中创建坐标类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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