[Haskell-beginners] my fist program
Ezequiel Hernan Di Giorgi
hernan.digiorgi at gmail.com
Sun Dec 2 13:43:46 CET 2012
Hola! Hallo!
I made my first program to understand lists and something about datat
types. I have some question about the following code...
*
import* Data.List
*data* Student = Student { id :: Int, nombre ::
String } *deriving* (*Show*, *Eq*)
main :: IO ()main = *do* putStrLn "---\nStudent Magnament
Program\n---" loop [] ashow :: [Student] -> IO ()ashow []
= putStrLn "Empty list"ashow [x] = putStrLn $ show xashow (x:xs)=
*do* putStrLn $ show x ashow xs aadd :: [Student] ->
IO ()aadd xs = *do* putStrLn "Enter the id" id <- getLine
putStrLn "Enter name" name <- getLine
loop $ (Student (read id) (read name) ) : xs aremove ::
[Student] -> IO ()aremove xs = *do* putStrLn "Enter the id"
id <- getLine putStrLn "Enter name"
name <- getLine loop $ delete (Student (read id) (read
name) ) xs loop :: [Student] -> IO ()loop xs = *do*
putStr $ "\n-------\nSelect an option\n" ++ "1)
Add student\n" ++ "2) Remove student\n"
++ "3) Show all students\n" ++
"4) Exit\n" ++ "\t Your option: "
option <- getLine putStrLn "\n------" *case*
read option *of* 1 -> aadd xs 2 ->
aremove xs 3 -> *do* ashow xs
loop xs 4 -> putStrLn "God bye!"
My question are:
- When i ask for the data fof a student i repeat the code, cause i have
to use fuctions using IO (), cuase i have to put something in the
screen. Therefore i cant have a fuction that only return a Student.
- loop $ (Student $ read id $ read name), is wrong why?
- i think that there are best way to achive that i have done. Or is ok
for a begginer that doesn't know functors or monads.
Sorry for my bad english, i am Argentinean.
Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20121202/d1ec38d7/attachment.htm>
More information about the Beginners
mailing list