este código:Haskell "donde" sangría: ¿por qué debe ser indentado pasado identificador?
import Data.Char (digitToInt)
myInt :: String -> Int
myInt [] = error "bad input: empty string"
myInt (x:xs)
| x == '-' = -1 * myInt xs
| otherwise = foldl convert 0 (x:xs)
where convert acc x
| x `elem` ['0'..'9'] = 10 * acc + digitToInt x
| otherwise = error ("bad input: not an int - " ++ [x])
falla:
Prelude> :l safeListFs.hs
[1 of 1] Compiling Main (safeListFs.hs, interpreted)
safeListFs.hs:9:8: parse error (possibly incorrect indentation)
Failed, modules loaded: none.
Pero esta versión:
import Data.Char (digitToInt)
myInt :: String -> Int
myInt [] = error "bad input: empty string"
myInt (x:xs)
| x == '-' = -1 * myInt xs
| otherwise = foldl convert 0 (x:xs)
where convert acc x
| x `elem` ['0'..'9'] = 10 * acc + digitToInt x
| otherwise = error ("bad input: not an int - " ++ [x])
está bien:
Prelude> :l safeListFs.hs
[1 of 1] Compiling Main (safeListFs.hs, interpreted)
Ok, modules loaded: Main.
no puedo entender por qué los dos últimos en las abolladuras importan
Esta pregunta es un buen ejemplo de por qué odio a la sintaxis de Haskell espacios en blanco; siempre me parece poco intuitivo en comparación con, digamos, Python. Lamentablemente, lo único que no me gusta * más * son los parches rizados que ensucian mi código. –
http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html#deftypes.offside La regla de fuera de juego me parece intuitiva. Solo tienes que dejar de pensar en bloques (como Python, que no tiene sentido en Haskell) y pensar en cambio en la continuación de una declaración o expresión. – ephemient
Use un editor de texto inteligente y olvídese de las reglas de identación raras. –