Send Beginners mailing list submissions to beginners@haskell.org 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 beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: A type level programming question (Justin Bailey) 2. Problems with one of my first examples (Jeff C. Britton) 3. Re: Problems with one of my first examples (Michael Snoyman) 4. Re: A type level programming question (Levi Stephen) 5. RE: Problems with one of my first examples (Jeff C. Britton) 6. Re: Problems with one of my first examples (Daniel Fischer) ---------------------------------------------------------------------- Message: 1 Date: Mon, 15 Dec 2008 12:03:55 -0800 From: "Justin Bailey" <jgbai...@gmail.com> Subject: Re: [Haskell-beginners] A type level programming question To: "Levi Stephen" <levi.step...@gmail.com> Cc: beginners@haskell.org Message-ID: <a45dff840812151203x2b61a86ck8b6c7c89d9ea4...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 On Sun, Dec 14, 2008 at 3:11 PM, Levi Stephen <levi.step...@gmail.com> wrote: > (!) :: (Pos s, Nat i, i :<: s) => FSVec s a -> i -> a > > I was wondering if it was possible to write a function of type: > > elementAt :: FSVec s a -> Int -> a > > that called the above function, throwing an error if the index was out > of bounds. e.g., > Why would you want to write that function? From the signature on (!), it looks like any out of bounds errors should occur at compile time. I'd say the library is trying to make it so you don't have to write that function. That said, I'd try removing the type signature from elementAt and see what your compiler infers for you. You'll also have to find a way to relate "idx" to "v". Unless "length v" returns a Nat, the comparison you have won't do it. Justin ------------------------------ Message: 2 Date: Mon, 15 Dec 2008 12:17:33 -0800 From: "Jeff C. Britton" <j...@iteris.com> Subject: [Haskell-beginners] Problems with one of my first examples To: <beginners@haskell.org> Message-ID: <10776152c3da8244baa5d5b10853fe99044e7...@bulldog.iteris.com> Content-Type: text/plain; charset="iso-8859-1" Hello, I have started reading "Yet Another Haskell Tutorial" by Hal Daum´e III which can be found here http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf One of the early examples in section 3.8 pg. 35 is this askForWords = do putStrLn "Please enter a word:" word <- getLine if word == "" then return [] else do rest <- askForWords return (word : rest) I want to print the returned list and everything I try fails. I have tried the following: printList l = if length l >= 1 then do putStrLn (head l) printList (tail l) else putStrLn("") f = printList askForWords and I get Expression : printList askForWords *** Term : askForWords *** Type : IO [[Char]] *** Does not match : [[Char]] ************************************* The exercise right below this asks for a very slight modification to read numbers instead. However, I am confused about how to convert strings to numbers. If I type in the hugs interactive console read "5" + 3 --> 8 -- ok perfect However read "5" gives ERROR - Unresolved overloading *** Type : Read a => a *** Expression : read "5" Yet page 33 of the tutorial has the following code: doGuessing num = do putStrLn "Enter your guess:" guess <- getLine let guessNum = read guess -- ok in let stmt, but not at repl prompt? Anyway I take the info that has been presented and create this function: askForNumbers = do hSetBuffering stdin LineBuffering putStrLn "Give me a number (or 0 to stop)" numStr <- getLine let num = read numStr if num == 0 then return [] else do rest <- askForNumbers return (num : rest) However, when I try to use it, like say map sqrt askForNumbers ERROR - Type error in application *** Expression : map sqrt askForNumbers *** Term : askForNumbers *** Type : IO [Integer] *** Does not match : [a] ********************************************************* Is there a way to write printList to handle Strings or numbers? Or should I write printList (map show askForNumbers) Thanks, Jeff ------------------------------ Message: 3 Date: Mon, 15 Dec 2008 12:48:16 -0800 From: "Michael Snoyman" <mich...@snoyman.com> Subject: Re: [Haskell-beginners] Problems with one of my first examples To: "Jeff C. Britton" <j...@iteris.com> Cc: beginners@haskell.org Message-ID: <29bf512f0812151248p163b3101pb9fe83c63685f...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" On Mon, Dec 15, 2008 at 12:17 PM, Jeff C. Britton <j...@iteris.com> wrote: > Hello, > > I have started reading "Yet Another Haskell Tutorial" by Hal Daum´e III > which can be found here > http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf<http://www.cs.utah.edu/%7Ehal/docs/daume02yaht.pdf> > > One of the early examples in section 3.8 pg. 35 > is this > > askForWords = do > putStrLn "Please enter a word:" > word <- getLine > if word == "" > then return [] > else do > rest <- askForWords > return (word : rest) > > I want to print the returned list and everything I try fails. > > I have tried the following: > > printList l = > if length l >= 1 > then do putStrLn (head l) > printList (tail l) > else putStrLn("") > > f = printList askForWords > > and I get > Expression : printList askForWords > *** Term : askForWords > *** Type : IO [[Char]] > *** Does not match : [[Char]] I believe one of the following will work for you: f = askForWords >>= printList f = do words <- askForWords printList words > > > > ************************************* > The exercise right below this asks for a very slight modification to read > numbers instead. > > However, I am confused about how to convert strings to numbers. > If I type in the hugs interactive console > read "5" + 3 --> 8 -- ok perfect > > However > read "5" gives > ERROR - Unresolved overloading > *** Type : Read a => a > *** Expression : read "5" > > Yet page 33 of the tutorial has the following code: > doGuessing num = do > putStrLn "Enter your guess:" > guess <- getLine > let guessNum = read guess -- ok in let stmt, but not at repl prompt? The problem here is type inference. The statement read "5" has type "(Read a) => a", which basically means anything that implements the class "Read." When you do read "5" + 3, the read "5" gets the type of the 3. I assume that in the latter case, you use the expression guessNum in a way later on that the compiler can infer its type. > > > > Anyway I take the info that has been presented and create this function: > askForNumbers = do > hSetBuffering stdin LineBuffering > putStrLn "Give me a number (or 0 to stop)" > numStr <- getLine > let num = read numStr > if num == 0 > then return [] > else do > rest <- askForNumbers > return (num : rest) > > However, when I try to use it, like say > > map sqrt askForNumbers > > ERROR - Type error in application > *** Expression : map sqrt askForNumbers > *** Term : askForNumbers > *** Type : IO [Integer] > *** Does not match : [a] Similar to above, try this: do nums <- askForNumbers map sqrt nums > > > ********************************************************* > > Is there a way to write printList to handle Strings or numbers? > Or should I write > printList (map show askForNumbers) Note: you should probably do this using mapM_, but for simplicity, I'll do it using explicit recursion: printList [] = putStrLn "" -- or return () if you don't want the extra blank line printList (x:xs) = do putStrLn (show x) printList xs If you have any questions about how these worked, let me know! Michael -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20081215/03bfa53c/attachment-0001.htm ------------------------------ Message: 4 Date: Tue, 16 Dec 2008 08:36:09 +1030 From: "Levi Stephen" <levi.step...@gmail.com> Subject: Re: [Haskell-beginners] A type level programming question To: "Justin Bailey" <jgbai...@gmail.com> Cc: beginners@haskell.org Message-ID: <8341e4f40812151406p101192aal97c6a9becf4f3...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 On Tue, Dec 16, 2008 at 6:33 AM, Justin Bailey <jgbai...@gmail.com> wrote: > On Sun, Dec 14, 2008 at 3:11 PM, Levi Stephen <levi.step...@gmail.com> wrote: >> (!) :: (Pos s, Nat i, i :<: s) => FSVec s a -> i -> a >> >> I was wondering if it was possible to write a function of type: >> >> elementAt :: FSVec s a -> Int -> a >> >> that called the above function, throwing an error if the index was out >> of bounds. e.g., >> > > Why would you want to write that function? From the signature on (!), > it looks like any out of bounds errors should occur at compile time. > I'd say the library is trying to make it so you don't have to write > that function. I may not have to write this function, but I'm guessing at some stage it's going to be necessary to convert from value level integers to type level. Is this a bad guess? The type-level library provides the function reifyIntegral for this purpose, but the continuation is only allowed to rely on the Nat class constraint. > > That said, I'd try removing the type signature from elementAt and see > what your compiler infers for you. I don't know how to implement elementAt yet. FSVec provides the (!) function for accessing elements, but I need to satisfy the i :<: s class constraint before calling it. > You'll also have to find a way to > relate "idx" to "v". Unless "length v" returns a Nat, the comparison > you have won't do it. length has type forall s a. Nat s => FSVec s a -> Int > > Justin > Thanks, Levi ------------------------------ Message: 5 Date: Mon, 15 Dec 2008 14:26:11 -0800 From: "Jeff C. Britton" <j...@iteris.com> Subject: RE: [Haskell-beginners] Problems with one of my first examples To: <beginners@haskell.org> Message-ID: <10776152c3da8244baa5d5b10853fe99044e7...@bulldog.iteris.com> Content-Type: text/plain; charset="iso-8859-1" Thanks, Michael. Ok, I understand most of what you did, but ... do nums <- askForNumbers; map sqrt nums ERROR - Type error in final generator *** Term : map sqrt nums *** Type : [Integer] *** Does not match : IO a do nums <- askForNumbers; printList nums -- works fine Thanks, Jeff ------------------------------------------------------------- Hello, I have started reading "Yet Another Haskell Tutorial" by Hal Daum´e III which can be found here http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf One of the early examples in section 3.8 pg. 35 is this askForWords = do putStrLn "Please enter a word:" word <- getLine if word == "" then return [] else do rest <- askForWords return (word : rest) I want to print the returned list and everything I try fails. I have tried the following: printList l = if length l >= 1 then do putStrLn (head l) printList (tail l) else putStrLn("") f = printList askForWords and I get Expression : printList askForWords *** Term : askForWords *** Type : IO [[Char]] *** Does not match : [[Char]] I believe one of the following will work for you: f = askForWords >>= printList f = do words <- askForWords printList words ************************************* The exercise right below this asks for a very slight modification to read numbers instead. However, I am confused about how to convert strings to numbers. If I type in the hugs interactive console read "5" + 3 --> 8 -- ok perfect However read "5" gives ERROR - Unresolved overloading *** Type : Read a => a *** Expression : read "5" Yet page 33 of the tutorial has the following code: doGuessing num = do putStrLn "Enter your guess:" guess <- getLine let guessNum = read guess -- ok in let stmt, but not at repl prompt? The problem here is type inference. The statement read "5" has type "(Read a) => a", which basically means anything that implements the class "Read." When you do read "5" + 3, the read "5" gets the type of the 3. I assume that in the latter case, you use the expression guessNum in a way later on that the compiler can infer its type. Anyway I take the info that has been presented and create this function: askForNumbers = do hSetBuffering stdin LineBuffering putStrLn "Give me a number (or 0 to stop)" numStr <- getLine let num = read numStr if num == 0 then return [] else do rest <- askForNumbers return (num : rest) However, when I try to use it, like say map sqrt askForNumbers ERROR - Type error in application *** Expression : map sqrt askForNumbers *** Term : askForNumbers *** Type : IO [Integer] *** Does not match : [a] Similar to above, try this: do nums <- askForNumbers map sqrt nums ********************************************************* Is there a way to write printList to handle Strings or numbers? Or should I write printList (map show askForNumbers) Note: you should probably do this using mapM_, but for simplicity, I'll do it using explicit recursion: printList [] = putStrLn "" -- or return () if you don't want the extra blank line printList (x:xs) = do putStrLn (show x) printList xs If you have any questions about how these worked, let me know! Michael ------------------------------ Message: 6 Date: Mon, 15 Dec 2008 23:40:59 +0100 From: Daniel Fischer <daniel.is.fisc...@web.de> Subject: Re: [Haskell-beginners] Problems with one of my first examples To: "Jeff C. Britton" <j...@iteris.com>, <beginners@haskell.org> Message-ID: <200812152340.59456.daniel.is.fisc...@web.de> Content-Type: text/plain; charset="iso-8859-1" Am Montag, 15. Dezember 2008 23:26 schrieb Jeff C. Britton: > Thanks, Michael. > > Ok, I understand most of what you did, but ... > > do nums <- askForNumbers; map sqrt nums > > ERROR - Type error in final generator > *** Term : map sqrt nums > *** Type : [Integer] > *** Does not match : IO a There's another one lurking there, :t sqrt sqrt :: (Floating a) => a -> a but Integer is not a member of class Floating. The one displayed says that you must have an IO-action in your do-block, not a plain list of numbers. do nums <- askForNumbers printList $ map (sqrt . fromInteger) nums works, or printList . map (sqrt . fromInteger) =<< askForNumbers > > do nums <- askForNumbers; printList nums -- works fine > > Thanks, > > Jeff > > > ------------------------------------------------------------- > Hello, > > I have started reading "Yet Another Haskell Tutorial" by Hal Daum´e III > which can be found here http://www.cs.utah.edu/~hal/docs/daume02yaht.pdf > > One of the early examples in section 3.8 pg. 35 > is this > > askForWords = do > putStrLn "Please enter a word:" > word <- getLine > if word == "" > then return [] > else do > rest <- askForWords > return (word : rest) > > I want to print the returned list and everything I try fails. > > I have tried the following: > > printList l = > if length l >= 1 > then do putStrLn (head l) > printList (tail l) > else putStrLn("") > > f = printList askForWords > > and I get > Expression : printList askForWords > *** Term : askForWords > *** Type : IO [[Char]] > *** Does not match : [[Char]] > > I believe one of the following will work for you: > > f = askForWords >>= printList > f = do > words <- askForWords > printList words > > > > > ************************************* > The exercise right below this asks for a very slight modification to read > numbers instead. > > However, I am confused about how to convert strings to numbers. > If I type in the hugs interactive console > read "5" + 3 --> 8 -- ok perfect > > However > read "5" gives > ERROR - Unresolved overloading > *** Type : Read a => a > *** Expression : read "5" > > Yet page 33 of the tutorial has the following code: > doGuessing num = do > putStrLn "Enter your guess:" > guess <- getLine > let guessNum = read guess -- ok in let stmt, but not at repl prompt? > > The problem here is type inference. The statement read "5" has type "(Read > a) => a", which basically means anything that implements the class "Read." > When you do read "5" + 3, the read "5" gets the type of the 3. I assume > that in the latter case, you use the expression guessNum in a way later on > that the compiler can infer its type. > > > > Anyway I take the info that has been presented and create this function: > askForNumbers = do > hSetBuffering stdin LineBuffering > putStrLn "Give me a number (or 0 to stop)" > numStr <- getLine > let num = read numStr > if num == 0 > then return [] > else do > rest <- askForNumbers > return (num : rest) > > However, when I try to use it, like say > > map sqrt askForNumbers > > ERROR - Type error in application > *** Expression : map sqrt askForNumbers > *** Term : askForNumbers > *** Type : IO [Integer] > *** Does not match : [a] > > Similar to above, try this: > do nums <- askForNumbers > map sqrt nums > > > > ********************************************************* > > Is there a way to write printList to handle Strings or numbers? > Or should I write > printList (map show askForNumbers) > > Note: you should probably do this using mapM_, but for simplicity, I'll do > it using explicit recursion: > > printList [] = putStrLn "" -- or return () if you don't want the extra > blank line printList (x:xs) = do putStrLn (show x) > printList xs > > If you have any questions about how these worked, let me know! > > Michael > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 6, Issue 5 ***************************************