News for hackers. Stuff that matters.
Re:F# 4.0 Preview by noblethrasher on Thursday November 13, 2014 @03:30AM (#8600492)
Really short answer:

F# is to C# as C is to Assembler (I regularly use both C# and F# and don't quite agree with this, but it is defensible).

Short answer:

Programming, especially OOP, has a lot of prescriptions: things like coding against interfaces rather than implementations, using abstractions, the law of Demeter, avoiding mutations, prefer composition to inheritance, etc.

Basically, you can think of F# as a language that compiles down to C#, but follows all of the best practices without fuss.

Consider this example of F# code:

		type Shape =
			| Circle of float
			| Square of float

		let area s =
			match s with
			| Circle r -> Math.PI * r * r
			| Square sd -> sd * sd
			
		let perimeter s =
			match s with
			| Circle r -> 2 * Math.PI * r
			| Square sd -> sd * 4

Here is the moral equivalent in C#:

	 public abstract class Shape
	 {
	 	public abstract float Area();
	 	public abstract float Perimeter();
	 }
     
	 public sealed class Circle
	 {
	 	readonly float r;
	 	
	 	public Circle(float r)
	 	{
	 		this.r = r;
	 	}
	 	
	 	public override float Perimeter()
	 	{
	 		return Math.PI * 2 * r;
	 	}
	 	
	 	public override float Area()
	 	{
	 		return Math.PI * r * r;
	 	}
	 }
     
	 public sealed class Square
	 {
	 	readonly float side;
	 	
	 	public Circle(float side)
	 	{
	 		this.side = side;
	 	}
	 	
	 	public override float Perimeter()
	 	{
	 		return 4 * side;
	 	}
	 	
	 	public override float Area()
	 	{
	 		return side * side;
	 	}
	 }
A couple of things to note:

* The C# code needs 5 times as many line as the F# code (adding a new shape to F# will require about 3 lines, but the corresponding C# class definition will be at least 15 lines).

* The F# code is idiomatic, but hardly any C# programmer seals classes or defines them as immutable by default, even though it is a good practice.

That said, there are some advantages that C# enjoys. Consider our example code: Even though C# requires more lines of code to define a derivative of Shape, at a certain point it will always be easier to add new classes to C# than the corresponding variant to F# (see "The Expression Problem" - on the other hand, adding new functions that apply to Shape will be easier in F# than adding the corresponding new methods in C#). Also, F# is supposed to be a hardcore statically-typed language, but there are some advanced concepts (e.g. type classes) that are easy to realize in C# yet not in F#.

Re:F# 4.0 Preview by wwosik on Thursday November 13, 2014 @04:48AM (#8600711)
Well, C# idiomatic would be rather

  public interface IShape 
  { 
      float Area { get; }
      float Perimeter { get; }
  }

  public Circle : IShape 
  {
      readonly float r ;

      public Circle(float r)
      {
         this.r = r;
      }
      
     public float Area { get { return Math.PI * r * r; } }
     public float Perimeter { get { return 2 * Math.PI * r; } }
  }

  public Square: IShape 
  {
      readonly float a ;

      public Square(float a)
      {
         this.r = r;
      }
      
     public float Area { get { return  a * a; } }
     public float Perimeter { get { return 4 * a; } }
  }
Also, with this one you can create new Shapes in another project. With F# solution there, you must have access to original code.
Re:F# 4.0 Preview by noblethrasher on Thursday November 13, 2014 @05:16AM (#8600776)
I agree that it would be more idiomatic. I disagree that it is good.

Abstract classes have disjunctive semantics (logical OR) whereas interfaces have conjunctive semantics (logical AND).

The actual important thing that I wanted to capture is that an instance of Shape can only be one kind of shape at a time. With an interface a class could be more than one kind of Shape.

    interface IFoo : IShape
    { 
        //some stuff
    }

    public class SomeShape : IShape, IFoo
    {
        //I am a circle AND a square!
    }
Re:F# 4.0 Preview by endeavour on Thursday November 13, 2014 @04:55AM (#8600731)
You've just described the expression problem.

http://c2.com/cgi/wiki?ExpressionProblem

Re:F# 4.0 Preview by achr2 on Thursday November 13, 2014 @08:30AM (#8601373)
I actually think C# (and many OOP languages) gets around this fairly well, by having virtual and extension methods. Many types of functions can be added easily without needing to go back through every inheritor.
Re:F# 4.0 Preview by louthy on Thursday November 13, 2014 @11:57AM (#8602541)
* EDIT - realised I responded with an answer not related to the topic; I'll leave this here as a point-of-reference for the OO side of F# *

