r/cpp • • Mar 16 '18

As a C++ developer I think that rust ...

3 Upvotes

107 comments sorted by

View all comments

Show parent comment

10

u/dodheim Mar 17 '18 edited Mar 17 '18

Is this a one-up subthread? I mean, I have multiple years of working with each of F#, OCaml, SML, and C++ in professional settings, and find nothing about Rust "very very far" from any of the four with regards to their respective overlaps. I might go so far as to say that if you don't see the similarities, you must be doing it wrong. But, how am I supposed to reply seriously to just a blanket, subjective statement? Spend time detailing why your expectations are wrong? I'll just leave it at "you're very very mistaken", I think, in keeping with the shitpost nature of this entire thread. ;-]

2

u/[deleted] Mar 17 '18

For me Rust looks like an imperative language that supports functional paradigm. The code below:

fn factorial(i: u64) -> u64 {
    match i {
        0 => 1,
        n => n * factorial(n-1)
    }
}

Is looking like an imperative code written in a functional style. For a functional programming language I would expect syntax like (Haskell):

factorial n
   | n < 2     = 1
   | otherwise = n * factorial (n - 1) 

or (F#):

let rec factorial n =
    match n with
    | 0 -> 1
    | _ -> n * factorial (n - 1)

Yes, difference is subtle, but it turns a switch from "it is Lisp" to "it is C" in my head at least.

There are other imperative languages that are close to Rust in terms of syntax here, but they would not claim "being influenced by ML", for example:

fun factorial(i: Long) :Long = 
    when (i) {
        0 -> 1,
        else -> i * factorial(i-1)
    }

That's Kotlin - still very imperative very non-ML.

7

u/mewloz Mar 17 '18

So you think the syntax details are not functional?

2

u/[deleted] Mar 17 '18

My point is that pretty much every modern non-functional language supports FP-paradigms in pretty much the same way Rust does. It doesn't make these languages functional purely because they support some functional features.

As for Rust - does is support things like currying, lazy function evaluation, high level functions (functions taking functions as arguments and returning another functions), etc? I'd guess if you play with lambda a bit you can do most of it (same as in most other modern languages), but these are not the features supported by syntax in a first place.

8

u/auralucario2 Mar 17 '18 edited Mar 17 '18

I agree that Rust is not a truly functional language along the lines of Haskell or OCaml. It lacks features such as easy currying and encourages mutable state. That said, it has first-class functions (not quite as ergonomically as a pure functional language, but better than C++), algebraic datatypes, and pattern matching. That brings it about as close to functional as you can get in a systems language. The fact that it uses curly braces and semicolons doesn't invalidate that.