2012-01-01 88 views
9

代码的下面片段是在创建函数generateUpTo其生成列表pAllSorted取决于n最大因此RMAX尝试的一部分。Haskell中 - 在范围上并不

nmax = rmax `div` 10 

pass = rmax `elem` mot 
fail = rmax `notElem` mot 

generateUpTo rmax = check rmax 
where 
     check pass = pAllSorted 
     check fail = error "insert multiple of 10!" 

但是,试图编译时,编译器为约RMAX一个“不在范围内”的错误(什么是这里)行1,3和4

(如何)我可以离开rmax undefined直到使用generateUpTo函数?

+7

对于一直投票Haskell初学者问题的人来说,如果你能知道,请留下评论,告诉你为什么要这样做。 – Phyx 2012-01-01 16:49:38

+1

我们会外出给他投票的! :)更严重的是,对于那些问我“为我工作”的作业问题的人来说,存在着很多的滥用,也许这是一个错误。 – gatoatigrado 2012-01-01 22:41:13

+1

@Phyx事实上,我尽量不要投新手问题,但也许我可以解释downvoter。这些问题中的大部分都是“过于本地化”的用语,更一般地说,这些用语并不有趣。 – 2012-01-01 23:40:04

回答

9

如果你想使用rmaxnmaxpassfail没有把它当作一个arguement,你需要把它列入的generateUpTowhere块。否则,它的字面意思是“不在范围内”。例如:

generateUpTo rmax = check rmax 
    where 
     check pass = pAllSorted 
     check fail = error "insert multiple of 10!" 
     nmax = rmax `div` 10 
     pass = rmax `elem` mot 
     fail = rmax `notElem` mot 

如果你想在多个地方使用这些功能,你可以只accect RMAX作为arguement:

nmax rmax = rmax `div` 10 
pass rmax = rmax `elem` mot 
fail rmax = rmax `notElem` mot 

注意 - 它看起来像你也有一些问题,你的定义check ... passfail值只有check的争论,而不是你上面定义的函数。

更新

使用n最大(外最,其中块范围的版本),你需要RMAX的值传递给它。像这样:

nmax rmax -- function application in Haskell is accomplished with a space, 
      -- not parens, as in some other languages. 

但是请注意,在nmax定义名称rmax不再显著。这些功能都是完全一样的:

nmax rmax = rmax `div` 10 
nmax a = a `div` 10 
nmax x = x `div` 10 

同样,你不需要有一个名为rmax值来调用它。

nmax rmax 
nmax 10 -- this is the same, assuming rmax is 10 
nmax foo -- this is the same, assuming foo has your 'rmax' value. 
+0

如果在其他地方定义** pAllsorted **,并且在该定义中使用** nmax **,那么如果我使用此方法解决问题,** nmax **是否仍然在范围内? – 2012-01-01 16:47:04

+0

在我的第二个例子中......是的,这应该在'where'块之外工作。要调用它,你需要传递rmax的值。例如:'nmax 10'或'nmax myRmaxVal' – 2012-01-01 16:49:15

+0

您能澄清一下如何将rmax的值传递给它吗? – 2012-01-01 16:53:19

4

只要把的nmaxpassfail定义成generateUpTowhere条款,就像你check一样。

2
nmax rmax = rmax `div` 10 

pass rmax = rmax `elem` mot 
fail rmax = rmax `notElem` mot 

generateUpTo rmax = check rmax 
where 
    check pass = pAllSorted 
    check fail = error "insert multiple of 10!" 

的Rmax为函数参数则在声明它的功能之外未定义的。在这个例子中,函数nmax中的rmax与generateUpTo中的rmax完全无关。