Extension methods don't extend the type. They extend the functionality attached to the type.

F# has these too [1] (actually more powerful versions):

The thing about using sum-types (the original Shape example) is that they're not supposed to be extended. You're defining the range of the type, which allows for powerful inference of completeness when using them. If you need extensible classes then F# can do those too, it can represent interfaces, abstract classes and concrete classes [2][3][4], and can even generate ad-hoc classes from an interface by using Object Expressions [5].

I personally try to stick to the pure functional approach, but it can be very handy when interoperating with the .NET framework, or other C# based libraries.

[1] http://fsharpforfunandprofit.com/posts/type-extensions/

[2] http://fsharpforfunandprofit.com/posts/classes/

[3] http://fsharpforfunandprofit.com/posts/inheritance/

[4] http://fsharpforfunandprofit.com/posts/interfaces/

[5] http://fsharpforfunandprofit.com/posts/object-expressions/

Re:F# 4.0 Preview by marxama on Thursday November 13, 2014 @08:57AM (#8601485)
Another thing is that the F# example has equality semantics built in, whereas in the C# example, you'd have to override Equals (and GetHashCode), adding more lines still (for some tedious work that you shouldn't have to do).
Re:F# 4.0 Preview by louthy on Thursday November 13, 2014 @07:54AM (#8601248)
Your F# example can be more concise:

    type Shape =
        | Circle of float
        | Square of float

    let area = function 
        | Circle r -> Math.PI * r * r
        | Square sd -> sd * sd

    let perimeter = function 
        | Circle r -> 2.0 * Math.PI * r
        | Square sd -> sd * 4.0
Re:F# 4.0 Preview by noblethrasher on Thursday November 13, 2014 @10:57AM (#8602173)
Well, that's just... neat.
Re:F# 4.0 Preview by CmonDev on Thursday November 13, 2014 @04:38AM (#8600673)
I noticed you sealed the C# classes. What if you want to keep it extensible, which makes sense? How will that extensible pattern matching look then?

Also would be interesting to see how long would C#6 code be (twice as short?).

Apart from the sample, I also don't understand why F#'s naming patterns are not consistent with the rest of .NET (in terms of starting letter case). Feels alien.

Also how come this was considered a sane syntax for arrays?

[|1; 2; 3|]

instead of

[1, 2, 3]

Even Haskell's syntax is less alien.

Re:F# 4.0 Preview by noblethrasher on Thursday November 13, 2014 @04:46AM (#8600706)
Not to mention indexers.

Well, programming languages do have a prescriptive aspect. Arrays in C# 1.0 were more fundamental than List whereas the situation in F# 1.0 was the reverse, and F# doesn't really want you to use arrays as much as it wants you to use list.

...

Personally, I always mark my classes as either sealed or abstract. In OO terms, I only care about extending the Shape class, not the individual subclasses because those themselves are are already extensions... of the Shape class. If I did want to extend, say, the Circle class, I would probably make a new abstract class called Ellipse that derived from Shape and then derive Circle from Ellipse.

But, I don't really think in strict OO terms any more. C# has a decent type system (despite what my fellow ML programmers might say), but one of the mistakes of the language (i.e. the spec) is that it conflates types with classes. Those are different things. You may see three classes, but I see one type. In this case it's a sum type called Shape that ranges over Circle and Square. A sum type , as you may know, is just the type-theoretic version of logical OR.

Indeed, it wasn't until I learned about sum types that I finally understood why you would use an abstract class as opposed to an interface: abstract classes are for when you want to say that a Foo is a Bar OR a Baz OR a Biz whereas interfaces are for when you want to say that Foo is a Bar AND a Baz, AND a Biz.

Re:F# 4.0 Preview by CmonDev on Thursday November 13, 2014 @11:14AM (#8602280)
> "In this case it's a sum type called Shape that ranges over Circle and Square"

How do I extend Shape to include Triangle without modifying the current code (e.g. in a separate file)?

Re:F# 4.0 Preview by noblethrasher on Thursday November 13, 2014 @12:13PM (#8602633)
You would have to modify the code, and as I mentioned, it can be pretty painful in F# and other ML languages if you already have a bunch of functions that apply to Shape.

Subclassing is a good and useful tool that simply works better in C# than F#. But, just as importantly, class hierarchies are often a poor way to model a problem.

Re:F# 4.0 Preview by CmonDev on Thursday November 13, 2014 @06:29PM (#8604855)
Fair enough, thanks for answers.
Re:F# 4.0 Preview by noblethrasher on Thursday November 13, 2014 @07:07PM (#8605018)
You're welcome.

