0

I'm busy going through the very anaemic walk-through F# project provided by MS in VS 2010 Beta 2, and came across something that needs some explanation:

let rec SumList xs =
    match xs with
    | []    -> 0
    | y::ys -> y + SumList ys

I assume match is something like a switch in other languages, where an empty list results in a 0 return value, but the second case fascinates me. Does this tell the 'runtime|interpretor' to evaluate the match argument as y cons ys, or rather as 'if the argument is of the form y cons ys', recurs with y and ys?

ProfK
  • 49,207
  • 121
  • 399
  • 775

2 Answers2

4

You may find the discussion here

Explaining pattern matching vs switch

helpful. I think it's hard to sum up how pattern-matching works, but it's both a control-flow construct (a la switch) as well as a binding construct.

Community
  • 1
  • 1
Brian
  • 117,631
  • 17
  • 236
  • 300
1

A pattern matching is not like a switch statement : switch operates on the value of an expression, whereas match can also operate on the expression's structure.

Pierre Bourdon
  • 10,521
  • 4
  • 33
  • 27
  • 1
    I'd say that makes it like a switch statement that can also operated on the expression's structure... – phoebus Nov 26 '09 at 19:26
  • Let the value of the 'switch' expression then be the boolean expression 'x is of structure y'. – ProfK Nov 26 '09 at 20:16