FSharp for beginners

130 views
Skip to first unread message

Thomas Clarke

unread,
Apr 8, 2016, 2:25:47 PM4/8/16
to F# Discussions
I'm planning a lecture course next year which will (among other things) teach F#. I have two infrastructure issues

(1) shrink-wrapped dev environment

Xamarin on Macs now seems to do the job well, and uses F# 4.0 which I think should now be what is used.

You would think things should be easier on Windows, but Visual Studio, which with power tools is definitely a nice environment, is somewhat temperamental to install. All that is needed is VS2015 community, F# 4.0, and VS power tools (for F# 4.0). And .Net. But I've tried this on multiple windows PCs with differing results. I can always eventually get it to work but the process is rocky and I'm wondering whether others think this and what is the best route to instructions that will work 99% of the time - with a lot of students installing stuff it is a good idea to minimise this type of problem.

Maybe I should use Xamarin on Windows as well - but I expect the tooling is not as good as VS? Automatic formatting is a good thing  for beginners.

Currently there is no easy to find shrink-wrapped route to installing on F#/mono on Linux but I expect by next year there will be, and Ionide with Visual Code looks good!

I also think that the F# "how to get started" page could do with some updating. the 3.1 vs 4.0 thing is confusing.

(2) String functions

When learning F# I found not knowing what was a module-level function and what a method was a pain. String functions the worst. I had not previously used .Net (and that will also be true for my students). String Split, startWith, endsWith, contains are really important and having these available only as a .Net methods is irregular. I'd like to teach F# with a good enough selection of functions so that methods need not be used for any of the normal data structure tasks. And the idea of methods that can break type safety by throwing run-time exceptions (there are many such) is horrible. What is good practice? It seems to me that having to use library methods and functions is not good when learning. Should I extend modules as needed with functions? And can I do this so that all available functions will be documented in autocomplete?

I don't really mind if I get no comment on these two issues but any thoughts would be gratefully received.

Tom

Dany Fabian

unread,
Apr 8, 2016, 2:45:29 PM4/8/16
to F# Discussions

As for (1), I can't help you out at the moment, because I haven't installed F# recently, but as for (2): Yes you can extend the modules by your own functions. When you comment them with /// comments, then intellisense should pick up the documentation you wrote and the experience is quite nice. Here is a random sample code, how to use the /// comments. Hope this help. https://github.com/fsharp/FAKE/blob/master/src/app/FakeLib/NGenHelper.fs


Other than that, you may want to look into some of the great libraries out there that extend the F# libraries with some of the functions you'd like. E.g. https://github.com/fsprojects/FSharpx.Extras/blob/master/src/FSharpx.Extras/Strings.fs



could well be, that the latter has about what you would like.




From: fsharp-o...@googlegroups.com <fsharp-o...@googlegroups.com> on behalf of Thomas Clarke <clar...@gmail.com>
Sent: 08 April 2016 19:25
To: F# Discussions
Subject: FSharp for beginners
 
--
--
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,
Apr 8, 2016, 2:52:57 PM4/8/16
to fsharp-o...@googlegroups.com
Just my thoughts off the top of my head. Regarding VS on Windows - you could make a VM image with everything working as you like and have the students run that perhaps?

My understanding of XS is that it's actually got a better REPL environment than VS now (although I've never used it - I'm sure that someone else can speak to that).

You could also look at VS Code + Ionide


Regarding string functions - yes, it's a pain but part of the legacy of .NET which is an OO framework. So you have these string methods living on the string itself etc. It's trivial to make a set of functions in a module that are just functions that take in a string and calls the method on it e.g

let contains (s:string) = s.Contains

You could put them all into a String module and just #load that at the start of any script. You'll get full intellisense / autocomplete on that.

Regarding exceptions - again that's the legacy of .net. You can either live with it or create an Either type e.g. Success / Fail and wrap any functions that you know can throw exceptions with a try / with so that they instead return the Either.

Hope that helps!

From: Thomas Clarke
Sent: ‎08/‎04/‎2016 20:25

