English 中文(简体)
1. 功能模式-配对问题
原标题:Haskell function pattern-matching issues
  • 时间:2009-09-01 22:50:47
  •  标签:

我做一些家务劳动,虽然我对SML有一些经验,但Haskell有一些困难。 考虑这一简单职能:

type Pos = (Int, Int)
data Move = North | South | East | West
move :: Move -> Pos -> Pos
move North (x,y) = (x, y+1)
move South (x,y) = (x, y-1)
move East  (x,y) = (x+1, y)
move West  (x,y) = (x-1, y)

moves :: [Move] -> Pos -> Pos
moves (m:ms) (x,y) = moves ms (move m (x,y))
moves [] p = p

该法典行之有效。 但是,如果我用简单的<代码>p<>p(x,y)(I dont use anyway)脱掉了援引(声明的功课):

moves :: [Move] -> Pos -> Pos
moves (m:ms) p = moves ms (move m p)
moves [] p = p

*Main> let p = (1,1) :: Pos
*Main> move [North, North] p

<interactive>:1:5:
    Couldn t match expected type `Move  against inferred type `[a] 
    In the first argument of `move , namely `[North, North] 
    In the expression: move [North, North] p
    In the definition of `it : it = move [North, North] p

这对我来说似乎有些奇怪,因为第二个参数在定义中已经被归类为Pos,因此如何出现这种争cho,而只是援引? I m 采用gh。

最佳回答

你在行动结束时忘记了“什么”?

*Main> move [North, North] p
问题回答

暂无回答




相关问题
热门标签