Learn ML in one filešŸŒŽ

jkone27
4 min readJul 15, 2023

your first steps with a modern ML language in ~ 200 loc

Rachel Reese talking about the very interesting history of fsharp at PolyConf16

ML languages are cool, and if you are a programmer or a student, chances are you only know a language belonging to the extended C-family of programming languages.

Broadly speaking, you know one or more of any of these:

C, C++, Python, Javascript/ES, Typescript, Java, Ruby*, C#, VB.NET*

NOTE: Starred languages (*) like Ruby and VB.NET are a little exception here, to be precise, so I extend the definition of C-like languages a bit broadly here. This is just to show the big picture of the really BIG differences with the ML family of languages. I tried to simplify the landscape a bit, see the picture below for more details.

Conceptually also other languages not mentioned here had important impacts on the evolution of the broader C-family, like Smalltalk for Object Oriented features, Fortran and Pascal for procedural programming, or BASIC in the case of VB and VB.NET Microsoft languages.

If your teachers were old dinosaurs or just boomers, you might have learned one of those last mentioned as well.

If you feel adventurous, you should try something else!

If you have encountered any other language in your career, you might have tried Rust or Elm or OCaml, or maybe Haskell* or F#. those languages belong (partially or fully) to another gene/seed of programming language family trees, the ML family of languages ā€” Haskell(*) is a bit different but many similarities still hold quite well.

sourced from this article

Or maybe in some weird blog post or arcane link, or some weird Reddit thread, you discovered about LISP, and how it influenced even JS (ES), or you read the mystical SICP book.

ML stands for Meta Language, not for Machine Learning, and not for Facebook Metaverse, sorry!

google: what does meta mean

Why Learn a ML-language?

  1. You will be able to read and understand a great deal more of the code out there in the wild, or at least, that will come at a much lesser cognitive cost to you.
  2. It will enhance the way you think about code in general, especially about abstractions via the concept of ADT or Abstract Data Types
  3. It might bring you closer to Math, Set Theory and Category Theory. A lot of things you learnt will make much more sense to you now! you can re-use this lost knowledge and maybe appreciate the math you used to hate in school as a new renaissance.
  4. Lambda (or anonymous) functions and lambda calculus will be much less ā€œanonymousā€ to you than before.
  5. Curry will not just be an Indian spice mix anymore! And by exploring further in this concept, your will in hindsight also learn why Haskell is called like it.
  6. ML languages will help you model your problems and domains with Types in an alternative way, that can also be combined with OOP in most ML modern languages, using Records and Discriminated Unions and pattern matching.

ML Keywords 101

In PowerPoint Slides Why Not?

let and type keywords

And 3 extra to be able to organize and reference code!

Now you can get started with a real ML language of your choice, here I will make an F# example. Here are other language playgrounds if you want to try other ML languages because why not?

  • Reason playground
  • Elm playground
  • OCaml playground
  • Rust playground, is not a 100% ML language but still a very cool and pragmatic language
  • Haskell playground -> technically not an ML language, similar concepts, and syntax might differ quite a bit though, also no OOP here.

Install F# (and dotnet)

brew install dotnet

dotnet fsi

> "hello";;
> #quit;;

To run this code, you need Visual Studio or Rider, or VSCode + Ionide extension, else you can just try it online without installing anything here.

Now you are ready to run the file below and learn an ML language in 1 go, hopefully!

The whole file is less than 200 lines of code.

Feel free to experiment or suggest additions.

Thanks to F# REPL you can also just select portions of your code and run them one after another, select the code you want to run, then hit option/alt + ENTER.

Resources

--

--