0

That question sounds trivial but I really can't find anything helping me on how to deal with it. Here's a simple example to reproduce that on Haskell :

import Numeric.LinearAlgebra.HMatrix
madd :: Double -> Vector Double -> Vector Double
madd a v = a + v

when I load this it complains

Couldn't match expected type ‘Double’
              with actual type ‘Vector Double’

it thinks v is Double while it should be Vector Double, how can I force it in the pattern match to be so ?

please : I need to match the arguments, not just assign to a lambda function.

mahmoud fathy
  • 361
  • 1
  • 7
  • 17
  • 1
    No, it thinks `a` should be a `Vector Double`, and you specify it as a `Double`. Note that in Haskell, the two operands of the `+` should be the same, and that Haskell has no subclassing or implicit type casts. – Willem Van Onsem Mar 26 '18 at 18:07
  • 2
    You can use `cmap (+a) v`. – Willem Van Onsem Mar 26 '18 at 18:08
  • 1
    How is this question about pattern matching? You're not doing any pattern matching here. If the real use case is more tricky than `madd a v = a + v` (which by the way is just a complicated way of writing `madd = (+)`), you should perhaps explain what the real use case is. – leftaroundabout Mar 26 '18 at 18:43
  • it works ... more elaboratively `cmap (\x -> x+a) v` because thie other syntax gets me confused .. But why ? Do I need cmap always in order to d operations on hmatrix vectors ! – mahmoud fathy Mar 26 '18 at 18:58
  • Well, you can certainly add vectors to vectors using `+` directly, but scalars are not vectors (no matter what Matlab et al. would have you believe). As I already commented in that other question, Haskell really doesn't lend itself well to an array-based workflow. This is somewhat _by design_: we like to give everything the type that it's _conceptually represents_, not say “oh well, everything is an array/vector/matrix/tensor/numberthingy”. – leftaroundabout Mar 26 '18 at 19:02
  • @leftaroundabout the question is about pattern matching. I need the arguments with the function. In this example `v` doesn't match the `Vector Double` which is confusing and I can't wrap my head around it ! – mahmoud fathy Mar 26 '18 at 19:05
  • @WillemVanOnsem the full error message `In the second argument of ‘(+)’, namely ‘v’ In the expression: a + v In an equation for ‘madd’: madd a v = a + v` seems concerned about `v` not `a` – mahmoud fathy Mar 26 '18 at 19:06
  • Are you sure you want to add a number to an _array_? When Matlab does something like that, it usually means the array is just a poor man's representation of a _function_ (sampled in some way). Why not just represent it as a function then? — Also: are you sure you've understood what pattern matching is? It seems you're using the term to mean _type matching_, but that's something quite different. – leftaroundabout Mar 26 '18 at 19:07

0 Answers0