I'm having trouble cause I can't define a function to do this:
Input: "(case (+ 5 5) ((4 9 1) 'd64)\\n((1 2) 'pepito)\\n((10) 'jorgito)"
Output: "(case (+ 5 5) ((4 9 1) 'd64)\n((1 2) 'pepito)\n((10) 'jorgito)"
I've tried this and many other, without success:
import Data.List.Split (splitOn)
import Data.List (intersperse)
import Data.Char
replace :: String -> String -> String -> [String]
replace current next [] = []
replace current next xs = intersperse [chr 92] $ splitOn current xs
I know the last function is not well coded looking their arguments, but I tried to do a replace function for "all strings" but I can't by now.
Then, my parser can't work cause is something like this:
parseCaseResult :: Parser LispVal
parseCaseResult = do
char '\''
first <- (letter <|> symbol) --de momento será una String
rest <- many (digit <|> letter <|> symbol)
return $ String (first:rest)
parseCasePair :: Parser CasePair
parseCasePair = do
list <- lexeme (char '(') >> (lexeme (char '(')) *> parseList <* (lexeme $ char ')')
result <- lexeme $ parseCaseResult <* char ')'
return (list, result)
parseCaseExpr :: Parser LispVal
parseCaseExpr = do
lexeme $ char '('
lexeme $ string "case"
conditional_expr <- lexeme (char '(') *> parseList <* lexeme (char ')')
-- armada en las nuevas líneas, solucionado con un parser más trabajado
lista <- sepBy parseCasePair newline -- $ try (string "\\\\\n") <|> try (string "\n") <|> string "\r"
return $ CaseExpr conditional_expr lista
New info:
Example that works in GHCi:
*Main> eval $ fromRight $ parse parseExpr "jaja" "(case (+ 5 5) ((4 9 1) 'd64\n((1 2) 'pepito\n((10) 'jorgito)"
10 (4 9 1)
10 (1 2)
10 (10)
Right "jorgito"
But if I compile and run the main
function, the code does not work I think because the parser doesn't match those \\n
.
Sorry for the bad explanation in earlier versions of this question.