使用haskell中的where子句导致错误?

时间:2013-10-14 19:13:43

标签: haskell

我是haskell的新手,试图完成一项小任务

get1th ( a , _, _ , _) = a
foo input = 
    where input = (a:_,b:_,c:_,d:_)
    if ( length (get1th input) == 2 ) 
        then permutations[2]
        else permutations[3]

我收到错误说

 parse error on input `where'

请给我一个提示

2 个答案:

答案 0 :(得分:3)

where子句必须写在最后:

foo input = 
    if ( length (get1th input) == 2 ) 
        then permutations[2]
        else permutations[3]
    where (a:_,b:_,c:_,d:_) = input

已更新

还需要交换到(a:_,b:_,c:_,d:_) = input,原因 - 我们要提取值,但不要重新定义input

答案 1 :(得分:1)

正如@wit指出的那样,where应该在表达式的末尾使用。为什么?这是因为:

  1. 它不是let;
  2. 因为它与函数的所有先前上下文(块)相关。
  3. 如果要正面定义别名,则应使用let表达式。

    有关它们的差异和优点的更多信息,请参阅Let vs. Where