cae*_*ar_ 15 syntax haskell where-clause guard-clause function-definition
我试图在一个函数中写3-4 where语句但我得到错误并且无法做到,我试图做类似的事情:
foo x=
| x == foo1 = 5
| x == foo2 =3
| x == foo3 =1
| otherwise =2
where foo1= samplefunct1 x
foo2= samplefunct2 x
foo3= samplefunct3 x
Run Code Online (Sandbox Code Playgroud)
我知道代码有点无用,但我只是写了这个来举例说明我的意思.
有没有人可以帮助我?提前致谢.
gsp*_*spr 23
删除=
后面foo x
并缩进你的代码,如
foo x
| x == foo1 = 5
| x == foo2 =3
| x == foo3 =1
| otherwise =2
where foo1 = samplefunct1 x
foo2 = samplefunct2 x
foo3 = samplefunct3 x
Run Code Online (Sandbox Code Playgroud)
你很好
Wil*_*ess 10
如果你的缩进有点不均匀,像这样:
foo x
| x == foo1 = 5
| x == foo2 =3
| x == foo3 =1
| otherwise =2
where foo1= samplefunct1 x
foo2= samplefunct2 x
foo3= samplefunct3 x
Run Code Online (Sandbox Code Playgroud)
确实,错误消息谈到了意外=
(并且将来,请在问题正文中包含完整的错误消息).
您可以通过重新对齐或使用{ ; }
以下方法修复此错误:
foo x
| x == foo1 = 5
| x == foo2 =3
| x == foo3 =1
| otherwise =2
where { foo1= samplefunct1 x ;
foo2= samplefunct2 x ;
foo3= samplefunct3 x }
Run Code Online (Sandbox Code Playgroud)
运行良好(不是它是一个很好的风格使用).有时它甚至看起来对你来说,但不是,如果在白色空间中隐藏了一些制表符.
这段代码几乎是正确的,你只需要正确的缩进:空格在haskell中很重要.此外,使用=
after foo
是一个错误与警卫,所以你也必须删除它.结果是:
foo x
| x == foo1 = 5
| x == foo2 =3
| x == foo3 =1
| otherwise =2
where foo1= whatever1 x
foo2= whatever2 x
foo3= whatever3 x
Run Code Online (Sandbox Code Playgroud)