r/functionalprogramming • u/Defection7478 • Apr 30 '24
Question Functional language to replace python
Hi all, I'm looking for some suggestions on a functional language to learn.
Some background: I write a lot of code in c# and python. I write a lot of ci/cd tooling in python or bash, and small to medium sized apps in python, and large apps in c#. For web frontends I use htmx + hyperscript. A very important feature I can use in both of these languages is templating (jinja2 / razor pages).
Presumably, I could try swapping in f# for c#, but I typically only use c# for very large apps, and I'd like something that I can start chewing on at a smaller scale. Something for ci/cd scripts, automation tasks, basic web servers, etc.
What I'm looking for in another language:
- (obviously) the goodness that comes with functional languages, a lot of things have been making their way to c# as I understand, but I figure I might as well get it straight from the source
- a mature templating library
- a mature standard library
- nice to have: static typing system
- simple dependency definition. I like that in both of the above languages I can define my dependencies in a single human-readable file (
requirements.txtorpyproject.toml,*.csprojalthough managing shared dependencies between csproj files is annoying) - simple modularity. I love how easy it is in c# to just add a separate project to a solution to keep things organized. I hate how obtuse it is to maintain the
.slnfile and all the namespaces. It is impossible without an IDE. python doesn't have this issue, but understanding how modules work,__init__.pyand__main__.py, modules vs packages, all that stuff is so annoying. I've been enjoying Rusts module system. - quick and easy startup. from 0 -> helloworld in python is literally
echo "print('hello world')" > hello.py. compared to the saga of booting of vs, creating a new solution, picking a name, ... that is c#.
any suggestions?
34
u/MasSunarto Apr 30 '24
Brother, F# can be used as scripting language (fsx file name extension).
26
u/ShrimpHands Apr 30 '24
Quick and easy is debatable but Scala hits everything else you need.
14
u/qqwy Apr 30 '24
I recommend giving Elixir try! It hits all your points, except the 'nice to have: static types'. (Though there is an opt-in type checker called dyalizer, quite similar to mypy, and proper static types are in the works). It is very easy to learn, the documentation is stellar, there is really good support for webdev and webtemplating, and packaging and running Elixir apps is a breeze.
10
u/[deleted] Apr 30 '24
F# is superb. Try using it with giraffe viewengine, suave server and htmx.
8
u/Asleep-Dress-3578 Apr 30 '24
If you want to work for the .NET ecosystem, you could try F#. If you want to work for the Python ecosystem, take a look at Coconut: http://coconut-lang.org
For Python, there is also a LISP variant called hy: https://hylang.org
It is also not a bad idea to learn JavaScript and TypeScript, and actually JavaScript is a semi-functional language, the SICP book has actually a JavaScript edition, too: https://mitpress.mit.edu/9780262543231/structure-and-interpretation-of-computer-programs/
8
u/yeastyboi Apr 30 '24
Check out this talk called Fsharp as a better python: https://youtu.be/_QnbV6CAWXc
8
u/raxel42 Apr 30 '24
Scala fits exceptionally well. I switched to Scala completely more than ten years ago. Scala can be compiled into native and JS, so I don't need to learn JS to write full-stack prototypes or canvas visualization.
5
u/yawaramin Apr 30 '24
You could give OCaml a try. It might check many of your boxes.
About a decade ago Thomas Leonard ported the 0install package manager from Python to OCaml. His conclusion was:
The article (and entire series of posts) is a great read: https://roscidus.com/blog/blog/2014/02/13/ocaml-what-you-gain/
I wrote the dream-html library and I think it's quite mature. It supports standard HTML, SVG, MathML, ARIA, and htmx markup out of the box, and provides a way of easily constructing HTML from pure immutable values, taking advantage of OCaml's strengths. Btw remember Leonard mentioning 'polymorphic variants' above? I use them quite a bit in this library, eg:
(It's the word starting with the backtick character.) These are very convenient for just using ad-hoc values to represent something, while also benefitting from type safety.
Arguably, this is where OCaml is weakest and you have to look at other popular libraries in the ecosystem to fill the gaps. Eg
yojsonfor JSON processing,containersfor a wide range of convenient collection types, etc. However the standard library nowadays is starting to get fleshed out so you may be surprised just how much you can do with it.OCaml is strongly, statically typed with nearly full type inference so you almost never need to write a type (but can for documentation reasons).
Dependency definition is slightly more complex because OCaml's toolchain is very modular–typically you declare the dependency in a
dune-projectfile (analogous torequirements.txt), thenopam installit (analogous topip install), then declare it separately in specificdunefiles that define how libraries are in your project are linked. This is kind of like if you removed all theimportstatements from your Python files and moved them to a separate file which declares all the imports. Simple example of this here: https://dune.build/OCaml is famous for its strong modularity, and its dune build system takes that approach as well, at the cost of having to manage a few more files. Basically, each subdirectory in the project is considered a separate component (typically a library that can be linked into the app) and needs its own separate
dunefile to control what packages are linked into it. It offers more fine-grained control but takes a bit of getting used to.Similar to your Python example
If you are interested, I have a post which gives a quick overview of the language and toolchain and has a small worked project so you can evaluate some real-world-ish code: https://dev.to/yawaramin/practical-ocaml-314j
Another demo app–a small backend-driven webapp using htmx: https://github.com/yawaramin/dream-html/tree/todoapp/app
4
u/Public_Possibility_5 Apr 30 '24
www.roc-lang.org
3
u/julienlucca May 01 '24
Elixir
5
u/miyakohouou Apr 30 '24
Haskell isn't too bad of a choice here, especially with something like turtle.
There are a few good choices here. Shakespeare and Pandoc are the big obvious choices, but there are several others as well.
People will complain about Haskell's standard library a bit, but honestly it's pretty good. It's a small standard library compared to other languages, so you'll get accustomed to adding a few other common dependencies (
unix,directory,text,containersare all probably going to be common for these kind of scripts). These are really common libraries though, it's not really a big deal to just kind of add them by default for each new project.Haskell has an excellent type system.
You can define all of your dependencies inside of your cabal file or package.yaml file. I personally use
hpackwith apackage.yamlfile to generate my project because I find it a little nicer to use. Overall it's a pretty light weight way to create a project and add dependencies.You can have multiple projects grouped together using a
caba.project, but spinning up a new project is lightweight enough that you might not need to.Haskell can be pretty quick to get started with, especially if you have a lot of the common packages that you want installed to your system. For example:
4
u/arthurno1 Apr 30 '24
CommonLisp.
Power and speed of a compiled language in a dynamic but strongly typed language that supports functional, OO and modular programming.
Built in-template language you ask for looks and feels exactly the same as the language itself, because it is the language itself, so you don't need to learn anything extra, more than how quotation works.
Mature and standardized library. Projects written 20-30 years still work as intended, without changes.
Type system annotations so you can insteuct compiler to build optimized code.
From zero to hello world is literally to type "Hello, world" at the repl since everything is evaluated.
Asdf build system gives you simple declarative way to define your projects, written in the same language as the rest of your program, and run by the same compiler.
Documentation built directly into the code and accessible at the repl.
And much more.
Try sbcl compiler.
2
u/MaxwellzDaemon Apr 30 '24
The J programming language is an extremely consistent and very modular notation that is easy to use and very full featured. It looks very different from many other languages because it is a mathematically oriented notation and is interpreted.
For quick and easy startup, helloworld would be
'hello world'
2
u/sergey-gornostaev May 01 '24
Perhaps you should look at https://hylang.org
2
u/radisrad6 May 01 '24
Elixir forsure — just look at the reviews on Stack overflow’s developer survey
1
u/adwolesi Oct 22 '24
My new favorite is V: https://vlang.io
Simple, fast, typed, and with first class support for scripting: https://docs.vlang.io/other-v-features.html#cross-platform-shell-scripts-in-v
It's like the missing child of Go and Rust.
1
u/CainKellye Apr 30 '24
Rust has all the functional programming I need. Also templates (macros) and mature libraries. Did you miss anything?
1
u/[deleted] Apr 30 '24
Nim?
0
u/[deleted] May 01 '24
[deleted] — view deleted comment
6
u/[deleted] Apr 30 '24
[removed] — view removed comment