I have two questions:

1.) does the order matter (is (1,2) in (2,1))?

2.) why use tuples instead of lists or sets?

By on 7/14/2011 12:06 AM ()

Hi:
(1) The order matters, (2,1) <> (1,2)
(2) I don't know which one is better for my question, but I think since tuple can have different number of elements, so it could be a good solution. But if it is easy to solve my question using list or set, I can also use list or set.
If you have any idea, show me your code. List or set is also OK for me.
Thanks,

By on 7/14/2011 9:30 AM ()

sorry the notifications seems out of order and I've seen this just right now. Chances be that you allready got your anwers but I'll try anyhow.

As much as I understand you just want to check if a Tuple/List A is a "prefix" of Tuple/List B. As I said it's hard to do this for tuples because they are "typed by item-count" (I hope you can understand what I want to say), but if you can assume the same type for each component lists are great and easy for this (if not you can always box the items first and use obj.Equals hoping that the types overload Equals with some sense - or implement your on IEqualityComparer or whatever).

So here is my try:

1
2
3
4
5
6
7
 

    let rec isPrefixOf a b =
        match (a,b) with
        | ([],_) -> true
        | (h::a),(h'::b) when h=h' -> isPrefixOf a b
        | _ -> false

You can test this with

1
2
3
4
5
6
7
8
9
10
11
12
13
 

    let tests = 
        isPrefixOf [] [1;2;3],
        isPrefixOf [] [],
        isPrefixOf [1] [2;3],
        isPrefixOf [1] [1;2;3],
        isPrefixOf [1;2] [1],
        isPrefixOf [1;2] [1;2],
        isPrefixOf [1;2] [1;2;3],
        isPrefixOf [1;2] [1;3;2]

By on 7/22/2011 1:09 AM ()
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