art*_*lla 3 haskell indentation
考虑以下操作Haskell代码:
代码A.
describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
where what [] = "empty."
what [x] = "a singleton list."
what xs = "a longer list."
Run Code Online (Sandbox Code Playgroud)
这是从函数中的语法中获取的片段.现在假设我们从以下一段严重缩进和不可操作的Haskell代码开始:
代码B.
describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
where what [] = "empty."
what [x] = "a singleton list."
what xs = "a longer list."
Run Code Online (Sandbox Code Playgroud)
那么有没有任何工具可以采用严重缩进的代码(代码B)并正确地缩进它以产生操作代码(代码A)?
这几乎是不可能做到的(我正在写这个作为响应,因为我一直在寻找一个可以做到这一点的工具),因为没有缩进的Haskell代码可能非常模糊.
考虑一下代码:
describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
where what [] = "empty."
what [x] = "a singleton list."
what xs = "a longer list."
Run Code Online (Sandbox Code Playgroud)
我们还要考虑一些替代的,也是有效的解释:
-- top-level function "what" without type signature
describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
where what [] = "empty."
what [x] = "a singleton list."
what xs = "a longer list."
-- same as above
describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
where what [] = "empty."
what [x] = "a singleton list."
what xs = "a longer list."
-- There might be a `data String a b`, so `String describeList xs` is
-- a type. The clause then becomes a guarded pattern match (similar to
-- `let bar :: Int = read "1"`) with scoped type variables. The `where`
-- clause is still syntactically valid. The whole thing might not compile,
-- but a syntax tool can't know that.
describeList :: [a] -> String
describeList xs = "The list is " ++ what xs
where what [] = "empty."
what [x] = "a singleton list."
what xs = "a longer list."
Run Code Online (Sandbox Code Playgroud)