Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: my fist program (Chadda? Fouch?)
2. Precedence of type annotation operator ::
(Jos? Romildo Malaquias)
3. Re: Precedence of type annotation operator :: (Daniel Fischer)
----------------------------------------------------------------------
Message: 1
Date: Sun, 2 Dec 2012 15:32:43 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] my fist program
To: Ezequiel Hernan Di Giorgi <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
<CANfjZRYwj0cJRtn4Zp-xKLidfZH+if6sMe=txlj-v5jmcqn...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
Well there's plenty in you code that can be improved, first I would
write this function :
> prompt :: (Read a) => String -> IO a
> prompt str = do
> putStr str
> hFlush stdout
> fmap read getLine
The hFlush is there to avoid buffering problems (this is not Haskell specific).
Then your aadd function become :
> aadd xs = do
> id <- prompt "Enter the id : "
> name <- prompt "Enter name : "
> loop $ (Student id name) : xs
But then you'll still have a duplicated section in aremove, why not
abstracting the Student selection :
> studentPrompt :: IO Student
> studentPrompt = do
> id <- prompt "Enter the id : "
> name <- prompt "Enter name : "
> return (Student id name)
Just to give you a taste, you could also write this as :
> studentPrompt = Student <$> prompt "Enter the id : " <*> prompt "Enter the
> name : "
using the Applicative typeclass (you'll probably see this later).
Then aadd becomes :
> aadd xs = do
> student <- studentPrompt
> loop (student : xs)
But writing a function for two lines and especially a function that's
mixing program logic into it (the call to loop) is probably both
unnecessary and confusing for someone reading your program (you should
always strive to localize the interactive program logic and keep the
rest pure or at least general purpose functions). So instead let's
inline it in the loop :
> loop studentList = do -- xs is a good list name when you don't know what's in
> it
> option <- prompt programOptions
> newStudentList <- case option of
> 1 -> fmap (: studentList) studentPrompt
> 2 -> fmap (`delete` studentList) studentPrompt
> 3 -> do ashow studentList; return studentList
> 4 -> do putStrLn "Good Bye !"; exitSuccess
> _ -> do putStrLn "Unknown option !"; return studentList
> loop newStudentList
--
Jeda?
On Sun, Dec 2, 2012 at 1:43 PM, Ezequiel Hernan Di Giorgi
<[email protected]> wrote:
> 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 x
> ashow (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.
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 2
Date: Sun, 2 Dec 2012 22:46:58 -0200
From: Jos? Romildo Malaquias <[email protected]>
Subject: [Haskell-beginners] Precedence of type annotation operator ::
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hello.
What is the exact precedence of the type annotation binary opertor :: ?
Romildo
------------------------------
Message: 3
Date: Mon, 03 Dec 2012 02:23:54 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Precedence of type annotation
operator ::
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
On Sonntag, 2. Dezember 2012, 22:46:58, Jos? Romildo Malaquias wrote:
> Hello.
>
> What is the exact precedence of the type annotation binary opertor :: ?
It's not really a binary operator.
It's part of the syntax, so it has no exact precedence, but since you're
asking about it, I presume you're not interested in type declarations
foo :: Int -> Double
foo = sin . fromIntegral
but rather in expression type signatures. The production in the context-free
syntax is
exp ? infixexp :: [context =>] type
so the signature is for the entire infix expression:
Prelude> toEnum . floor $ 12.7 + toEnum 73 :: Char
'U'
hence if it had a precedence, it would be below 0 (the precedence of ($)).
But be aware that
"The grammar is ambiguous regarding the extent of lambda abstractions, let
expressions, and conditionals. The ambiguity is resolved by the meta-rule that
each of these constructs extends as far to the right as possible."
thus
Prelude> (\x -> x + x :: Int -> Int) 2
<interactive>:16:10:
No instance for (Num (Int -> Int)) arising from a use of `+'
Possible fix: add an instance declaration for (Num (Int -> Int))
In the expression: x + x :: Int -> Int
In the expression: \ x -> x + x :: Int -> Int
In the expression: (\ x -> x + x :: Int -> Int) 2
the type signature here extends only over the `x + x`, since it is parsed as a
part of the lambda abstraction
[ \x -> (x + x :: Int -> Int) extends farther to the right than just
\x -> x + x]
So if you want to give a type signature to a lambda abstraction, you need
explicit parentheses:
Prelude> ((\x -> x + x) :: Int -> Int) 2
4
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 54, Issue 4
****************************************