To: F# Discussions
Subject: FSharp for beginners

Gauthier Segay

unread,
Apr 8, 2016, 3:30:17 PM4/8/16
to fsharp-o...@googlegroups.com
Hello Thomas,

(1) I think you should look at

* installing F# 3.1 standalone package first
* then VS2015 (and checking F# language in the custom install)
* then fsharp powertools

Despite F# 4.0 is the most recent version, lots of libraries depends
on FSharp.Core of previous release and some tooling (ionide for VS
Code for example) relies on F# 3.1 being installed, having both
installed seems to smooth things out a bit.

(2) having a base library as a facade for .net framework sounds like a
good idea if your emphasis is on functional programming, you can make
those functions' signature and behaviour "total". Others have
addressed well your concern for intellisense documentation.

Great initiative to use F# for your lectures :)

Warren Young

unread,
Apr 8, 2016, 10:02:12 PM4/8/16
to fsharp-o...@googlegroups.com
On Apr 8, 2016, at 12:25 PM, Thomas Clarke <clar...@gmail.com> wrote:
>
> All that is needed is VS2015 community, F# 4.0, and VS power tools (for F# 4.0). And .Net.

That should be two items, not four.

F# 4.0 is an option during the Visual Studio 2015 Community installation process, not a separate installation item.

The necessary versions of .NET are also installed along with Visual Studio 2015. You should not have to do anything special. If you think you do, post relevant details.

And the Power Tools are easily installed from within Visual Studio 2015, via Tools > Extensions and Updates > Online > "f# power tools”.

(You might not be aware that this is a change in the free tier of Visual Studio relative to 2013 and prior. Microsoft has finally given up on restricting VS extensions to the paid tiers.)

> But I've tried this on multiple windows PCs with differing results.

Details??

> Maybe I should use Xamarin on Windows as well

No. That was deprecated with the merger of Xamarin into Microsoft. Don’t expect Xamarin Studio for Windows to get any official support.

(Source: https://www.xamarin.com/faq#xpq6)

I suppose the community might take over maintenance again, so that you might someday see Mono Develop for Windows, but given that Xamarin for Visual Studio is now free for the same reason that they’ve deprecated Xamarin Studio for Windows, I don’t see why you’d hold off your plans waiting for it to appear.

> Currently there is no easy to find shrink-wrapped route to installing on F#/mono on Linux

Sure there is:

http://fsharp.org/use/linux/
http://www.mono-project.com/download/#download-lin

On Ubuntu and Debian, it’s a single command. On Red Hattish Linuxes, you have to tell your OS’s package manager about the Mono package repository, and *then* it’s one command.

Alternatively, there’s .NET Core, if your needs are simple:

https://dotnet.github.io/

There’s a whole lot that .NET can do that .NET Core cannot — no surprise — but for a tutorial class, you might not run up against any of those limitations.

> Ionide with Visual Code looks good!

That would also be a fine companion to .NET Core.

> the 3.1 vs 4.0 thing is confusing.

For your purposes, I don’t see any reason to even think about 3.1. You’re writing new code, not maintaining existing working code.

> When learning F# I found not knowing what was a module-level function and what a method was a pain. String functions the worst. I had not previously used .Net (and that will also be true for my students). String Split, startWith, endsWith, contains are really important and having these available only as a .Net methods is irregular.

Yes, well, that’s the sort of thing you expect from a mixed-paradigm language like F#, where the OCaml standard library is replaced with .NET. You can’t expect it to look like it was designed from the start by a single mind.

Bjarne Stroustrup said, "There are only two kinds of programming languages: those people always bitch about and those nobody uses.” F# is the first kind. :)

The reason this is true is that once a language becomes powerful enough to work for a huge variety of problems, it has become somewhat ugly. F# is uncommonly clean for a language with its power and scope.

> I'd like to teach F# with a good enough selection of functions so that methods need not be used for any of the normal data structure tasks.

Why then use a mixed-paradigm language? F# is not just FP, it’s imperative and OO, and we like it that way. If you want a pure FP language, go hang out with the Scheme and Haskell purists.

(They’re the second kind of programming language. <runs and ducks>)

> It seems to me that having to use library methods and functions is not good when learning.

Tell you what: go read “The Little Schemer” and decide if you really do want to teach a class that builds everything up from primitives.

You might find that greatly preferable, and if so, now you have your textbook.

Personally, I prefer using a language that provides many affordances, so I’m not tempted to reinvent wheels unnecessarily. But then, I’m a practitioner first and a student second.

Thomas Clarke

unread,
Apr 9, 2016, 10:29:00 AM4/9/16
to F# Discussions
Dear all, thank you for these diverse comments.

I can't use a VM (I think) because there is no license for windows for student laptops which is the use case where I'd like foolproof installation.
Good idea about fsharpx.extras and I will happily use the string stuff there - easy enough to write my own stuff but I'd rather it was more standard.
Also, nice that I can add stuff and document it for intellisense so easily - I'll probably do that as well!
Good idea about install 3.1 and 4.0 - though with luck this will be less relevant by the time the course starts Spring 2017.

Thanks especially to Warren:

Thanks very much for the information about VS2015 Community. I'll try it again on a few clean machines. It sounds like it should be easy and I was probably just doing the wrong thing.

I fully understand about .NET interoperability being a compromise - and F# itself being a compromise language par excellence. Personally, that is too my taste. I want a language that works well and don't care about purity. I like many things about python because it is so carefully designed for usability not perfection, but the lack of good static typing is a really unacceptable pain. Actually as pragmatic decisions go I think python's array-based lists, and the supporting syntax around them, are quite excellent.

Why do a want to teach F#? Well it was a narrow decision. But the purpose of the course is to give students who can program a bit more insight into and experience of real programming. I know a functional language might seem weird for that but they learn fundamental things in a short uni course that if I teach say C# they will never learn after. Whereas they will all learn C# etc anyway. I will be teaching some of the dirtier bits of F# but when getting them started with FP (the first few weeks) it is helpful to keep things clean. They have only 80 hours or so of time and I want them to do some decent project work in order to explore issues about modularity and testing. They have all learnt C/C++ (but half have done no real OOP stuff). I'm relying on them being pretty bright.

So: I'm hoping:
(a) The relative simplicity and productivity of a mostly pure functional language will make for a quick learning curve practically - they can well cope with the concepts
(b) I can use impure shortcuts as needed gracefully to avoid the things which are cumbersome.
(c) They will all come to love F# as a great scripting and general purpose programming language (which it is).
(d) The great tooling and particularly the "type checking as you type" will speed learning and programming for these <80 hours novices. This is debatable. Lack of good tooling means you need to get straight yourself how types work earlier, but I think on balance help when learning is good.

There is an argument for using Haskell, or even Scala (more similar syntax). But I think ML syntax is really quick to pick up and F# is reasonably familiar in terms of base types and operators on them.

Whether it is best to teach FP or not is a complex decision which I come down firmly on the "teach FP" side of. Partly because the underlying theory is fun. Whether F# is the best FP candidate for this purpose I'm not sure, as you can see, but I'm going with it.

Best wishes, Tom

Thomas Clarke

unread,
Apr 9, 2016, 10:33:03 AM4/9/16
to F# Discussions
> It seems to me that having to use library methods and functions is not good when learning. 

Tell you what: go read “The Little Schemer” and decide if you really do want to teach a class that builds everything up from primitives. 


OK - what I meant was that library functions and methods are two different things and it is easiest just to use library functions - especially because that makes intellisense finding stuff easier. When writing programs in limited time the right decent set of library stuff is essential - so that is a given!


RobertBoissy

unread,
Apr 9, 2016, 2:41:14 PM4/9/16
to F# Discussions
Hi Thomas,

I'm very pleased to hear of your plans.  You may want to consider a Microsoft DreamSpark subscription, and/or the free resources available for students through DreamSpark, to help with your efforts:


If you are considering a DreamSpark subscription (I maintain our department's DreamSpark Premium subscription), please check with your institution.  Most academic institutions have a Volume Licensing Agreement with Microsoft, and one of the (not so well known) benefits of these agreements is that it entitles the institution to two free DreamSpark subscriptions. One of these free subscriptions is a Departmental DreamSpark Premium subscription, and the other is an institution-wide DreamSpark Standard subscription.  Frankly, I am surprised that an academic department that teaches Computer Science courses doesn't already have a departmental DreamSpark Premium subscription.

I use F# on Mono on RHEL-family Linux distributions (Fedora, CentOS, RHEL, etc.) together with Atom/Ionide, and I look forward to using F# on DotNetCore on these Linux distributions also.  The setup is all scripted using bash, and the resulting environments (and their dependencies) are documented and reproducible starting from a bare OS install, even on the latest version of Fedora.

Please feel free to contact me directly if I can be of any additional help. 

Best wishes,

Robert

Thomas Clarke

unread,
Apr 9, 2016, 2:53:37 PM4/9/16
to F# Discussions
Thanks Robert - that is good to hear.

You are right now I come to think of it - I'll check dreamspark. Our students do have dreamspark free - does it include windows install license?

It is good to know that easy install solutions exist now - they then surely will when needed next year.

I'm a great believer in giving everyone decent support on their own laptops and that means all of Win/Mac/Linux if possible.


On Friday, 8 April 2016 19:25:47 UTC+1, Thomas Clarke wrote:

Warren Young

unread,
Apr 10, 2016, 1:30:52 AM4/10/16
to F# Discussions
On Saturday, April 9, 2016 at 8:29:00 AM UTC-6, Thomas Clarke wrote:

Good idea about install 3.1 and 4.0 - though with luck this will be less relevant by the time the course starts Spring 2017.

I think you should plan on teaching 4.0 only, because this need to support 3.1 will fade. You aren't doing your students any favors by bothering them with transient conditions.

There are at least two features of F# 4.0 that are especially useful in a teaching context:

1. The language compiler now papers over the distinction between mutable and ref cells. You might not need to introduce the concept of ref at all, reducing confusion.

2. The container functions are much more regular in 4.0, again reducing confusion because your students don't have to convert data between containers or choose containers based on which methods work on them: https://github.com/fsharp/FSharpLangDesign/blob/master/FSharp-4.0/ListSeqArrayAdditions.md

 it is easiest just to use library functions - especially because that makes intellisense finding stuff easier.

I guess your point is that it's easier to find everything if functions aren't hidden by namespace hierarchies and classes?

If so, I cannot agree. That road leads to PHP: http://php.net/manual/en/indexes.functions.php

Thomas Clarke

unread,
Apr 10, 2016, 7:40:33 PM4/10/16
to F# Discussions
Hi Warren,

I agree 4.0 better than 3.1, and for the reasons you say which I have also found. I would not teach 3.1 but it might be needed in some situations as pointed out above.

The issue is, for example, split on string. Do I find it as a method on strings, or as a function String.split? I'd much rather the latter and there are a few places like this where the 4.0 standard libraries are a bit weak. I'd want to strengthen them enough so no-one feels they need to use methods for any normal data processing. When learning the language it is natural to explore String.* using autocomplete, having also to autocomplete s.* where s is a string is a burden. In addition using methods creates syntactic irregularity, and is a type inference annoyance. I used them myself learning the language but in retrospect wish I had not! It is great having access to all the .Net stuff when needed for solving problems, but not great to be using it regularly. So common list/map/string/array stuff should avoid them. I'm happy with functions in namespaces (no php chaos), but not with dynamic methods.

Dave Thomas

unread,
Apr 12, 2016, 12:05:35 PM4/12/16
to F# Discussions
The problem is thats how it is with F#, the core lib doesn't have all the batteries included so you have to use the .Net framework to get anything done.  In my mind if F# had been standalone like OCaml or Haskell then it would of been a stronger entity, as it stands its beholden to the core frameworks, whether they're idiomatic or not.
Reply all
Reply to author
Forward
0 new messages