10 ms·
F# 4.0 Preview
- galago 12y agoIt would be useful if someone knowledgeable and critical could explain clearly the use of this language. The marketing blather doesn't give me much. I'm not a software engineer, so this just seems like a bunch of syntax wankery. I'm probably wrong, I'm just looking for a brief explanation as to why.
- alkonaut 12y agoIt's a functional language for the .NET platform. So you'll want it if you want a functional language and you want to use the .NET platform. Does that answer your question?
- CmonDev 12y agoF# is a hybrid language just as C# is. Just a different flavour of the mix.
- alkonaut 12y agoYes a longer answer is that it is "functional first" just like C# is "OO first". It's safe to say that f# is much closer to ML than Java tough, so it's very much a functional language in a way that C# is not.
- noblethrasher 12y agoReally 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#.
- CmonDev 12y agoI 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.
- noblethrasher 12y agoNot 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.
- CmonDev 12y ago> "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)?
- enricosada 12y agoThis post is about latest feature added to f# language (syntax cleanup, speed, etc) why use a functional language? see http://fsharpforfunandprofit.com/posts/ten-reasons-not-to-use-a-functional-programming-language/ http://fsharpforfunandprofit.com/posts/ten-reasons-not-to-us... Why use f#? see http://fsharpforfunandprofit.com/why-use-fsharp/ http://fsharpforfunandprofit.com/why-use-fsharp/ (concise for programmers) or http://fpbridge.co.uk/why-fsharp.html http://fpbridge.co.uk/why-fsharp.html (more high level) more info about fsharp at http://fsharp.org/ http://fsharp.org/ my take: #ecosystem - vibrant and friendly community - open source (long time ago, use pr, wrote by community) - cross plat (linux/mac/win/ios/android) - is .NET, same standard library as C# or VB.NET and virtual machine (CLR). Finally open source - use nuget components written for .net - third party component with commercial support #language - fun to use - concise, i can easy write code without boilerplate - multi-paradigm (functional first, but can do oop like c#) - easy interop with c#, native - fast - help write correct code (no null reference => no null reference exception, pattern matching, - easy async, parallel, gpu - unit of measure (like 1<m/s> ) and a lot, read the links above for a better (and faster) summary
- JackMorgan 12y agoF# is a general purpose language. It can do anything Java/C# can do, only shorter and safer. Here is an example of a simple language tool not in Java/C# that makes it easier to write, read, and is safer. http://deliberate-software.com/function-pattern-matching/ http://deliberate-software.com/function-pattern-matching/ F# can also call anything written in .NET, so it's easy to start using it right away. The biggest complaint people have is that it is a totally new language, and the parts that make it simpler and safer take effort to learn. In all honesty, several developers I know don't want to learn it, mostly because they only know C#, and do not believe the claims of speed and safety. They cannot imagine what could be different, so a day or two in, they just think of it as "syntax wankery", and never get past that point. Really, the different syntax just makes it about a third the size of the equivalent C#. That alone is fine, and usually is the reason people prefer languages like Ruby/Python. But the real power comes from deeply understanding computation expressions, the LINQ equivalent, and F# data structures. Those three allow code that is much safer and easier to write, without sacrificing any functionality. At my work, (Winforms app and an MVC4 web site) I use it anywhere I'd use C#.
- fsloth 12y agoGood explanations already, but I'll add my two cents. F# has: * Rich language constructs that encourage pithy and correct code (e.g. arrays and lists are a language type rather than a library datastructure and the syntax facilitates implementing very readably and succintly common operations on them) * The combination of algebraic data structures and pattern matching enables succinct implementation of solutions for lots of common programming problems. This was perhaps the biggest wow feature of the language when I learned to use it well. * Functional languages tend to encourage immutability. F# works idiomatically with mutable data as well, thus being well suited for e.g. numerical code. I would call this letting us have the cake and eat it too. * I don't think there are any specific downsides to F#. Some programs are better expressed in C# still but the .Net platform allows one to access those classes seamlessly from F# side. IMHO, these are all wins. There's not much unnecessary language wankery in F#, it was developed as an industrial tool from day 1. so most features have some rationale behind them.
- oddthink 12y agoI'll try to give a pragmatic example. At work, we've got a lot of analytics code in C# for model-estimation, pricing, etc. I spend a lot of time tweaking models, running them, then extracting and analyzing the results. F# works very well as a scripting language for me, where otherwise I would be using something like IronPython. The results-processing I could do from anything (R, python), but scripting the run (running N random samples of something, doing simple cross-validation, manually hacking a pseudo-hierarchical model, etc.) is much easier when I have direct access to the C# API. Since the codebase is always-evolving, the type checks of F# (as opposed to IronPython) are useful when going back to an old script. I also just enjoy using F#. I'd been hoping to learn a ML-family language for years and working at a F#-friendly shop has let me do that.
- virtualwhys 12y agoThis is pretty cool, in my day-to-day language, Scala, we cannot do the following: [('a',1);('b',2);('c',3)] |> List.map String In F# 4.0 the compiler infers that String takes 2 arguments and extracts the tuple accordingly. In Scala we're forced to manually extract the tuple when the recipient function takes more than a single argument. def plus(a: Int, b: Int) = a + b List((1,2),(3,4)) map plus // kaboom List((1,2),(3,4)).map{case(a,b)=> plus(a,b)} // ok We'll see where the Microsoft OSS movement goes. If in the not too distant future we can seamlessly take advantage of, for example, Type Providers and LINQ-to-SQL/Objects on Linux, I'll certainly give F# a deeper look. Side note: the next 3 to 4 years will be huge in computing, IMO. Microsoft stack is coming to Linux and OSX, Oracle is finally pushing forward Java and the JVM, and FP languages in general seem to be picking up steam in terms of adoption. Good times ahead for everyone ;-)
- pjmlp 12y ago> FP languages in general seem to be picking up steam in terms of adoption I have been looking forward to this since I learned Caml Light at the university back in 1996. Same applies to LP. It was been a long way.
- deleted 12y ago[deleted]
- deleted 12y ago[deleted]
- jbeja 12y agoI can't wait for .NET to be ported to Linux to try this, it looks awesome.
- steego 12y agoStay tuned: There are already a number of docker containers with F# on them using Mono. Because the F# compiler is open source, you can probably create your own container pretty easily based on the preview builds. https://github.com/crosbymichael/fsharp-docker https://github.com/crosbymichael/fsharp-docker
- sklogic 12y agoIt works just fine on Mono. Use the current F# from git.
- gnuvince 12y agoVery cool. After my M.Sc. I'm going to need to find a job, and since OCaml jobs are pretty much inexistant in Montreal, F# is my backup plan.
- genwin 12y agoGood thinking to be considering the demand. Why not a remote job? Also consider the trend. There's a site that ranks language popularity on a periodic basis.
- louthy 12y agoIt's great to see the gradual improvements in F#. I think it's a brilliant language. I was initially not too sure, because, even though I've been coding C# for a long time, my first foray into the functional world was Haskell - which is a stunning language. But when it comes to plain old let's-get-things-done pragmatism, F# wins out imo.
- FlyingSnake 12y agoF# is a wonderful language, albeit I'm still getting used to style. Is anyone here using F# for real project? It would be great to see it in wild.
- louthy 12y agoI'm using it on a number of proprietary projects. Check out [1] for a ton of 'in the wild' projects: [1] https://github.com/fsprojects https://github.com/fsprojects
- DanielBMarkham 12y agoBunch of folks are. I've used it for several. I think there have been a couple of AskHNs for this. I'd show mine but you wouldn't see any code! My latest is http://newspaper23.com http://newspaper23.com I use F# to generate static pages. This allows me to completely decouple the back-end from the display. I don't have interactivity yet, but I really don't see a need to add a framework to get it. F# and mono allows me to shoot for brutal simplicity. I like that. Many times projects that might be huge monstrocities in other languages turn out to be just a few well-coded modules running as Unix-y microservices in F#. People are starting to give F# a lot of love as a .NET language, then wondering "Where's the framework?" I'm coming around to the conclusion that it works best when there is no framework. Fun times ahead.
- deleted 12y ago[deleted]
- noblethrasher 12y ago> People are starting to give F# a lot of love as a .NET language, then wondering "Where's the framework?" I'm coming around to the conclusion that it works best when there is no framework. Yes, in F# and other ML-like languages you tend to use libraries more than frameworks. (I'm the original poster, and I've been extolling the virtues of ML-style functional programming throughout this thread, but I still owe you an email explaining why I think OOP is ultimately better than FP)
- jackfoxy 12y ago
- DanielBMarkham 12y agoNormalized collection modules is a big plus. It was always a pain having to cast something out of one thing and into another just to get some method that should have been there in the first place. Same goes for slicing. Some good stuff, guys!
- fallat 12y agoIf I've already been learning Haskell, should I keep going or stop, drop everything and learn F#? I'd like someone with F# and Haskell experience to answer.
- louthy 12y agoDepends on your requirements. I use F# day-to-day, but I think Haskell overall is a better language. There's some ugly aspects to F#, like its OO grammar, lack of typeclasses and seeming randomness to the casing (sometimes camelCase and sometimes PascalCase). These are all artefacts of it being a pragmatic tool in a toolset: its requirement to work with other .NET languages and frameworks (and Mono). So for a bit of ugliness, and some loosening of the type-system shackles, you get a whole ton of stuff. Haskell as I'm sure you're aware is much stricter and much harder to interoperate with. But much more refined as a language. Whilst you do get the "If it compiles it will probably work" thing with F#, it's nowhere near the level of Haskell. Haskell tends not to play as well with Windows, but again it depends what you're doing. I've had very few problems with Haskell on Windows by writing stand-alone services that talk to the rest of the world through RabbitMQ. So if you're writing something that must absolutely be provably sound, doesn't need to talk to much else, then I'd go with Haskell. If you need lots of core Windows features it'd probably be best with F#. I went from Haskell to F#, and I'm glad I learned Haskell first because it gave me a much stronger grounding in functional concepts. It's much easier to 'break the rules' in F#. I hope that helps.
- fallat 12y agoYes, thank you for your explanation, it was great. That is why I've started with Haskell too (6 months in now), because it teaches you the fundamentals about FP, and is strict about it (a good thing in my opinion). That being said, I will continue to use Haskell. I know C# and I'm sure the jump won't be too difficult if I ever need it.
- louthy 12y agoYep, the jump from Haskell to F# is quite easy. It's a shame that F# doesn't put Computation Expressions up front like Haskell does with monads. The times when I feel like I'm getting into a mess with F# are nearly always fixed with monads (passing state around for example). The nice thing with F# is that [if you have C# experience] you can pretty much just fall back to the standard .NET framework library if you get stuck. And the fact that F# supports dot notation means intellisense works. I've found over time I use the core BCL less and less as I retreat to a more pure functional approach however. Again, a good grounding in Haskell will help you there.