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: Could not deduce (Matrix m (Maybe a)) from the context
(Matrix m a) (Lyndon Maydwell)
2. Re: PI calculation - Newbie question (Daniel Fischer)
3. Re: PI calculation - Newbie question (Felipe Lessa)
4. Uninstall GHC binary package? () (Kwanghoon Choi)
5. Re: Uninstall GHC binary package? () (Daniel Fischer)
6. Re: Beginners Digest, Vol 19, Issue 30 (Gabi)
----------------------------------------------------------------------
Message: 1
Date: Fri, 29 Jan 2010 19:11:43 +0800
From: Lyndon Maydwell <[email protected]>
Subject: Re: [Haskell-beginners] Could not deduce (Matrix m (Maybe a))
from the context (Matrix m a)
To: Daniel Fischer <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
Ah, yes. That might be just what I'm after!
> However, having a look, none of the methods in the class look like they
> depend on the actual type a, so it might be better to have
>
> class Matrix m where
> Â Â fromRows :: [[a]] -> m a
> Â Â toList :: m a -> [a]
> Â Â rows :: m a -> Integer
> Â Â columns :: m a -> Integer
> Â Â row :: m a -> Integer -> [a]
> Â Â column :: m a -> Integer -> [a]
> Â Â at :: m a -> Integer -> Integer -> a
> Â Â (!!!) :: m a -> Integer -> Integer -> a
> Â Â vicinityRows :: m a -> Integer -> Integer -> [[Maybe a]]
> Â Â vicinityMatrix :: m a -> Integer -> Integer -> m (Maybe a)
> Â Â -- No constraint needed!!
> Â Â neighbours :: m a -> Integer -> Integer -> [a]
------------------------------
Message: 2
Date: Fri, 29 Jan 2010 13:13:45 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] PI calculation - Newbie question
To: [email protected]
Cc: Gabi <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Freitag 29 Januar 2010 11:59:30 schrieb Gabi:
> Hi Group,
>
> I am just trying to learn the lang and Implemented this PI calculator.
> It is really slow and very memory consuming (much much slower than its
> equivalent in Clojure for instance)
>
> I think the problem is in "rs <- sequence (replicate n isRandIn)" -
> But I don't know how to get around it (how do I get a lazy sequence of
> rs? Is it the problem anyway?)
It's one part of the problem.
sequence (replicate n isRandIn)
requires all n random number computations to be in memory at once. When you
want far more than 10000 random points, that'll take a lot of memory.
The more evil part is that the things aren't actually calculated when you
call sequence, so what you get is a long list of thunks "calculate when
needed" which take a really large amount of space (
./MCPi 100000 +RTS -sstderr
3.14048
252,873,148 bytes allocated in the heap
48,792,700 bytes copied during GC
8,861,192 bytes maximum residency (5 sample(s))
128,768 bytes maximum slop
23 MB total memory in use (0 MB lost due to fragmentation)
./MCPi 1000000 +RTS -sstderr
3.1401
2,523,687,224 bytes allocated in the heap
527,553,588 bytes copied during GC
107,345,604 bytes maximum residency (8 sample(s))
7,619,396 bytes maximum slop
219 MB total memory in use (2 MB lost due to fragmentation)
).
You can reduce the space requirements by forcing the calculations earlier,
e.g.
isRandIn = do
p <- randPoint
return $! inCirc p
The ($!) requires the evaluation of (inCirc p) [to the outermost
constructor, since it's an Int, that means complete evaluation], the list
that sequence produces is a list of evaluated Ints, needs far less space.
Unfortunately, that is much slower. I'm not sure why.
However, it's not good to do that much in IO (and getStdRandom isn't
exactly fast, it has to retrieve the random generator and store the
modified generator).
The bulk of the algorithm is a pure calculation, given the *lazy* list of
pseudo-random coordinates.
So
----------------------------------------------------------------------
-- curried version of inCirc, no need to construct pairs
inCirc :: Double -> Double -> Int
inCirc x y
| dx*dx + dy*dy < 0.25 = 1
| otherwise = 0
where
dx = x - 0.5
dy = y - 0.5
-- transform a list of coordinates into a list of indicators
-- whether the point is inside the circle (sorry for the
-- stupid name)
inCircles :: [Double] -> [Int]
inCircles (x:y:zs) = inCirc x y : inCircles zs
inCircles _ = []
-- given a count of experiments and an infinite list of coordinates,
-- calculate an approximation to pi
calcPi :: Int -> [Double] -> Double
calcPi n ds = fromIntegral ct / fromIntegral n * 4
where
ct = sum . take n $ inCircles ds
-- now the IO part is only
-- * getting the number of experiments and
-- * getting the StdGen
main :: IO ()
main = do
args <- getArgs
sg <- getStdGen
let n = case args of
(a:_) -> read a
_ -> 10000
print $ calcPi n (randomRs (0,1) sg)
----------------------------------------------------------------------
Now the list of coordinates is consumed as it is produced, things can
immediately be garbage-collected (if compiled with optimisations, without
optimisations, you would have to replace "sum" with "foldl' (+) 0").
It runs in constant space and faster. It's still slow, though, because
System.Random's StdGen is slow.
For faster generation of pseudo-random numbers, take a look at e.g.
mersenne-random (there are other fast PRNG packages on hackage, too, IIRC).
>
> -- p.hs simple PI calculator, using the Monte Carlo Method
>
> import System( getArgs )
> import System.Random
> inCirc :: (Double, Double) -> Int
> inCirc (x,y) = if ((dx * dx) + (dy * dy)) < 0.25
> then 1
> else 0
> where dx = x - 0.5
> dy = y - 0.5
>
>
> randPoint :: IO (Double, Double)
> randPoint = do
> x <-getStdRandom (randomR (0, 1 :: Double))
> y <-getStdRandom (randomR (0, 1 :: Double))
> return (x, y)
>
>
> isRandIn = do
> p <- randPoint
> return (inCirc p)
>
>
> main = do
> args <- getArgs
> let n = if null args
> then 10000
> else read $ (head args)::Int
>
> rs <- sequence (replicate n isRandIn)
> let pi = (fromIntegral(sum rs) / fromIntegral n) * 4
> print pi
------------------------------
Message: 3
Date: Fri, 29 Jan 2010 10:32:08 -0200
From: Felipe Lessa <[email protected]>
Subject: Re: [Haskell-beginners] PI calculation - Newbie question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Fri, Jan 29, 2010 at 12:59:30PM +0200, Gabi wrote:
> I think the problem is in "rs <- sequence (replicate n isRandIn)" -
> But I don't know how to get around it (how do I get a lazy sequence of
> rs? Is it the problem anyway?)
First of all, we don't like to be inside IO just for getting
random numbers. It is better to write, for example,
type Point = (Double, Double)
randPoint :: StdGen -> (Point, StdGen)
randPoint gen = let (x, gen') = randomR (0,1) gen
(y, gen'') = randomR (0,1) gen'
in ((x,y),gen'')
and then thread the generator yourself
randPoints :: StdGen -> [Point]
randPoints gen = let (p,gen') = randPoint gen
in p : randPoints gen' -- lazy!
To use those functions, something like
import Control.Applicative ((<$>))
inCirc :: Point -> Bool
inCirc = ...
calcPi :: Int -> [Point] -> Double
calcPi n pts = let s = length $ filter inCirc $ take n pts
in 4 * fromIntegral s / fromIntegral n
main = do pi <- calcPi 10000 . randPoints <$> newStdGen
print pi
I haven't tested performance, but it shouldn't be worse :), and
it's cleaner and more idiomatic. To be even clearer you could
use MonadRandom[1] which encapsulates the passing of StdGen between
computations.
[1] http://hackage.haskell.org/package/MonadRandom
However System.Random (used by MonadRandom as well) is known to
be very very very very slow. If you want more performance then
you should use one of the other PRNG libraries on Hackage:
http://hackage.haskell.org/package/mwc-random
http://hackage.haskell.org/package/mersenne-random
http://hackage.haskell.org/package/gsl-random
The reason why System.Random is slow is because it must support
'split', which none of the packages above support.
HTH,
--
Felipe.
------------------------------
Message: 4
Date: Fri, 29 Jan 2010 22:35:22 +0900
From: Kwanghoon Choi <[email protected]>
Subject: [Haskell-beginners] Uninstall GHC binary package? ()
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Dear All,
After I installed the ghc binary package
(ghc-6.12.1-i386-unknown-linux.tar.bz2) manually, I can't find a way to
uninstall it. Could anyone help me out?
Thanks in advance.
Kwanghoon
PS.
I have installed two versions of ghc. One is with the ghc-6.12.1 package,
and the other is ghc-6.8.2 package. The former is downloaded to be installed
manually, but the latter is done automatically by using APT tool. They are
installed in a different way. I mean the ghc-6.12.1 is under /usr/local/bin,
but ghc-6.8.2 is under /usr/bin. (Of course, you can change this default
setting though)
My original purpose is to install the newest QuickCheck package. Having both
ghc packages seems to cause a conflict. After I try with $ runhaskell
Setup.lhs configure, I met a dependency error requiring me to install mtl
(monad transformer library). I did install by APT tool. But the retrial with
$ runhaskell Setup.lhs configure does not work because the configuration is
being done by ghc-6.12.1 package. The ghc-6.12.1 package doesn't recognize
the presence of mtl package that I installed just before. Only the ghc-6.8.2
package seems to recognize the presence of mtl package. The mtl package
seems to be built aligned with ghc-6.8.2 package. I guess this strange
phenomena comes from the different configuration of the two versions.
This is the reason why I try to uninstall ghc-6.12.1, but it doesn't seem to
provide any way to be uninstalled, unfortunately. If I am wrong, please
correct me.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20100129/3f1c6b17/attachment-0001.html
------------------------------
Message: 5
Date: Fri, 29 Jan 2010 15:24:43 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Uninstall GHC binary package? ()
To: [email protected]
Cc: Kwanghoon Choi <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Freitag 29 Januar 2010 14:35:22 schrieb Kwanghoon Choi:
> Dear All,
>
> After I installed the ghc binary package
> (ghc-6.12.1-i386-unknown-linux.tar.bz2) manually, I can't find a way to
> uninstall it. Could anyone help me out?
It will probably be better to uninstall the 6.8.2. There are less
antiquated GHCs in some repositories for debian based systems, you can get
at least 6.10.* from those.
But if you really want to get rid of 6.12, I think you'll have to delete it
manually. The most important thing is to not have the ghc, ghc-pkg, runghc
and runhaskell scripts [perhaps some are binaries] in you /usr/local/bin [I
suppose that's before /usr/bin in your path, hence that's picked up by
runhaskell]. Rename them before deleting them to check whether things still
work.
However, there's no need to get rid of either. Just learn how to use cabal-
install.
>
> Thanks in advance.
>
> Kwanghoon
>
> PS.
> I have installed two versions of ghc. One is with the ghc-6.12.1
> package, and the other is ghc-6.8.2 package. The former is downloaded to
> be installed manually, but the latter is done automatically by using APT
> tool. They are installed in a different way. I mean the ghc-6.12.1 is
> under /usr/local/bin, but ghc-6.8.2 is under /usr/bin. (Of course, you
> can change this default setting though)
apt may not be the best method to deal with Haskell stuff. With cabal-
install, you can get up-to-date versions of the packages on hackage.
>
> My original purpose is to install the newest QuickCheck package. Having
> both ghc packages seems to cause a conflict. After I try with $
> runhaskell Setup.lhs configure,
Right. "which runhaskell" will probably say /usr/local/bin/runhaskell, and
$ runhaskell --version
runghc 6.12.1
?
> I met a dependency error requiring me to
> install mtl (monad transformer library).
That is no longer included in the ghc packages.
> I did install by APT tool.
That unfortunately only knows about the 6.8.2 it installed itself, so it
installs mtl for 6.8.2 (which, IIRC, is completely superfluous as mtl was
still included then).
> But
> the retrial with $ runhaskell Setup.lhs configure does not work because
> the configuration is being done by ghc-6.12.1 package.
You could try
runhaskell ./Setup.lhs configure --with-compiler=/usr/bin/ghc
That would install the package for 6.8.2, if it works (which it probably
won't because the newest QuickCheck's cabal file won't be understood by the
old Cabal that came with 6.8.2).
> The ghc-6.12.1
> package doesn't recognize the presence of mtl package that I installed
> just before. Only the ghc-6.8.2 package seems to recognize the presence
> of mtl package. The mtl package seems to be built aligned with ghc-6.8.2
> package.
Haskell packages are built for and registered with one compiler (version)
at the time. So 6.8.2 will know nothing about packages you register with
6.12.1 and vice versa.
> I guess this strange phenomena comes from the different
> configuration of the two versions.
>
> This is the reason why I try to uninstall ghc-6.12.1, but it doesn't
> seem to provide any way to be uninstalled, unfortunately. If I am wrong,
> please correct me.
I suggest:
download http://hackage.haskell.org/package/cabal-install
(version 0.8, to be sure)
unpack it
cd cabal-install-0.8
./bootstrap.sh
If you get
"The 'cabal' program has been installed in ...",
all went well and you have a working cabal-install which you can use to get
the latest greatest haskell packages (after reading the rest of
bootstrap.sh's output and acting accordingly).
Then a simple
$ cabal install quickcheck
will download the latest quickcheck package and its dependencies, and build
them and register them with your great 6.12.1.
If you get
"Sorry, something went wrong."
ask again here or in the haskell-cafe.
------------------------------
Message: 6
Date: Fri, 29 Jan 2010 18:03:12 +0200
From: Gabi <[email protected]>
Subject: [Haskell-beginners] Re: Beginners Digest, Vol 19, Issue 30
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I copy pasted Daniel's code. Changed the code to use Mersenne
genrerator and got 4200% (!) improvement:
import System.Random.Mersenne
import System( getArgs )
inCirc :: Double -> Double -> Int
inCirc x y
| dx*dx + dy*dy < 0.25 = 1
| otherwise = 0
where
dx = x - 0.5
dy = y - 0.5
-- transform a list of coordinates into a list of indicators
-- whether the point is inside the circle (sorry for the
-- stupid name)
inCircles :: [Double] -> [Int]
inCircles (x:y:zs) = inCirc x y : inCircles zs
inCircles _ = []
-- given a count of experiments and an infinite list of coordinates,
-- calculate an approximation to pi
calcPi :: Int -> [Double] -> Double
calcPi n ds = fromIntegral ct / fromIntegral n * 4
where
ct = sum . take n $ inCircles ds
-- now the IO part is only
-- * getting the number of experiments and
-- * getting the StdGen
main :: IO ()
main = do
args <- getArgs
sg <- getStdGen
let n = case args of
(a:_) -> read a
_ -> 10000
rands <- randoms sg :: IO [Double]
print $ calcPi n rands
time ./slow-pi +RTS -K1G -RTS 1000000
3.14222
real 0m6.886s
user 0m6.680s
sys 0m0.200s
time ./improved-pi 1000000
3.14364
real 0m0.163s
user 0m0.160s
sys 0m0.000s
Thanks all for the help !
On Fri, Jan 29, 2010 at 12:31 PM, <[email protected]> wrote:
> 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. Could not deduce (Matrix m (Maybe a)) from the context
> (Matrix m a) (Lyndon Maydwell)
> 2. Re: subset - a little add (Daniel Fischer)
> 3. Re: Could not deduce (Matrix m (Maybe a)) from the context
> (Matrix m a) (Daniel Fischer)
> 4. Re: Could not deduce (Matrix m (Maybe a)) from the context
> (Matrix m a) (Lyndon Maydwell)
> 5. subset - a little add (Luca Ciciriello)
> 6. Re: Could not deduce (Matrix m (Maybe a)) from the context
> (Matrix m a) (Daniel Fischer)
> 7. PI calculation - Newbie question (Gabi)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Fri, 29 Jan 2010 16:52:37 +0800
> From: Lyndon Maydwell <[email protected]>
> Subject: [Haskell-beginners] Could not deduce (Matrix m (Maybe a))
> from the context (Matrix m a)
> To: [email protected]
> Message-ID:
> <[email protected]>
> Content-Type: text/plain; charset=UTF-8
>
> Hi Beginners.
>
> I'm writing a matrix class for a game of life implementation. When I
> try to compile it I get the error "Could not deduce (Matrix m (Maybe
> a)) from the context (Matrix m a)" for the method vicinityMatrix.
>
> However, when I query the type of an identical implementation to
> vicinityMatrix in ghci it is successful:
>
> :t \m x y -> fromRows $ vicinityRows m x y
> \m x y -> fromRows $ vicinityRows m x y
> :: forall (m :: * -> *) (m1 :: * -> *) a.
> (Matrix m (Maybe a), Matrix m1 a) =>
> m1 a -> Integer -> Integer -> m (Maybe a)
>
> What might be preventing the class from compiling?
>
> Thanks guys.
>
> ---
>
> My Matrix class definition follows below:
>
> module Matrix (Matrix) where
>
> import Data.Array
> import Data.Maybe (catMaybes)
> import Control.Monad (guard)
>
> class Matrix m a
> where
> fromRows :: [[a]] -> m a
> toList :: m a -> [a]
> rows :: m a -> Integer
> columns :: m a -> Integer
> row :: m a -> Integer -> [a]
> column :: m a -> Integer -> [a]
> at :: m a -> Integer -> Integer -> a
> (!!!) :: m a -> Integer -> Integer -> a
> vicinityRows :: m a -> Integer -> Integer -> [[Maybe a]]
> vicinityMatrix :: m a -> Integer -> Integer -> m (Maybe a)
> neighbours :: m a -> Integer -> Integer -> [a]
>
> toList m = do
> x <- [0 .. columns m - 1]
> y <- [0 .. rows m - 1]
> return $ at m x y
>
> row m n = [at m x n | x <- [0 .. columns m - 1]]
> column m n = [at m n y | y <- [0 .. rows m - 1]]
>
> at = (!!!)
> (!!!) = at
>
> vicinityRows m x y = do
> x' <- [x - 1 .. x + 1]
> return $ do
> y' <- [y - 1 .. y + 1]
> return cell where
> cell
> | x < 0 = Nothing
> | y < 0 = Nothing
> | x >= columns m = Nothing
> | y >= rows m = Nothing
> | otherwise = Just $ at m x y
>
> vicinityMatrix m x y = fromRows $ vicinityRows m x y
>
> -- neighbours = catMaybes . toListN . vicinityMatrix
>
> toListN :: Matrix m a => m a -> [a]
> toListN m = do
> x <- [0 .. columns m - 1]
> y <- [0 .. rows m - 1]
> guard $ x /= 1 && y /= 1
> return $ at m x y
>
>
> ------------------------------
>
> Message: 2
> Date: Fri, 29 Jan 2010 10:06:29 +0100
> From: Daniel Fischer <[email protected]>
> Subject: Re: [Haskell-beginners] subset - a little add
> To: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset="utf-8"
>
> Am Freitag 29 Januar 2010 08:36:35 schrieb Luca Ciciriello:
>> Just a little add to may previous mail.
>>
>> The solution I've found from myself is:
>>
>>
>>
>> subset :: [String] -> [String] -> Bool
>> subset xs ys = and [elem x ys | x <- xs]
>>
>
> Variant:
>
> subset xs ys = all (`elem` ys) xs
>
> but is that really what you want? That says subset [1,1,1,1] [1] ~> True.
> If you regard your lists as representatives of sets (as the name suggests),
> then that's correct, otherwise not.
>
> However, this is O(length xs * length ys). If you need it only for types
> belonging to Ord, a much better way is
>
> import qualified Data.Set as Set
> import Data.Set (fromList, isSubsetOf, ...)
>
> subset xs ys = fromList xs `isSubsetOf` fromList ys
>
> or, if you don't want to depend on Data.Set,
>
> subset xs ys = sort xs `isOrderedSublistOf` sort ys
>
> xxs@(x:xs) `isOrderedSublistOf` (y:ys)
> | x < y = False
> | x == y = xs `isOrderedSublistOf` ys
> | otherwise = xxs `isOrderedSublistOf` ys
> [] `isOrderedSublistOf` _ = True
> _ `isOrderedSublistOf` [] = False
>
>>
>>
>> My question is if exists a more elegant way to do that.
>>
>>
>>
>> Luca.
>
>
>
> ------------------------------
>
> Message: 3
> Date: Fri, 29 Jan 2010 10:17:10 +0100
> From: Daniel Fischer <[email protected]>
> Subject: Re: [Haskell-beginners] Could not deduce (Matrix m (Maybe a))
> from the context (Matrix m a)
> To: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset="utf-8"
>
> Am Freitag 29 Januar 2010 09:52:37 schrieb Lyndon Maydwell:
>> Hi Beginners.
>>
>> I'm writing a matrix class for a game of life implementation. When I
>> try to compile it I get the error "Could not deduce (Matrix m (Maybe
>> a)) from the context (Matrix m a)" for the method vicinityMatrix.
>>
>> However, when I query the type of an identical implementation to
>>
>> vicinityMatrix in ghci it is successful:
>> :t \m x y -> fromRows $ vicinityRows m x y
>>
>> \m x y -> fromRows $ vicinityRows m x y
>>
>> :: forall (m :: * -> *) (m1 :: * -> *) a.
>>
>> (Matrix m (Maybe a), Matrix m1 a) =>
>> m1 a -> Integer -> Integer -> m (Maybe a)
>>
>> What might be preventing the class from compiling?
>
> Well, the error says the compiler (the type checker) can't deduce the
> context (Matrix m (Maybe a)) from the givens. If you supply that
> information,
>
> vicinityMatrix :: Matrix m (Maybe a) =>
> m a -> Integer -> Integer -> m (Maybe a)
>
> it'll work.
>
>>
>> Thanks guys.
>>
>> ---
>>
>> My Matrix class definition follows below:
>>
>> module Matrix (Matrix) where
>>
>> import Data.Array
>> import Data.Maybe (catMaybes)
>> import Control.Monad (guard)
>>
>> class Matrix m a
>> where
>> fromRows :: [[a]] -> m a
>> toList :: m a -> [a]
>> rows :: m a -> Integer
>> columns :: m a -> Integer
>> row :: m a -> Integer -> [a]
>> column :: m a -> Integer -> [a]
>> at :: m a -> Integer -> Integer -> a
>> (!!!) :: m a -> Integer -> Integer -> a
>> vicinityRows :: m a -> Integer -> Integer -> [[Maybe a]]
>> vicinityMatrix :: m a -> Integer -> Integer -> m (Maybe a)
>> neighbours :: m a -> Integer -> Integer -> [a]
>>
>> toList m = do
>> x <- [0 .. columns m - 1]
>> y <- [0 .. rows m - 1]
>> return $ at m x y
>>
>> row m n = [at m x n | x <- [0 .. columns m - 1]]
>> column m n = [at m n y | y <- [0 .. rows m - 1]]
>>
>> at = (!!!)
>> (!!!) = at
>>
>> vicinityRows m x y = do
>> x' <- [x - 1 .. x + 1]
>> return $ do
>> y' <- [y - 1 .. y + 1]
>> return cell where
>> cell
>>
>> | x < 0 = Nothing
>> | y < 0 = Nothing
>> | x >= columns m = Nothing
>> | y >= rows m = Nothing
>> | otherwise = Just $ at m x y
>>
>> vicinityMatrix m x y = fromRows $ vicinityRows m x y
>>
>> -- neighbours = catMaybes . toListN . vicinityMatrix
>>
>> toListN :: Matrix m a => m a -> [a]
>> toListN m = do
>> x <- [0 .. columns m - 1]
>> y <- [0 .. rows m - 1]
>> guard $ x /= 1 && y /= 1
>> return $ at m x y
>
>
>
>
> ------------------------------
>
> Message: 4
> Date: Fri, 29 Jan 2010 17:45:32 +0800
> From: Lyndon Maydwell <[email protected]>
> Subject: Re: [Haskell-beginners] Could not deduce (Matrix m (Maybe a))
> from the context (Matrix m a)
> To: Daniel Fischer <[email protected]>
> Cc: [email protected]
> Message-ID:
> <[email protected]>
> Content-Type: text/plain; charset=UTF-8
>
> Thanks Daniel.
>
> It works, but I'm a bit confused as to why the extra type information is
> needed.
>
> On Fri, Jan 29, 2010 at 5:17 PM, Daniel Fischer
> <[email protected]> wrote:
>> Am Freitag 29 Januar 2010 09:52:37 schrieb Lyndon Maydwell:
>>> Hi Beginners.
>>>
>>> I'm writing a matrix class for a game of life implementation. When I
>>> try to compile it I get the error "Could not deduce (Matrix m (Maybe
>>> a)) from the context (Matrix m a)" for the method vicinityMatrix.
>>>
>>> However, when I query the type of an identical implementation to
>>>
>>> vicinityMatrix in ghci it is successful:
>>> :t \m x y -> fromRows $ vicinityRows m x y
>>>
>>> \m x y -> fromRows $ vicinityRows m x y
>>>
>>> :: forall (m :: * -> *) (m1 :: * -> *) a.
>>>
>>> (Matrix m (Maybe a), Matrix m1 a) =>
>>> m1 a -> Integer -> Integer -> m (Maybe a)
>>>
>>> What might be preventing the class from compiling?
>>
>> Well, the error says the compiler (the type checker) can't deduce the
>> context (Matrix m (Maybe a)) from the givens. If you supply that
>> information,
>>
>> vicinityMatrix :: Matrix m (Maybe a) =>
>> m a -> Integer -> Integer -> m (Maybe a)
>>
>> it'll work.
>>
>>>
>>> Thanks guys.
>>>
>>> ---
>>>
>>> My Matrix class definition follows below:
>>>
>>> module Matrix (Matrix) where
>>>
>>> import Data.Array
>>> import Data.Maybe (catMaybes)
>>> import Control.Monad (guard)
>>>
>>> class Matrix m a
>>> where
>>> fromRows :: [[a]] -> m a
>>> toList :: m a -> [a]
>>> rows :: m a -> Integer
>>> columns :: m a -> Integer
>>> row :: m a -> Integer -> [a]
>>> column :: m a -> Integer -> [a]
>>> at :: m a -> Integer -> Integer -> a
>>> (!!!) :: m a -> Integer -> Integer -> a
>>> vicinityRows :: m a -> Integer -> Integer -> [[Maybe a]]
>>> vicinityMatrix :: m a -> Integer -> Integer -> m (Maybe a)
>>> neighbours :: m a -> Integer -> Integer -> [a]
>>>
>>> toList m = do
>>> x <- [0 .. columns m - 1]
>>> y <- [0 .. rows m - 1]
>>> return $ at m x y
>>>
>>> row m n = [at m x n | x <- [0 .. columns m - 1]]
>>> column m n = [at m n y | y <- [0 .. rows m - 1]]
>>>
>>> at = (!!!)
>>> (!!!) = at
>>>
>>> vicinityRows m x y = do
>>> x' <- [x - 1 .. x + 1]
>>> return $ do
>>> y' <- [y - 1 .. y + 1]
>>> return cell where
>>> cell
>>>
>>> | x < 0 = Nothing
>>> | y < 0 = Nothing
>>> | x >= columns m = Nothing
>>> | y >= rows m = Nothing
>>> | otherwise = Just $ at m x y
>>>
>>> vicinityMatrix m x y = fromRows $ vicinityRows m x y
>>>
>>> -- neighbours = catMaybes . toListN . vicinityMatrix
>>>
>>> toListN :: Matrix m a => m a -> [a]
>>> toListN m = do
>>> x <- [0 .. columns m - 1]
>>> y <- [0 .. rows m - 1]
>>> guard $ x /= 1 && y /= 1
>>> return $ at m x y
>>
>>
>>
>
>
> ------------------------------
>
> Message: 5
> Date: Fri, 29 Jan 2010 10:01:36 +0000
> From: Luca Ciciriello <[email protected]>
> Subject: [Haskell-beginners] subset - a little add
> To: <[email protected]>
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset="iso-8859-1"
>
>
> Thanks Daniel.
>
> Yes my function operate only in a set-theory contest and your solution:
>
> subset xs ys = all (`elem` ys) xs
>
> is indeed more elegant than mine.
>
> Thanks again for your help.
>
> Luca.
>
>> From: [email protected]
>> To: [email protected]
>> Subject: Re: [Haskell-beginners] subset - a little add
>> Date: Fri, 29 Jan 2010 10:06:29 +0100
>> CC: [email protected]
>>
>> Am Freitag 29 Januar 2010 08:36:35 schrieb Luca Ciciriello:
>> > Just a little add to may previous mail.
>> >
>> > The solution I've found from myself is:
>> >
>> >
>> >
>> > subset :: [String] -> [String] -> Bool
>> > subset xs ys = and [elem x ys | x <- xs]
>> >
>>
>> Variant:
>>
>> subset xs ys = all (`elem` ys) xs
>>
>> but is that really what you want? That says subset [1,1,1,1] [1] ~> True.
>> If you regard your lists as representatives of sets (as the name suggests),
>> then that's correct, otherwise not.
>>
>> However, this is O(length xs * length ys). If you need it only for types
>> belonging to Ord, a much better way is
>>
>> import qualified Data.Set as Set
>> import Data.Set (fromList, isSubsetOf, ...)
>>
>> subset xs ys = fromList xs `isSubsetOf` fromList ys
>>
>> or, if you don't want to depend on Data.Set,
>>
>> subset xs ys = sort xs `isOrderedSublistOf` sort ys
>>
>> xxs@(x:xs) `isOrderedSublistOf` (y:ys)
>> | x < y = False
>> | x == y = xs `isOrderedSublistOf` ys
>> | otherwise = xxs `isOrderedSublistOf` ys
>> [] `isOrderedSublistOf` _ = True
>> _ `isOrderedSublistOf` [] = False
>>
>> >
>> >
>> > My question is if exists a more elegant way to do that.
>> >
>> >
>> >
>> > Luca.
>>
>
>
>
> Not got a Hotmail account? Sign-up now - Free
>
> _________________________________________________________________
> Send us your Hotmail stories and be featured in our newsletter
> http://clk.atdmt.com/UKM/go/195013117/direct/01/
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://www.haskell.org/pipermail/beginners/attachments/20100129/5941baf3/attachment-0001.html
>
> ------------------------------
>
> Message: 6
> Date: Fri, 29 Jan 2010 11:31:33 +0100
> From: Daniel Fischer <[email protected]>
> Subject: Re: [Haskell-beginners] Could not deduce (Matrix m (Maybe a))
> from the context (Matrix m a)
> To: Lyndon Maydwell <[email protected]>
> Cc: [email protected]
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset="utf-8"
>
> Am Freitag 29 Januar 2010 10:45:32 schrieb Lyndon Maydwell:
>> Thanks Daniel.
>>
>> It works, but I'm a bit confused as to why the extra type information is
>> needed.
>
> Well, you call fromRows on the result of vicinityRows.
>
> vicinityRows m x y :: [[Maybe a]]
>
> fromRows :: Matrix mat b => [[b]] -> mat b
>
> So for
>
> fromRows (vicinityRows m x y)
>
> to be well typed, you need an
>
> instance Matrix matr (Maybe a) where ...
>
> for some matrix type matr. That has to come from somewhere. It might come
> from an
>
> instance Matrix m a => Matrix m (Maybe a) where ...
>
> or you can supply it as a constraint on the function vicinityMatrix.
>
> However, having a look, none of the methods in the class look like they
> depend on the actual type a, so it might be better to have
>
> class Matrix m where
> fromRows :: [[a]] -> m a
> toList :: m a -> [a]
> rows :: m a -> Integer
> columns :: m a -> Integer
> row :: m a -> Integer -> [a]
> column :: m a -> Integer -> [a]
> at :: m a -> Integer -> Integer -> a
> (!!!) :: m a -> Integer -> Integer -> a
> vicinityRows :: m a -> Integer -> Integer -> [[Maybe a]]
> vicinityMatrix :: m a -> Integer -> Integer -> m (Maybe a)
> -- No constraint needed!!
> neighbours :: m a -> Integer -> Integer -> [a]
>
>>
>> On Fri, Jan 29, 2010 at 5:17 PM, Daniel Fischer
>>
>> <[email protected]> wrote:
>> >
>> > Well, the error says the compiler (the type checker) can't deduce the
>> > context (Matrix m (Maybe a)) from the givens. If you supply that
>> > information,
>> >
>> > vicinityMatrix :: Matrix m (Maybe a) =>
>> > m a -> Integer -> Integer -> m (Maybe a)
>> >
>> > it'll work.
>> >
>> >> Thanks guys.
>> >>
>> >> ---
>> >>
>> >> My Matrix class definition follows below:
>> >>
>> >> module Matrix (Matrix) where
>> >>
>> >> import Data.Array
>> >> import Data.Maybe (catMaybes)
>> >> import Control.Monad (guard)
>> >>
>> >> class Matrix m a
>> >> where
>> >> fromRows :: [[a]] -> m a
> <snip>
>> >> vicinityRows :: m a -> Integer -> Integer -> [[Maybe a]]
>> >> vicinityMatrix :: m a -> Integer -> Integer -> m (Maybe a)
>
>
> ------------------------------
>
> Message: 7
> Date: Fri, 29 Jan 2010 12:59:30 +0200
> From: Gabi <[email protected]>
> Subject: [Haskell-beginners] PI calculation - Newbie question
> To: [email protected]
> Message-ID:
> <[email protected]>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Hi Group,
>
> I am just trying to learn the lang and Implemented this PI calculator.
> It is really slow and very memory consuming (much much slower than its
> equivalent in Clojure for instance)
>
> I think the problem is in "rs <- sequence (replicate n isRandIn)" -
> But I don't know how to get around it (how do I get a lazy sequence of
> rs? Is it the problem anyway?)
>
> -- p.hs simple PI calculator, using the Monte Carlo Method
>
> import System( getArgs )
> import System.Random
> inCirc :: (Double, Double) -> Int
> inCirc (x,y) = if ((dx * dx) + (dy * dy)) < 0.25
> then 1
> else 0
> where dx = x - 0.5
> dy = y - 0.5
>
>
> randPoint :: IO (Double, Double)
> randPoint = do
> x <-getStdRandom (randomR (0, 1 :: Double))
> y <-getStdRandom (randomR (0, 1 :: Double))
> return (x, y)
>
>
> isRandIn = do
> p <- randPoint
> return (inCirc p)
>
>
> main = do
> args <- getArgs
> let n = if null args
> then 10000
> else read $ (head args)::Int
>
> rs <- sequence (replicate n isRandIn)
> let pi = (fromIntegral(sum rs) / fromIntegral n) * 4
> print pi
>
>
> ------------------------------
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
> End of Beginners Digest, Vol 19, Issue 30
> *****************************************
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 19, Issue 31
*****************************************