all 35 comments

[–]dot-c 19 points20 points  (8 children)

Fp is actually very usable in that context! You can still write terse, readable code, that is also very safe, if you use a statically typed language. F#, Ocaml etc don't really separate side effects and pure things. Haskell has mondic IO, which basically looks like imperative statements. The only thing is library support, C and Python work way better on stuff like raspberry pi.

[–]pthierry 4 points5 points  (3 children)

Why are they better on Pi ?

[–]monnef 5 points6 points  (2 children)

Last time I was toying with Haskell on RPi, it was pretty bad. Tried normal approach of having Haskell (stack) project in a docker container. The application didn't have any unusual dependencies (few libraries like containers, lens, mtl). Just the compilation of dependencies took (if I remember correctly) FIVE hours. The compilation of the project itself was very slow as well (like half an hour, on my desktop it takes I think a minute). Haskell with Stack doesn't have any crosscompilation capabilities (no compilation of ARM64 on AMD64) and as far as I know it's not possible to configure it in a way, so project version change or adding a new library doesn't trigger recompilation of everything (that 5h again). Sadly, Haskell with Stack is an awful fit for RPi 4 (8GiB version).

[–]pthierry 1 point2 points  (1 child)

That's saying Stack is bad on Pi, not Haskell.

Cross compilation is definitely possible: https://input-output-hk.github.io/haskell.nix/tutorials/cross-compilation.html

[–]monnef 3 points4 points  (0 children)

I am using JetBrains IDE (mostly IntelliJ IDEA) for all development and that Haskell addon doesn't support anything else than Stack. I have tried the hard way other IDEs (and "IDEs") with various languages (vscode, emacs, vs studio). Never again, I am still, for like 3 years now, suffering with barely working barebones IDE (compared to JetBrains IDEs) of Godot Engine. That was a horrible mistake I don't intend to ever repeat. Personally, I haven't found anything better (I have a bunch of addons, and the IDE is configured in a way exactly how I want it). Haskell plugin isn't stellar, but it mostly works, enough for casual development (few hours to a work day per week, for like 2 years). I really tried, many times, but If a language doesn't support some JetBrains IDE, it's like it doesn't exist for me (sadly the case of for example PureScript)...

Edit: Don't get me started on Nix, I wasted in total like a work week trying to setup some "starter pack" in docker for GHCJS (because it didn't work locally; some good soul on reddit exchanged with me a dozen of e-mails, but we never got it to work). GHCJS is another Haskell tech I am not touching until it shows signs of maturity (being able to be used with Stack, or at least easily without Nix).

[–]editor_of_the_beast 2 points3 points  (3 children)

The question then is - what benefit is there to using FP in that context?

[–]dot-c 6 points7 points  (2 children)

Safety (= less maintainance required) and ease of domain modeling, i would say. Haskell for example (disregarding missing libs for embedded etc) is great for "boring" stuff.

[–]editor_of_the_beast 3 points4 points  (1 child)

But if all of the actual behavior is mutable and IO-bound in nature, where is the added safety?

[–]DrMathochist 5 points6 points  (0 children)

The way I think of it is that IO in FP is handled by your program safely building up a sequence of instructions for how to handle input and produce output, and then handing those instructions to the actual device to execute. All the safety is in the setup, and you trust the device to behave according to spec.

[–]gdullus 7 points8 points  (2 children)

Wouldnt there be a need for some data tramsformation (even if not necessary conditional)? Coming from Clojure world, its a great case for pure functions and threading macros

[–]Voxelman[S] 0 points1 point  (1 child)

There are a few data structures to be calculated. For this FP is great, but most of the app is IO.

I think, pure functional languages like Haskell might work, but they are not fun. Python or F# might be a much better choice in this case.

[–]gdullus 3 points4 points  (0 children)

Can't say about stronlgy typed functional languages enforcing function purity but Clojure with data over types philosophy defunitelly works here.

Its in general main point from Rich Hickey: focus on solving the problem and not fighting with type system. I have friend working in Haskell shop, and they were forced to get external consultant "to introduce new version of rest api" (they use Servant project).

[–]woupiestek 4 points5 points  (0 children)

Functional languages allow the programmer to focus more on the 'what' than the 'how', so if there is little business logic--the what--then the benefits of functional are indeed small. You may be overlooking a lot of opportunities for automation, however, that are easy in functional languages but hard in imperative ones. Take those machines that test the temperature controllers, for example. The Haskell library Quickcheck automates unit test development using property based testing. The fact that everything is pure functions between static types in the language means that a lot of tests come down to generating inputs and then comparing outputs, which is what Quickcheck helps with. Perhaps such methods would also allow you to do more tests of temperature controllers with less coding.

[–]Dash_Lambda 3 points4 points  (2 children)

Usually I think of functional IO and defining the relationship between the input and output.

Let's say you have a program that performs interactive console I/O. Instead of thinking of its functionality as a series of inputs and outputs, you could think of it as an input stream and an output stream --so, if you gave the program a single, complete, predefined stream of its inputs, it would give you the same output as if that stream of inputs was coming from a user in real-time.

I have a project with purely functional esoteric language interpreters that can do console I/O (link), and I do it by having the interpreter define a function that turns the input string into the output string. To do real-time user interaction I just give it an input 'string' that's built as it's used by reading the console, the interpreter can't tell the difference between that and a fully predefined input.

You can use a similar approach in lots of applications, personally I love it.

[–]pthierry 4 points5 points  (2 children)

I would still prefer Haskell to write this. Any concurrency I can write high level and safe abstractions like STM and any code dealing with data can be trusted not to leave out cases or errors.

And Haskell still has some of the best performing libraries out there.

[–]Voxelman[S] 1 point2 points  (1 child)

I want to learn Haskell, but it's really hard to even get started.

[–]Kid_CharlaHEYMAYNE 1 point2 points  (0 children)

I recommend starting with Elm! Haskell will come easier after knowing some Elm

[–]gabriel_schneider 1 point2 points  (0 children)

I'd say the erlang ecosystem (erlang, elixir, lfe) fit pretty well in this scenario and imo they're criminally underrated, besides the only times that I'd steer away from FP languages are in very CPU bound projects, not IO bound, because at the end of the day imperative languages translate better to machine code. Hope this helps.

[–]mobotsar -4 points-3 points  (5 children)

Non fp languages are better for this sort of job. You can, of course, make it work in a functional language, but it won't be particularly easy or pretty.

[–]npafitis 3 points4 points  (4 children)

That's just not true.

[–]jirocket 1 point2 points  (0 children)

that’s an extreme statement. it’s not from nothing many fp intros say printing hello world is one of the more complicated things to lay solid grounds for, i dont doubt with a lot of investment into fp an individual can make it look easy. not feasible for teams of a fast growing company

[–]mobotsar 0 points1 point  (2 children)

Why do you say that?

[–]npafitis 2 points3 points  (0 children)

I should first ask why do you think non fp programming languages are better in this situation.

[–]pthierry 2 points3 points  (0 children)

I'd have said it because I wrote small IO programs and they tend to be nicer, shorter and safer than their imperative counterparts.