1
2
3
4
5
let a = 2 in 
match (4,5,6) with     
  | (1,2,3) -> printfn "Yes"     
  | (4,_a,b) when _a=a -> printfn "No %i %i" a b     
  | _ -> printfn "Fell through" ; printfn "a is %i" a 

I thought this would also work, but it doesn't seem to -- maybe this only works for top-level matches?

1
2
3
4
5
6
7
8
9
10
 

[<Literal>] 
let a = 2 
match (4,5,6) with 
    | (1,2,3) -> printfn "Yes" 
    | (4,a,b) -> printfn "No %i %i" a b 
    | _ -> printfn "Fell through" ; 
printfn "a is %i" a 
By on 12/30/2010 10:33 AM ()

To be used as a literal pattern, the identifier must start with a capital letter. E.g., this will work:

1
2
3
4
5
6
7
8
9
10
 

[<Literal>] 
let A = 2 

match (4,5,6) with 
    | (1,2,3) -> printfn "Yes" 
    | (4,A,b) -> printfn "No %i %i" A b 
    | _ -> printfn "Fell through" ; 
By on 12/30/2010 11:07 AM ()

Hmmm, thanks for the quick replies as always, but I'm not sure this is really what I was looking for - perhaps my example was too simplistic.

The [<Literal>] qualifier seems to have a similar effect to a #define preprocessor statement in C, doesn't it? I.E. it's just making the symbol 'A' an alias for the value '2' (in this case). A better example would be if the match statement was within a function body and I wanted to create a pattern that was derived in some way from the parameter(s) of the function - see the difference?

I can see how to do it with the "when" clause and that seems to be the best option under the circumstances. When I made the original posting I thought that I just needed to be able to substitute values at runtime into the pattern, but I've now realised that I'm really trying to implement something closer to Prolog unification which is a bi-directional process and that simply will not work in this simplistic manner...

Martin

By on 12/31/2010 7:29 AM ()

If you've done prolog, then then the first thing to realize is that pattern matching is not unification... -however, it can cover many of the cases that are one-way, and serves for advanced dispatching based on values in structured constructs. (The literal needing to be capital is just the opposite of prolog - where the capital would be a variable.) Having the "when" statement makes it obvious what is going on.

However, a backtrack{} monad/bulider for F# would be interesting...

By on 12/31/2010 9:14 AM ()

indeed...

I don't actually need the back-tracking of Prolog, but I do need the bi-directional effect of Prolog Unification - I'm effectively trying to match a tuple of data with a tuple that can contain a combination of literals and variables. Literals in the pattern must be equal to the corresponding element of the input tuple for a match to occur. variables in the pattern must either be unbound, in which case they take on the value of the corresponding element in the input tuple, or they must be bound to a value which is equal to that in the corresponding element of the input tuple - if you see what I mean...

When I made the original posting, I was thinking in a Prolog mindset. Now I'm coming back to F#. Since all the expressions I'm interested in are representable as strings, I'm defining a data type which it a "string option" and a function which compares one of these with the input term. If the string option isSome, then my function checks for equality with the input string and returns a tuple that is a bool representing the equality test and the bound value. If the string option isNone, then i interpret this as an unbound variable and return the tuple (true, {value of input string}) which represents the new binding...

Martin

By on 12/31/2010 10:28 AM ()

>To be used as a literal pattern, the identifier must start with a capital letter

That just ain't right... :)
Any idea what the rationale is here?? (Whatever the reason is, it doesn't seem like [Literal] should silently fail w/lower-case.)

By on 12/30/2010 3:53 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper