tags:

views:

80

answers:

2

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?

+1  A: 

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
I'd say that makes it like a switch statement that can also operated on the expression's structure...
phoebus
Let the value of the 'switch' expression then be the boolean expression 'x is of structure y'.
ProfK
+4  A: 

You may find the discussion here

http://stackoverflow.com/questions/199918/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.

Brian