Modify Tupla from Haskell

0

How can I modify a tuple in a txt file from Haskell ?

I have a file .hs where you have the example below, what I want to know is how to add another tuple to the file from WinGhci .

Example:

alumnos :: [(Integer,[Char],Integer,Double,Double,Double,Double )]

alumnos = [
    (16320100, "Alex", 20, 24.23, 45.21, 26.16, 60.90),
    (16320101, "Emy", 21, 59.23, 12.55, 61.97, 85.04)
]
    
asked by Braiian Hdez 15.05.2017 в 21:07
source

1 answer

2

It's a pretty craft solution (not to say horrible), but it should work as long as you do not change the format of students in the file.

Clarification: students is a list, not a tuple. Therefore the question is wrongly formulated because you do not want to modify any tuple, but the list.

Now yes, hands-on:

import Data.List

main =
  do text <- readFile "Nombre del archivo" -- Leemos todo el texto que hay en el archivo. Acá hay que reemplazar Nombre del archivo por tu archivo, incluyendo la extensión .hs.
     let text' = take (length text - 1) text -- Estamos dejando de lado el corchete que cierra a la lista.
     let text'' = text' ++ "Tu tupla" -- Agregamos los datos nuevos.
     writeFile "Nombre del archivo" text''

The only thing that raises my doubts is whether writeFile adds text to the end of the file or writes from the beginning, stepping on what was before, but it should work. Anything does not bother me that you make it notice. Greetings!

    
answered by 09.05.2018 в 17:45