(Forgive me for multiple posts here) pattern matching on empty sets

107 views
Skip to first unread message

Thomas Clarke

unread,
Sep 22, 2015, 1:17:21 PM9/22/15
to F# Discussions
I use pattern matching a lot, and now have some trouble with sets. Is there any way to pattern match on an empty set? That is for me a common use case.

I'd use lists except that I don't then have a complete set of set operations on them (I actually need intersection and difference).

The only other solution is ugly - to use if then else for all the set empty matching.

I'm posting various newbie questions here: let me know if these posts should better go to some other place?

Thanks, Tom

Stachu Korick

unread,
Sep 22, 2015, 1:25:55 PM9/22/15
to fsharp-o...@googlegroups.com
Personally I love the questions coming in.
I'm getting little bites of F# knowledge throughout my workday, getting me out of the depressing land of OOP.
Please continue so I can escape into sanity throughout the day.
You're my only hope.

Here's my really quick attempt in linqpad.


let (|EmptySet|_|) (set : Set<'T>) = 
if set.IsEmpty then Some()
else None
let set1 = new Set<int>([1; 2])
let set2 = new Set<int>([])

let printIfEmpty set =
match set with
| EmptySet -> printfn "yay"
| _ -> ()
printIfEmpty set1
printIfEmpty set2


--
--
To post, send email to fsharp-o...@googlegroups.com
To unsubscribe, send email to
fsharp-opensou...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/fsharp-opensource
---
You received this message because you are subscribed to the Google Groups "F# Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fsharp-opensou...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Isaac Abraham

unread,
Sep 22, 2015, 1:49:19 PM9/22/15
to Thomas Clarke, F# Discussions

Personally I think it’s great to see some questions here, so I would continue if I were you, but in terms of other resources: -

 

-        Twitter #fsharp is probably not the best for this, although I’ve done it in conjunction with gists in the past with some success.

-        Slack - https://functionalprogramming.slack.com in #fsharp. You might find this useful if you want a bit more of a discussion than an email thread. I think that there’s a Gitter room somewhere as well?

Isaac Abraham

unread,
Sep 22, 2015, 1:51:26 PM9/22/15
to fsharp-o...@googlegroups.com

Hey

 

Just a quick question – I’m assuming from the reference to linqpad that you’re a Windows user, probably using Visual Studio? If so – why use Linqpad over standard .fsx F# scripts? Not criticising you – but genuinely interested if there’s any benefits of using it.

 

 


From: Stachu Korick
Sent: 22 September 2015 18:25
To: fsharp-o...@googlegroups.com
Subject: Re: (Forgive me for multiple posts here) pattern matching on empty sets

 

 

Personally I love the questions coming in.

I'm getting little bites of F# knowledge throughout my workday, getting me out of the depressing land of OOP.

Please continue so I can escape into sanity throughout the day.

You're my only hope.

 

Here's my really quick attempt in linqpad.

 

 

let (|EmptySet|_|) (set : Set<'T>) = 

          if set.IsEmpty then Some()

          else None

         

let set1 = new Set<int>([1; 2])

let set2 = new Set<int>([])

 

let printIfEmpty set =

          match set with

          | EmptySet -> printfn "yay"

          | _ -> ()

         

printIfEmpty set1

printIfEmpty set2

 

On Tue, Sep 22, 2015 at 1:17 PM, Thomas Clarke <clar...@gmail.com> wrote:

Stachu Korick

unread,
Sep 22, 2015, 1:59:01 PM9/22/15
to fsharp-o...@googlegroups.com
I'm indeed a windows user (ASP.NET MVC + C#, etc. dayjob) on VS2013.
If I need to do some really quick scripting I'll use fsi in ConsoleZ. I use F# as a quick calculator pretty frequently.
If I'm not 100% sure on the syntax (active patterns) then I find linqpad's quick hit of 'f5' a bit quicker than saving a text file and then running the .fsx file.

I should really switch to a workflow in sublime though.

I would use the new 'atomic' thing released the other day for F# stuff like this but I haven't had a chance to check it out yet.
Message has been deleted

Thomas Clarke

unread,
Sep 22, 2015, 4:04:48 PM9/22/15
to F# Discussions
Thanks Stachu -

I had not though of wrapping sets in a union and of course you are right, it solves the problem!

Another solution is to turn the sets into lists just before a I match them. In my use case I have several sets which are constructed in s similar fashion so I map stuff over lists to generate them. In that case converting everything back to a list just for the pattern match is low cost - though in another case it would maybe not be.

I do generally love FSharp syntax. It is not as clean as pure ML, because of the compromises to allow object member application, but the two together are very usable.

I have not yet got to the stage of being very frustrated by a not very powerful type system - that may yet happen! The good side of that is that the excellent type inference from partial parse is wonderful, and would fail with a richer type system.

One thing - because method lookup while writing code is so powerful it would be really nice to have a way to see static Type functions as well as methods, since it is not - especially when learning - clear where the stuff you need is.

So that "abc".<auto-complete>   would generate a list of properties, methods, as now and then also String.? functions.


Isaac Abraham

unread,
Sep 22, 2015, 7:50:10 PM9/22/15
to Stachu Korick, fsharp-o...@googlegroups.com

It sounds to me like you’ve missed out on all the VS integration with F# scripts! You can create a standalone F# script file in Visual Studio (File -> New, it doesn’t even need to be part of a project or solution), with full intellisense in the editor etc. You also then can highlight arbitrary bits of code and send them to FSI directly in Visual Studio and see the results. There’s also the F# Power Tools VS extension which provides refactors and improved colouring etc.

 

I think you’ll find this a much, much more effective way to write and explore F# than through a console or Linqpad.

Stachu Korick

unread,
Sep 22, 2015, 9:43:24 PM9/22/15
to Isaac Abraham, fsharp-o...@googlegroups.com
Ah, I actually was aware of this. VS is just really fat (mem usage, takes a bit to load) and it's kind of a disincentive to open it :(
Fsi or linqpad open instantly in comparison.

Thanks, though!
Reply all
Reply to author
Forward
0 new messages