Haskell - 不在范围内

9 haskell

的代码的下面片段是在创建的功能的尝试的一部分generateUpTo其生成列表pAllSorted取决于n最大,因此RMAX.

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!"
Run Code Online (Sandbox Code Playgroud)

但是,在尝试编译时,编译器会在(在此处)第1,3和4行中给出关于rmax的"不在范围内"错误.

(如何)在使用generateUpTo函数之前,我可以保留rmax undefined 吗?

Ada*_*ner 9

如果你想使用rmax里面nmax,passfail没有传递给它作为一个arguement,你需要把它列入了wheregenerateUpTo.否则,它实际上是"不在范围内".例:

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
Run Code Online (Sandbox Code Playgroud)

如果您希望在多个地方使用这些功能,您可以将rmax作为参数加入:

nmax rmax = rmax `div` 10
pass rmax = rmax `elem` mot
fail rmax = rmax `notElem` mot
Run Code Online (Sandbox Code Playgroud)

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

更新

要使用nmax(where-the-block-block范围版本),您需要将rmax的值传递给它.像这样:

nmax rmax  -- function application in Haskell is accomplished with a space,
           -- not parens, as in some other languages.
Run Code Online (Sandbox Code Playgroud)

但请注意,rmax定义中的名称nmax不再重要.这些功能都完全相同:

nmax rmax = rmax `div` 10
nmax a = a `div` 10
nmax x = x `div` 10
Run Code Online (Sandbox Code Playgroud)

同样,您不需要使用名为的值来调用它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.
Run Code Online (Sandbox Code Playgroud)