Kap*_*pol 2 haskell list-comprehension where-clause
让我们定义一个简单的函数func:
func :: [Int] -> [Int]
Run Code Online (Sandbox Code Playgroud)
我想where在定义这个虚假函数时在列表解析中使用一个子句.
func xs = [ y where y = x + 1 | x <- xs]
Run Code Online (Sandbox Code Playgroud)
不幸的是,在尝试编译时,我收到以下消息:
parse error on input `where'
Run Code Online (Sandbox Code Playgroud)
如果我决定使用该let条款,一切正常:
func xs = [ let y = x + 1 in x | x <- xs] -- compilation successful
Run Code Online (Sandbox Code Playgroud)
为什么我不能where像我原来想要的那样使用?
正如您在Haskell报告(https://www.haskell.org/onlinereport/exps.html#list-comprehensions)中所看到的那样,列表理解在左侧显示一个表达式.let是表达式,而是where顶级声明的一部分.因此,不允许那里.