It just occurred to me that you may have been asking about extending Shape in the C# example. In that case, you'd just subclass Shape as normal[1]. Some people argue that sum types shouldn't be extended, but I never really understood that. Basically, C# lets you define unbounded sum types (in the form of subclassing) whereas idiomatic F# more or less demands that you know up front how many variants you're going to need.

[1] Although, sometimes I do define my classes to prevent this by marking the constructor(s) of the abstract base class as private, and then writing the derived class definitions within the body of the base class.

Re:F# 4.0 Preview by CmonDev on Friday November 14, 2014 @05:54AM (#8606580)
I have seen opinions that C# is good for architecture and F# is good for implementation of components. I might opt for such an approach. Really wish I had a default non-null immutability in C# though.
Re:F# 4.0 Preview by enricosada on Thursday November 13, 2014 @05:54AM (#8600866)
F# is ML family (like ocaml) With F# you use tuple a lot so is nicer have ',' as separator for tuple parts.

  let tuple1 = (1, 3) //tuple (int, int)
  let tuple2 = 1, 3 //same as before, parens are optional

  let list = [ 1; 2; 3 ] //list of int

  let array = [| 1; 2; 3; |] //array of int

  let listOfTuple1 = [ (0,0); (1,2); (4,5) ] //list of tuple (int, int)
  let listOfTuple2 = [  0,0;   1,2;   4,5  ] //same as before
see https://dotnetfiddle.net/vzeZvJ

nice and fast syntax guide http://fsharpforfunandprofit.com/posts/fsharp-in-60-seconds/

Re:F# 4.0 Preview by frowaway001 on Thursday November 13, 2014 @09:25AM (#8601622)
I think the question is "why has ML such a nice syntax, and OCaml managed to make it worse all across the board"?
Re:F# 4.0 Preview by Xophmeister on Thursday November 13, 2014 @04:47AM (#8600708)
IIRC, F# is derived from (or, at least, heavily influenced by) OCaml and that's the OCaml syntax for arrays. OCaml lists (I don't know F#, but presumably it's the same) have the [x; y; z] format. Note that lists and arrays are different: They're both homogeneous, but arrays have a fixed length while lists can shrink/grow; indexing arrays is O(1), whereas you have to traverse lists at O(n). In functional programming, it's a very common pattern to work with lists, hence the nicer syntax.
Re:F# 4.0 Preview by iconjack on Thursday November 13, 2014 @11:13AM (#8602274)
There are only so many ASCII symbols to use for separators and brackets when creating syntax for literals. Think of [| and |] as〚 and 〛 (double square brackets), then it doesn't look so bad, I think.
Re:F# 4.0 Preview by AndrewDucker on Thursday November 13, 2014 @04:45AM (#8600703)
Yeah, it looks a lot cleaner - but if you make that code available as a library then other people can't add new shapes easily, or deal with shapes that have more than one length (e.g. a rectangle, which has two).
Re:F# 4.0 Preview by couchand on Thursday November 13, 2014 @09:16AM (#8601563)
Well, yes, you can add a rectangle quite easily:

    type Shape =
        | Circle of float
        | Square of float
        | Rectangle of float * float
The thought about adding new shapes (mentioned several times in this thread) is just misplaced. If you're releasing library code people should not be extending your built-in types, that's bad form -- consumers of a library should interact with it through interfaces only.
Re:F# 4.0 Preview by ufo on Thursday November 13, 2014 @11:05AM (#8602223)
The flipside is that its very easy for your library users write a new function that takes a Shape as a parameter. If you model things with a class hierarchy its easy to add a new Shape subclass but you can't add any new methods to the Shape interface.
Re:F# 4.0 Preview by wwosik on Thursday November 13, 2014 @04:52AM (#8600718)
That comment about shapes with more than one defining size property is very valid.
Re:F# 4.0 Preview by zer0nes on Thursday November 13, 2014 @05:07AM (#8600750)
This is a rather poor example. If you distribute the C# code above as a library, people can extend and introduce another shape, while they cannot do it easily with you F# code.
Re:F# 4.0 Preview by noblethrasher on Thursday November 13, 2014 @05:26AM (#8600798)
I did mention the Expression Problem[1] in my comment, and explicitly called out subclassing as a particular advantage of C# (the OP asked for criticism of F#).

Also, even though F# can also subclass, that facility is a bit weak since it can't define protected members.

[1]http://c2.com/cgi/wiki?ExpressionProblem - funny thing is that the linked page coincidentally uses the exact same example code.