Haskell访问类型类参数问题

问题描述 投票:-2回答:1

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

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

让我们说我的代码是这样的:(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 )

很多!!

list haskell recursion parameters typeclass
1个回答
0
投票

有一些非常相似的东西。

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

但是,您的func仅重新实现了map,因此您可以使用它并继续使用anotherFunc(可以根据需要在本地定义):

func = map anotherFunc
  where anotherFunc (Shape c ds) = ...
© www.soinside.com 2019 - 2024. All rights reserved.