首页 > 解决方案 > Haskell 访问类型类参数问题

问题描述

这很可能是一个简单的问题,但我找不到答案:

如何访问自定义类型的参数?

假设我的代码是这样的:(anotherFunc 只是为了帮助我访问参数)

data Shape = (Shape Color [Dimension])

func :: [Shape] -> [Shape]
func (x:xs) = anotherFunc x : func xs

anotherFunc :: [Shape] -> [Shape]
anotherFunc (Shape Color (x:xs)) = <some simple operations>

有没有类似的东西??

func ( (Shape Color (x:xs)):shapes )

很多谢谢!

标签: listhaskellrecursionparameterstypeclass

解决方案


有一些与此非常相似的东西。

func ((Shape _ (x:xs)):shapes) = ...

但是,您func只是重新实现map,因此您可以使用它并继续使用anotherFunc(如果您愿意,可以在本地定义):

func = map anotherFunc
  where anotherFunc (Shape c ds) = ...

推荐阅读