[Haskell-beginners] wanto post haskell question
magnate.lee at gmail.com
Wed Feb 3 21:39:28 EST 2010
I think the following function will work for you:
multSM :: Double -> Matrix -> Matrix --multiplies a scalar and a matrix
multSM d m = [ [d * b | b <- a] | a <- m]
I just use nested list comprehension.
2010/2/2 조광래 <kwangraecho at gmail.com>:
> hi I have a question about haskell coding.
> 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
> 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]]
> So I solve 3 [2,3,4] but I do not know how to get the rest 3
> Please help me on this problem.
> Beginners mailing list
> Beginners at haskell.org
More information about the Beginners