Adr*_*ian 2 haskell parse-error
我在Haskell中编写了以下函数
coordenadas :: (Floating a) => String -> (a, a, a)
coordenadas linea = (x, y, z)
    where  (_ : xStr : yStr : zStr : _) = words linea
           x = read $ tail xStr :: Float
           y = read $ tail yStr :: Float
           z = read $ tail zStr :: Float
Run Code Online (Sandbox Code Playgroud)
这个功能是为了接收像串"N1 X2 Y1 Z10"并产生像一个元组(2, 1, 10),但是当我尝试编译,编译器说,有一个parse error on input '='在该行x = read $ tail xStr :: Float.
有谁知道如何解决它?
谢谢回答.
我搞定了:
coordinates :: String -> (Float, Float, Float)
coordinates line = (x,y,z)
    where   (_ : xStr : yStr : zStr : _) = words line
            x = read $ tail xStr :: Float
            y = read $ tail yStr :: Float
            z = read $ tail zStr :: Float
main = do
    let line = "test x1.0 y1.0 z1.0 test"
    print $ coordinates line
Run Code Online (Sandbox Code Playgroud)
这(1.0, 1.0, 1.0)按预期输出.
我自己对Haskell有点新意,所以我不知道为什么这是一个关于缩进的挑剔(并且会欣赏那些比我知道更多的人的指针!),但显然正确的方法是:
where,标签再次,然后输入的第一行(注意:在我的编辑器中,"tab"是"4个空格",而不是制表符)
编辑:我想我只是弄清楚为什么很难排在我的最后:语法高亮!我的编辑器加粗了"where",这使得它更宽,这使得正确的缩进看起来不正确.我实际上通过关闭突出显示确认了这一点,只要线条彼此对齐,它就会起作用.
这也意味着这种方式可能避免类似的问题:
coordinates :: String -> (Float, Float, Float)
coordinates line = (x,y,z)
    where 
        (_ : xStr : yStr : zStr : _) = words line
        x = read $ tail xStr :: Float
        y = read $ tail yStr :: Float
        z = read $ tail zStr :: Float
Run Code Online (Sandbox Code Playgroud)