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: [Haskell-cafe] running and understanding a lifting
program (Patrick Browne)
2. Help-me read file (Gilmara Pompelli)
3. Need help with code duplication (types and typeclasses)
(Edward Turpin)
4. Re: Need help with code duplication (types and typeclasses)
(Brent Yorgey)
5. read (Russ Abbott)
6. Best practice for catching pure errors in IO? (John Smith)
7. Re: read (edgar klerks)
----------------------------------------------------------------------
Message: 1
Date: Mon, 25 Oct 2010 23:12:45 +0100
From: Patrick Browne <[email protected]>
Subject: [Haskell-beginners] Re: [Haskell-cafe] running and
understanding a lifting program
To: Patrick LeBoutillier <[email protected]>
Cc: beginners <[email protected]>, Haskell Cafe
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Patrick,
Thanks for taking the time to get the program running.
It seems fine, but I cannot get the *md* to print out, probably missing
the Show class somewhere.
Thanks again,
Pat
Patrick LeBoutillier wrote:
> Patrick,
>
> I found this program interesting and decided to spend a bit of time on
> it, even though I'm still a newbie.
> I did a few things to simplify the code, here are some comments:
>
> 1) I chose to rename the arithmetic functions in the Number class
> instead of trying to overload the "real" ones, I'm not that good at
> Haskell yet...
>
> 2) The program had some errors, namely I think the definition of the
> Point type should be:
>
> data Point a = Point a a
>
> to allow for different types of Points.
>
> 3) The Points class seemed useless in the end, I simply defined the
> dist function at the top level.
>
> 4) If you import Control.Monad, it makes functions (and therefore
> "Changing v") into a Monad (maybe my terminology is off here...) and
> allows you to use the general liftM and liftM2 lifting functions
> instead of defining your own.
>
>
> Here's the complete program:
>
> {-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
>
> data Point a = Point { x ::a, y :: a }
> type Time = Float
>
> -- Functor Changing, which adds time parameter t to its input value.
> -- For example, Changing Float indicates a changing floating number
> -- (i.e. a function of time).
> type Changing v = Time -> v
>
> -- Lifting functions
> lift1 op a = \t -> op (a t)
> lift2 op a b = \t -> op (a t) (b t)
>
> class Number a where
> add, sub, mul :: a -> a -> a
> square, squareRoot :: a -> a
> square a = a `mul` a
>
> instance Number Float where
> add = (+)
> sub = (-)
> mul = (*)
> squareRoot = sqrt
>
> instance Number (Changing Float) where
> add = lift2 add
> sub = lift2 sub
> mul = lift2 mul
> squareRoot = lift1 squareRoot
>
> -- The distance operation is defined as follow
> dist :: Number a => Point a -> Point a -> a
> dist a b = squareRoot $ square((x a) `sub` (x b)) `add` square ((y a)
> `sub` (y b))
>
> -- Running the code
> -- If p1 and p2 are two 2D static points,
> -- their distance d is calculated as follows:
> p1, p2 :: Point Float
> p1 = Point 3.4 5.5
> p2 = Point 4.5 4.5
>
> -- distance between p1 and p2 --> 1.55
> d = dist p1 p2
>
> -- For 2D moving points mp1 and mp2, their distance md,
> -- which is a function of time, is calculated as follows:
> mp1, mp2 :: Point (Changing Float)
> mp1 = Point (\t -> 4.0 + 0.5 * t) (\t -> 4.0 - 0.5 * t)
> mp2 = Point (\t -> 0.0 + 1.0 * t) (\t -> 0.0 - 1.0 * t)
> -- distance between mp1 and mp2
> md = dist mp1 mp2
> -- distance md for time 2 ----> 5.83
>
>
>>
>> This message has been scanned for content and viruses by the DIT Information
>> Services E-Mail Scanning Service, and is believed to be clean.
>> http://www.dit.ie
>> _______________________________________________
>> Haskell-Cafe mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
> Patrick
>
This message has been scanned for content and viruses by the DIT Information
Services E-Mail Scanning Service, and is believed to be clean. http://www.dit.ie
------------------------------
Message: 2
Date: Mon, 18 Oct 2010 16:20:21 -0300
From: Gilmara Pompelli <[email protected]>
Subject: [Haskell-beginners] Help-me read file
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hello
I have a file with 100 lists, with 100 ints.
I have to read the file and apply the map and sort functions on lists.
II did it to read file:
learquivo :: FilePath -> IO ([[Int]])
learquivo s = do
conteudo <- readFile s
return (read conteudo)
But now applying the sort, the error appears:
<interactive>:1:5:
Couldn't match expected type `[a]'
against inferred type `IO [[Int]]'
how can I fix?
Thanks
------------------------------
Message: 3
Date: Sat, 23 Oct 2010 23:24:05 -0400
From: Edward Turpin <[email protected]>
Subject: [Haskell-beginners] Need help with code duplication (types
and typeclasses)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="UTF-8"
I can't seem to get my head around types and typeclasses (as well as
many other things in Haskell). Here's the following code I need help
with.
In module 1:
//////////////////////// Start of Code //////////////////////////////
data NewtonRow = NewtonRow { iteration :: Integer,
xn :: Double,
fxn :: Double,
errEst :: Double
}
instance Show NewtonRow where
show row = show (iteration row) ++ "\t" ++
showEFloat (Just 8) (xn row) "\t" ++
showEFloat (Just 2) (fxn row) "\t" ++
showEFloat (Just 2) (errEst row) "\n"
newtype NewtonTable = NewtonTable { getNewtonTable :: [NewtonRow] }
instance Show NewtonTable where
show table = "n\t" ++ "xn\t\t\t" ++ "fxn\t\t" ++ "xn - xn-1\n" ++
concatMap show (getNewtonTable table)
instance Monoid NewtonTable where
mempty = NewtonTable []
table1 `mappend` table2 = NewtonTable $ (getNewtonTable table1) ++
(getNewtonTable table2)
cons :: NewtonRow -> NewtonTable -> NewtonTable
row `cons` table = NewtonTable (row : getNewtonTable table)
////////////////////// End of Code ////////////////////////////////
In module 2:
///////////////// Start of more Code ///////////////////////////////
data BisectRow = BisectRow { iteration :: Integer,
start :: Double,
end :: Double,
middle :: Double,
deviation :: Double,
fm :: Double
}
instance Show BisectRow where
show row = show (iteration row) ++ "\t" ++
showEFloat (Just 4) (start row) "\t" ++
showEFloat (Just 4) (end row) "\t" ++
showEFloat (Just 4) (middle row) "\t" ++
showEFloat (Just 4) (deviation row) "\t" ++
showEFloat (Just 4) (fm row) "\n"
newtype BisectTable = BisectTable { getBisectTable :: [BisectRow] }
instance Show BisectTable where
show table = "n\t" ++ "start\t\t" ++ "end\t\t" ++ "middle\t" ++
"error\t\t" ++ "f(middle)\n" ++
concatMap show (getBisectTable table)
instance Monoid BisectTable where
mempty = BisectTable []
table1 `mappend` table2 = BisectTable $ (getBisectTable table1) ++
(getBisectTable table2)
cons :: BisectRow -> BisectTable -> BisectTable
row `cons` table = BisectTable (row : getBisectTable table)
////////////////////// End of code /////////////////////////////////
There is obviously duplication going on, but I can't figure out how to
get rid of it. "BisectTable" is essentially the same as "NewtonTable"
except that it has a different number of columns, different column
headers (in the "show" functions), and different row elements.
"BisectRow" and "NewtonRow" are also somewhat similar. And the "cons"
functions in each module pretty much do the same thing. I'm just trying
to learn Haskell, so as far as I know I might be doing ass-backwards.
Can anybody give any suggestions?
------------------------------
Message: 4
Date: Tue, 26 Oct 2010 19:37:25 -0400
From: Brent Yorgey <[email protected]>
Subject: Re: [Haskell-beginners] Need help with code duplication
(types and typeclasses)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Sat, Oct 23, 2010 at 11:24:05PM -0400, Edward Turpin wrote:
> I can't seem to get my head around types and typeclasses (as well as
> many other things in Haskell). Here's the following code I need help
> with.
>
> In module 1:
> //////////////////////// Start of Code //////////////////////////////
> data NewtonRow = NewtonRow { iteration :: Integer,
> xn :: Double,
> fxn :: Double,
> errEst :: Double
> }
>
> instance Show NewtonRow where
> show row = show (iteration row) ++ "\t" ++
> showEFloat (Just 8) (xn row) "\t" ++
> showEFloat (Just 2) (fxn row) "\t" ++
> showEFloat (Just 2) (errEst row) "\n"
>
> newtype NewtonTable = NewtonTable { getNewtonTable :: [NewtonRow] }
>
> instance Show NewtonTable where
> show table = "n\t" ++ "xn\t\t\t" ++ "fxn\t\t" ++ "xn - xn-1\n" ++
> concatMap show (getNewtonTable table)
>
> instance Monoid NewtonTable where
> mempty = NewtonTable []
> table1 `mappend` table2 = NewtonTable $ (getNewtonTable table1) ++
> (getNewtonTable table2)
>
> cons :: NewtonRow -> NewtonTable -> NewtonTable
> row `cons` table = NewtonTable (row : getNewtonTable table)
How about something like this? The idea is to make the Table type
polymorphic, and to abstract the row-type-specific table header into a
new type class.
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype Table a = Table { getTable :: [a] }
deriving Monoid -- derive the Monoid instance from the
-- underlying list instance
cons :: a -> Table a -> Table a
row `cons` (Table t) = Table (row : t)
class HasTableHeader a where
tableHeader :: a -> String
instance TableHeader NewtonRow where
tableHeader _ = "n\t" ++ "wn\t\t\t" ++ "fxn\t\t" ++ "xn - xn-1\n"
instance HasTableHeader a => Show (Table a) where
show (Table []) = "---"
show (Table (r:rs)) = showHeader r ++ concatMap show (r:rs)
I think you can see how to extend this to work with the other row type
as well.
-Brent
------------------------------
Message: 5
Date: Tue, 26 Oct 2010 23:36:35 -0700
From: Russ Abbott <[email protected]>
Subject: [Haskell-beginners] read
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi,
I'm having a problem with the read function. I thought it took a string and
converted it into a value (if there is a conversion). But it doesn't seem to
work for me on GHCi
Prelude> read "123"
*** Exception: Prelude.read: no parse
Prelude> read (show 123)
*** Exception: Prelude.read: no parse
There must be something trivial that I'm missing.
Thanks.
*-- Russ Abbott*
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20101027/04f5e260/attachment-0001.html
------------------------------
Message: 6
Date: Wed, 27 Oct 2010 08:45:13 +0200
From: John Smith <[email protected]>
Subject: [Haskell-beginners] Best practice for catching pure errors in
IO?
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
I have a pure value which may throw errors when it is evaluated in an IO monad.
My code to handle it looks like this:
temp <- try $ evaluate value
case temp of
Left (error::ErrorCall) -> scoldTheUserAndTryAgain
Right correct -> doSomething
Can this be done without the temporary variable, or with less plumbing? I tried
lifting either, but GHC was fussing
about ambiguous types, appending a signature to a pattern match is no longer
allowed(?), and it didn't look that much
cleaner.
------------------------------
Message: 7
Date: Wed, 27 Oct 2010 08:53:01 +0200
From: edgar klerks <[email protected]>
Subject: Re: [Haskell-beginners] read
To: [email protected]
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hi Russ,
You probably have the monomorphism restriction turned of, so you don't get
an error message. If you add a type, it will work fine. Like this:
Prelude> read "123"
*** Exception: Prelude.read: no parse
Prelude> read "123" :: Int
123
Prelude>
Greets,
Edgar
On Wed, Oct 27, 2010 at 8:36 AM, Russ Abbott <[email protected]> wrote:
> Hi,
>
> I'm having a problem with the read function. I thought it took a string and
> converted it into a value (if there is a conversion). But it doesn't seem to
> work for me on GHCi
>
> Prelude> read "123"
> *** Exception: Prelude.read: no parse
> Prelude> read (show 123)
> *** Exception: Prelude.read: no parse
>
>
> There must be something trivial that I'm missing.
>
> Thanks.
>
> *-- Russ Abbott*
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20101027/c2c90343/attachment.html
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 28, Issue 40
*****************************************