Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
F# is derived from OCaml, but what major items are missing or added? Specifically I'm curious as to whether the resources available for learning OCaml are also useful to someone who wants to learn F#.
This question has been answered for some time now, but I was quite surprised that most of the answers say what OCaml features are missing in F# - this is definitely good to know if you want to port existing OCaml programs to F# (which is probably the motivation of most of the referenced articles). However, there are many features that make F# a different language (not just a limited version of OCaml for .NET!) Here is a couple of things that are added in F#:
Units of measure that allow you to type-check code dealing with numerical calculations
Meta-programming using quotations (which makes it possible to use LINQ in F# and is also essential for promissing projects like the WebSharper platform)
Active patterns for creating abstractions for functional data types (and generally very useful feature for more complicated pattern matching applications)
Computation expressions which is a language feature behind asynchronous workflows (a library for asynchronous I/O/web service/GUI programming)
.NET compatible object-system that makes it possible to fully interoperate with the .NET platform (OCaml also has a support for objects but different - there are of course some benefits in both of the systems).
Overloaded operators - As far as I know, OCaml doesn't have overloaded operators - in F# you can use + for all numeric types as well as your types that support it.
And, honestly, I think that it is also worth mentioning the Visual Studio IDE. This is not a part of the language, but it really improves the user experience (IntelliSense support in Visual Studio is really good!)
If you look at the list, there are many things that largely contributed to the popularity of F#, so it's much more than just "OCaml without functors". F# is definitely based on OCaml (and takes ideas from other languages such as Haskell) and shares many aspects with them, however there is also a lot of other things. I guess that without things like asynchronous workflows, .NET style OO and meta-programming, the Microsoft Developer Division would never include F# in Visual Studio 2010.
The main differences are that F# does not support:
functors
OCaml-style objects
polymorphic variants
the camlp4/5 preprocessor or extension points (ppx)
In addition, F# has a different syntax for labeled and optional parameters.
In theory, OCaml programs that don't use these features can be compiled with F#. Learning OCaml is a perfectly reasonable introduction to F# (and vice versa, I'd imagine).
The complete list of differences is here (note: archive.org replacement of dead link).
F# and OCaml are taxonimically classes in the ML family of languages, which includes a whole passle of other weird animals too. F# is newer than OCaml, and it doesn't have either functors [functions of module -> module] or row types [object classes and polymorphic variants] yet. Between them, those two simplifications probably make the learning curve easier for someone developing on the .Net platform. Sadly, those two language features are hugely powerful in OCaml, so reading the OCaml literature to gain insights into how to code for F# will probably lead to premature frustration with the latter when it's probably an excellent alternative to C# where both are available.
F# supports OCaml syntax directly. It might not be 100% compatible, but I think it's pretty close.
http://plus.kaist.ac.kr/~shoh/fsharp/html/index.html
Here is a list of differences (not sure how up-to-date it is)
http://plus.kaist.ac.kr/~shoh/fsharp/html/fsharp-vs-ocaml.html
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to determine what facilities Clojure and F# have for creating DSLs. What facilities does each provide in order to create and manipulate DSLs?
As F# is statically typed, does this make it more difficult for this specific task? On the Clojure part I have no real experience but all LISPs are known to be great for metaprogramming/DSLs.
My question is not intended to make a war or something of the sort between both languages. If I'm making questions about the both lately is because I do think both are great and want to know more about the specifics of the two.
After reading some days about Intentional Programming, it has made me revive my interest in DSLs and all.
Although I have some knowledge of F# it's true I haven't yet developed anything using quotations or something like that. I've seen examples of DSLs based on discriminated unions which seem interesting.
You can ultimately create DSLs in any language.
What make Clojure / other Lisps particularly unique and well-suited to metaprogramming is that they are homoiconic - that is, the language itself is expressed naturally in the data structures of the same language. In Lisp, you are effectively writing code directly as an AST.
This is surprisingly powerful - it means that code generation is effectively equivalent to creating a relatively simple data structure. And the language provides facilities for you to generate arbitrary code at compile time via macros. This effectively allows you to "extend the language" to support any particular DSL you require.
As an example, I recently found myself wanting an imperative for loop in Clojure (apologies to functional programming purists, but sometimes you want one....). Adding this to the language was a 5-liner:
(defmacro for-loop [[sym init check change :as params] & steps]
`(loop [~sym ~init value# nil]
(if ~check
(let [new-value# (do ~#steps)] (recur ~change new-value#))
value#)))
So now I can do:
(for-loop [i 0 (< i 10) (inc i)]
(println i))
=> < prints numbers from 0..9 >
This is obviously a simple example, but it should hopefully be clear that the ability to generate new language constructs by creating a set of short macros that expand to exactly the code you want makes building a DSL particularly easy.
Some reading / links you may find interesting:
The Curious Clojureist - Excellent video describing some of the unique features of Clojure (with some amusing comparisons to Java)
Paul Graham - Beating the Averages
What makes lisp macros so special
I can't talk about Clojure since I have't used it, but I know a bit about DSLs in F#. F# provides two main language oriented programming features (which is what Don Syme likes to call them): code quotations and computation expressions.
Code quotations are closer to what you would get with macros in a language like Lisp. They allow you to generate expressions programmatically which you can then execute. By using the ReflectedDefinition attribute on F# expressions you get access to their ASTs. See http://msdn.microsoft.com/en-us/library/dd233212.aspx for more details.
Computation expressions are similar to the do notation in Haskell. The compiler rewrites code using a special syntax into calls to a type which you define. This type should ideally form a monad. Since they are monads in disguise they should allow you to implement the custom semantics of your DSL. See http://msdn.microsoft.com/en-us/library/dd233182.aspx for more details.
IMO computation expressions are better suited for writing DSLs on top of F#, while code quotations are better for tasks like transformations or translations (e.g. F# to JavaScript).
Besides these two main features you have the rest of the language which you can use.
Of course, above I've been only talking about embedded domain specific languages. You can go the extra mile and use fslex and fsyacc for a standalone DSL.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I don't believe seeing this. It says:
For April, Chris Smith will be presenting on writing a Java to x86 Compiler in F#.
The presentation may go on for an hour or two which definitely is not enough to write a compiler. I've heard that F# is powerful, but this powerful?
Well, all I wanted to ask is this: Can you write a compiler in F# that quickly?
Let's first start with a few corrections:
It's not a Java compiler, it's a compiler for a small subset of Java.
It doesn't say anywhere that the compiler will be written in the time, only that it will be explained.
In fact, it doesn't even say that, it says, it will be presented. Heck, I can present GCC in 3 minutes. It's not gonna be a very useful presentation, but it's gonna be a presentation.
That said, explaining a well-structured, simple compiler for a simple language implemented in a language which is well-suited for writing compilers within an hour is definitely feasible.
F# is a member of the ML family of languages (specifically, a close cousin of OCaml), and those are indeed well-suited for writing compilers. In fact, the reason why Robin Milner chose the name ML (meta language) for his language, was because it is specifically designed for writing compilers. A compiler is basically a big function (thus making it very natural to implement in a functional language) that detects patterns (thus making it very natural to implement in a language with pattern matching) and executes a little bit of code for each pattern it detects (thus making it very natural to implement in a language with first-class functions). And whaddayaknow? F# is a functional language with very sophisticated pattern matching facilities. Another nice feature is an expressive type system with algebraic data types and discriminated unions which makes it very easy to represent Abstract Syntax Trees.
At the Lang.NET Symposium Jason Olson gave a talk on F#, during which he showed some pieces of an interpreter that he is currently working on that demonstrate these features very well.
Fredrik Holmström is currently working on IronJS, an ECMAScript 3 implementation for the Dynamic Language Runtime. Take a look at the code, specifically the AST types and some of the analysis and parsing code.
Jonathan Tang's Write Yourself a Scheme in 48 Hours is another good example of writing an interpreter, this time in Haskell which shares many features with F#.
The 90 Minute Scheme to C compiler by Marc Feeley is a presentation about a Scheme compiler written in Scheme.
In Implementing Scheme in Ruby, James Coglan teaches the audience Scheme, live-codes and explains a Scheme interpreter in Ruby and writes a couple of sample Scheme programs, all in 15 minutes.
Giving a presentation about a project isn't the same thing as implementing the whole project during the presentation.
It's perfectly possible to present some interesting aspects of a Java to x86 compiler within an hour, and even show some code: but that's not the same as creating all the code then and there.
Java is a fairly complex language, so I suppose that Chris isn't going to implement a complete Java compiler. However, his talk really points out that manipulating with code (and tree-like structures in general) is much easier in F# than in any other .NET language. That's why F# has been used in various static analysis tools (e.g. Microsoft's static driver verifier)
Tools like fslex and fsyacc make it easy to write parser for a language. Chris has a blog with simple mathematical expressions. Robert Pickering wrote a more sophisticated example that actually generates IL code (compiles mathematical expressions to .NET) in just a few lines of code. This can be even easier on .NET 4.0 if you generate code using Expression Trees.
So I suppose that even if he was writing the compiler from scratch, he could write compiler for a langauge that can be used to write non-trivial sample programs.
Can you write a compiler in F# that quickly?
I have written two tiny compilers in F# over the past week, each in about that much time. So yes, it can be done. Here is one of them.
Note that the ML family of languages that F# is descended from were specifically designed for this application (metaprogramming).
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 13 years ago.
Will F# ever be a mainstream language like C# is? Or will it remain a niche language?
Do you foresee any clients coming to you with projects executed in F#?
Will a professional programmer be able to make a living from F#. What sort of demand do you predict for F# programmers?
Kind regards,
There are people making a living out of functional programming today. For examples, see the commercial users of functional programming workshop which is being held each year.
For the first version of F#, Microsoft is targetting engineering, mathematical, financial and data-processing applications (if I remember correctly). Whether that is a niche or not depends on your perspective :) but it seems like a reasonable market.
Thanks to F#'s excellent .NET integration you can, to a large extent, choose to make your projects using F#. What should your client care. If you get a competitive advantage from that with respect to C# programmers, maybe you'd better hope F# does not hit the mainstream...
Lastly, certainly F# (and Scala) are indicators that at least functional programming will become "more mainstream". But when is a language considered mainstream? I wouldn't be surprised if there are many many more lines of code in C and COBOL out there than C#,VB and Java combined. So from a C programmer's perspective, C# is a niche language. I think programming languages, thanks to virtual machines, are becoming more diversified in general (think also Ruby, Python and Haskell, for example, not taking into account all the smaller languages like Clojure, Ioke,...).
I can't answer this for sure. Certainly it's highly subjective. We can just wait and see what happens. But one thing is for sure. Even if F# remains a minority language, ideas and functional style of programming will be further added to other languages gradually. You can't say C# 3.0 is the same as C# 1.0. It's just a matter of name similarity.
I don't think that F# will ever reach the popularity of C# or other imperative languages because most applications are designed imperatively and most programmers think in this way.
But F# provides many very interesting features like LOP, compiler compilers, computation expressions (async workflows), quotations, units of measure.
Many problems can be formulated much better and more concise in a functional programming language (look at those F# examples using async {} or seq {}) and because F# is a bit more mainstream than e.g. Haskell (it has got the .NET framework that simplifies many tasks and is not purely functional!), it will be easier for many programmers to get into functional programming and learn it's advantages.
In addition, it's harder to write wrong code in F# than e.g. in C because you have good type checking, many strong types infered by the compiler and immutable values - You can intuitively prove the correctness of a functional algorithm which is often hard in an imperative one. Just think of this code which should count the number of zeros in an array:
int countZeros;
for (int i = 1; i <= length; i++) {
if (data[i] = 0) {
countZeros++;
}
}
This all are factors that will bring people to use F#. The rest is marketing (Microsoft should have a F# Express Edition in VS2010!!)
I can't see functional languages as a whole becoming mainstream. Ultimately anything that is alien to a mere mortal human way of thinking is never going to be mainstream.
Functional programming will however be more than niche. Its benefits in terms of expressing a problem that can be solved over multiple processors is compelling. What I see is F# libraries for specific purposes and/or F# concepts migrating to C#.
I'm not the expert but I think one of the advantages of functional programming is the relatively painless approach to parallelism and multicore processing. Therefore in a view of the recent Microsoft announcement in this area, namely Axum http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx which is an another approach to parallel programming, I really doubt that F# will ever go mainstream. If Axum is adopted, it will be probably integrated with C# (like Code Contracts moved from Lab to C# 4.0) and F# will be used only in very specific domains.
I don't see it happening until Functional Programming going mainstream.
OTOH, if you take up F#, that's one more mainstream programmer on-board....
Avoiding it is a self-fulfilling prophecy.
The great thing about F# is the powerful combination of programming paradigms it allows. You can write most of your program in the functional style - brief, elegant and free from side effects (common bugs). But when you encounter a problem you can't solve functionally, you can then enter a short section of imperative code to get past it. It's also very easy to mark sections of your code as asynchronous, and it will then run in parallell if you have multiple cores/processors. The solution will be much easier than a C# equivalent. After witnessing a demo by one of the creators of F#, I was left with the impression that they have managed to extract great features from both Python and Erlang. F# will probably be fronted as the .NET answer to these two classes of languages and could eventually achieve a comparable degree of mainstream adoption.
In line-of-business development, it will never happen. It is simply too hard, so the average LOB developer will never be able to maintain it. In higher-end software companies, it could happen though.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
From day 1 of my programming career, I started with object-oriented programming. However, I'm interested in learning other paradigms (something which I've said here on SO a number of times is a good thing, but I haven't had the time to do). I think I'm not only ready, but have the time, so I'll be starting functional programming with F#.
However, I'm not sure how to structure much less design applications. I'm used to the one-class-per-file and class-noun/function-verb ideas in OO programming. How do you design and structure functional applications?
Read the SICP.
Also, there is a PDF Version available.
You might want to check out a recent blog entry of mine: How does functional programming affect the structure of your code?
At a high level, an OO design methodology is still quite useful for structuring an F# program, but you'll find this breaking down (more exceptions to the rule) as you get down to lower levels. At a physical level, "one class per file" will not work in all cases, as mutually recursive types need to be defined in the same file (type Class1 = ... and Class2 = ...), and a bit of your code may reside in "free" functions not bound to a particular class (this is what F# "module"s are good for). The file-ordering constraints in F# will also force you to think critically about the dependencies among types in your program; this is a double-edged sword, as it may take more work/thought to untangle high-level dependencies, but will yield programs that are organized in a way that always makes them approachable (as the most primitive entities always come first and you can always read a program from 'top to bottom' and have new things introduced one-by-one, rather than just start looking a directory full of files of code and not know 'where to start').
How to Design Programs is all about this (at tiresome length, using Scheme instead of F#, but the principles carry over). Briefly, your code mirrors your datatypes; this idea goes back to old-fashioned "structured programming", only functional programming is more explicit about it, and with fancier datatypes.
Given that modern functional languages (i.e. not lisps) by default use early-bound polymorphic functions (efficiently), and that object-orientation is just a particular way of arranging to have polymorphic functions, it's not really very different, if you know how to design properly encapsulated classes.
Lisps use late-binding to achieve a similar effect. To be honest, there's not much difference, except that you don't explictly declare the structure of types.
If you've programmed extensively with C++ template functions, then you probably have an idea already.
In any case, the answer is small "classes" and instead of modifying internal state, you have to return a new version with different state.
F# provides the conventional OO approachs for large-scale structured programming (e.g. interfaces) and does not attempt to provide the experimental approaches pioneered in languages like OCaml (e.g. functors).
Consequently, the large-scale structuring of F# programs is essentially the same as that of C# programs.
Functional programming is a different paradigm for sure. Perhaps the easiest way to wrap your head around it is to insist that the design be laid out using a flow chart. Each function is distinct, no inheritance, no polymorphism, distinct. The data is passed around from function to function to make deletions, updates, insertion, and create new data.
On structuring functional programs:
While OO languages structure the code with classes, functional languages structure it with modules. Objects contain state and methods, modules contain data types and functions. In both cases the structural units group data types together with related behavior. Both paradigms have tools for building and enforcing abstraction barriers.
I would highly recommend picking a functional programming language you are comfortable with (F#, OCaml, Haskell, or Scheme) and taking a long look at how its standard library is structured.
Compare, for example, the OCaml Stack module with System.Collections.Generic.Stack from .NET or a similar collection in Java.
It is all about pure functions and how to compose them to build larger abstractions. This is actually a hard problem for which a robust mathematical background is needed. Luckily, there are several patterns with deep formal and practical research available. On Functional and Reactive Domain Modeling Debasish Ghosh explores this topic further and puts together several practical scenarios applying pure functional patterns:
Functional and Reactive Domain Modeling teaches you how to think of
the domain model in terms of pure functions and how to compose them to
build larger abstractions. You will start with the basics of
functional programming and gradually progress to the advanced concepts
and patterns that you need to know to implement complex domain models.
The book demonstrates how advanced FP patterns like algebraic data
types, typeclass based design, and isolation of side-effects can make
your model compose for readability and verifiability.
What is the added value for learning F# when you are already familiar with LISP?
Static typing (with type inference)
Algebraic data types
Pattern matching
Extensible pattern matching with active patterns.
Currying (with a nice syntax)
Monadic programming, called 'workflows', provides a nice way to do asynchronous programming.
A lot of these are relatively recent developments in the programming language world. This is something you'll see in F# that you won't in Lisp, especially Common Lisp, because the F# standard is still under development. As a result, you'll find there is a quite a bit to learn. Of course things like ADTs, pattern matching, monads and currying can be built as a library in Lisp, but it's nicer to learn how to use them in a language where they are conveniently built-in.
The biggest advantage of learning F# for real-world use is its integration with .NET.
Comparing Lisp directly to F# isn't really fair, because at the end of the day with enough time you could write the same app in either language.
However, you should learn F# for the same reasons that a C# or Java developer should learn it - because it allows functional programming on the .NET platform. I'm not 100% familiar with Lisp, but I assume it has some of the same problems as OCaml in that there isn't stellar library support. How do you do Database access in Lisp? What about high-performance graphics?
If you want to learn more about 'Why .NET', check out this SO question.
If you knew F# and Lisp, you'd find this a rather strange question to ask.
As others have pointed out, Lisp is dynamically typed. More importantly, the unique feature of Lisp is that it's homoiconic: Lisp code is a fundamental Lisp data type (a list). The macro system takes advantage of that by letting you write code which executes at compile-time and modifies other code.
F# has nothing like this - it's a statically typed language which borrows a lot of ideas from ML and Haskell, and runs it on .NET
What you are asking is akin to "Why do I need to learn to use a spoon if I know how to use a fork?"
Given that LISP is dynamically typed and F# is statically typed, I find such comparisons strange.
If I were switching from Lisp to F#, it would be solely because I had a task on my hands that hugely benefitted from some .NET-only library.
But I don't, so I'm not.
Money. F# code is already more valuable than Lisp code and this gap will widen very rapidly as F# sees widespread adoption.
In other words, you have a much better chance of earning a stable income using F# than using Lisp.
Cheers,
Jon Harrop.
F# is a very different language compared to most Lisp dialects. So F# gives you a very different angle of programming - an angle that you won't learn from Lisp. Most Lisp dialects are best used for incremental, interactive development of symbolic software. At the same time most Lisp dialects are not Functional Programming Languages, but more like multi-paradigm languages - with different dialects placing different weight on supporting FPL features (free of side effects, immutable data structures, algebraic data types, ...). Thus most Lisp dialects either lack static typing or don't put much emphasis on it.
So, if you know some Lisp dialect, then learning F# can make a lot of sense. Just don't think that much of your Lisp knowledge applies to F#, since F# is a very different language. As much as an imperative programming used to C or Java needs to unlearn some ideas when learning Lisp, one also needs to unlearn Lisp habits (no types, side effects, macros, ...) when using F#. F# is also driven by Microsoft and taking advantage of the .net framework.
F# has the benefit that .NET development (in general) is very widely adopted, easily available, and more mass market.
If you want to code F#, you can get Visual Studio, which many developers will already have...as opposed to getting the LISP environment up and running.
Additionally, existing .NET developers are much more likely to look at F# than LISP, if that means anything to you.
(This is coming from a .NET developer who coded, and loved, LISP, while in college).
I'm not sure if you would? If you find F# interesting that would be a reason. If you work requires it, it would be a reason. If you think it would make you more productive or bring you added value over your current knowledge, that would be a reason.
But if you don't find F# interesting, your work doesn't require it and you don't think it would make you more productive or bring you added value, then why would you?
If the question on the other hand is what F# gives that lisp don't, then type inference, pattern matching and integration with the rest of the .NET framework should be considered.
I know this thread is old but since I stumbled on this one I just wanted to comment on my reasons. I am learning F# simply for professional opportunities since .NET carries a lot of weight in a category of companies that dominate my field. The functional paradigm has been growing in use among more quantitatively and data oriented companies and I'd like to be one of the early comers to this trend. Currently there doesn't an exist a strong functional language that fully and safely integrates with the .NET library. I actually attempted to port some .NET from Lisp code and it's really a pain b/c the FFI only supports C primitives and .NET interoperability requires an 'interface' construct and even though I know how to do this in C it's really a huge pain. It would be really, really, good if Lisp went the extra mile in it's next standard and required a c++ class (including virtual functions w/ vtables), and a C# style interface type in it's FFI. Maybe even throw in a Java interface style type too. This would allow complete interoperability with the .NET library and make Lisp a strong contender as a large-scale language. However with that said, coming from a Lisp background made learning F# rather easy. And I like how F# has gone the extra mile to provide types that you would commonly see it quantitative type work. I believe F# was created with mathematical work in mind and that in itself has value over Lisp.
One way to look at this (the original question) is to match up the language (and associated tools and platforms) to the immediate task. If the task requires an overwhelming percentage of .NET code, and it would require less shoe-horning in one language than another to meet the task head-on, then take the path of least resistance (F#). If you don't need .NET capabilities, and you're comfortable working with LISP and there's no arm-bending to move away from it, keep using it.
Not really much different from comparing a hammer with a wrench. Pick the tool that fits the job most effectively. Trying to pick a tool that's objectively "best" is nonsense. And in any case, in 20 years, all of the currently "hot" languages might be outdated anyway.