want to post haskell question

hi I have a question about haskell coding. Matrices One of many ways to define matrices in Haskell is the list of matrix rows, where a row is a list of double precision oating point numbers: type Matrix=[[Double]] Using this type you should define the following functions (definition of dim is given as an inspirational example). dim :: Matrix -> (Int,Int) --returns matrix dimension (number of rows and columns) dim m = if (not.isMatrix) m then error "Not a matrix" else (length m, length (head m)) isMatrix :: Matrix -> Bool --checks whether all rows have the same length isMatrix m = and [length (head m) == length a | a <- m] I have done upto here and I am stuck on multSM. multSM :: Double -> Matrix -> Matrix --multiplies a scalar and a matrix when I do: multSM d m = [[(b*a)| b<-[d], a<-(head m)]] or [map (*d) (head m)] ...I get (using Hugs): Main> multSM 3 [[2,3,4],[1,3,4],[6,3,0]] [[6.0,9.0,12.0]] So I solve 3 [2,3,4] but I do not know how to get the rest 3 [[1,3,4],[6,3,0]. Please help me on this problem.

Hi,
Try to understand this:
type Matrix = [[Double]]
multSM :: Double -> Matrix -> Matrix
multSM d m = map (\ i -> map (\ j -> d*j ) i) m
Now try this for multSM (instead of the above definition)
multSM = map . map . (*)
Hope it helps,
On 2 February 2010 09:35, 조광래
hi I have a question about haskell coding.
Matrices
One of many ways to define matrices in Haskell is the list of matrix rows, where a row is a list of double precision oating point numbers:
type Matrix=[[Double]]
Using this type you should define the following functions (definition of dim is given as an inspirational example).
dim :: Matrix -> (Int,Int) --returns matrix dimension (number of rows and columns) dim m = if (not.isMatrix) m then error "Not a matrix" else (length m, length (head m))
isMatrix :: Matrix -> Bool --checks whether all rows have the same length isMatrix m = and [length (head m) == length a | a <- m]
I have done upto here and I am stuck on multSM.
multSM :: Double -> Matrix -> Matrix --multiplies a scalar and a matrix
when I do:
multSM d m = [[(b*a)| b<-[d], a<-(head m)]] or [map (*d) (head m)]
...I get (using Hugs):
Main> multSM 3 [[2,3,4],[1,3,4],[6,3,0]] [[6.0,9.0,12.0]]
So I solve 3 [2,3,4] but I do not know how to get the rest 3 [[1,3,4],[6,3,0].
Please help me on this problem.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Ozgur Akgun

|multSM d m = [[(b*a)| b<-[d], a<-(head m)]]
Well, let's see what do we have here. We have []'s around something. "Something" is [(b*a)| b<-[d], a<-(head m)], which is just a legal Haskell value, so our "mutlSM d m" has to be a one-element list, with the only element being equal to what we put inside the brackets. It's like [1] or ["Hello"], just with more complex expression inside. This unique value is, as we've seen, [(b*a)| b<-[d], a<-(head m)]. Here we also have []'s around something, but this new "something" is NOT a legal Haskell value; it's a list comprehension. That means that we have something like this: multSM d m = [concatMap (\b -> concatMap (\a -> b*a) (head m)) [d]] Now, concatMap f [d] = f d, so multSM d m = [concatMap (\a -> d*a) (head m)] and if m = [m1:ms], then multSM d m = [concatMap (\a -> d*a) m1] So, you take first row ("head" only takes the first element of the list, you know), multiply it by "d", and make a one-element list of it.
participants (3)
-
Miguel Mitrofanov
-
Ozgur Akgun
-
조광래