4 ms·
It is not the defining feature but loss of the loop is one of the most obvious differences for people who look at a functional language Rust has immutability, p
by vvpan 11mo ago
It is not the defining feature but loss of the loop is one of the most obvious differences for people who look at a functional language Rust has immutability, pattern matching, etc, but it remains an imperative language with "some functional features". Or this is my subjective analysis.
- jlouis 11mo agoYou can be functional "in spirit" more than purely functional. OCaml and Standard ML falls into this category. Ocaml has loops for instance. You might just not see many loops if code is written by OCaml developers, because there's frankly no need to use them in a lot of places. You often want to lift the abstraction level of iteration to an arbitrary data structure such that you get freedom of implementation. See Applicative and Monad.
- int_19h 11mo agoBut, again, loops aren't automatically imperative per se. Consider loop / recur in Clojure for an example. For someone coming from JavaScript, it's not really much different from writing a while loop where every branch has to terminate with an explicit break or continue.
- tome 11mo agoSo is Haskell not functional? Or an imperative language with some functional features? -- ghci> example -- Triangular number 1 is 0 -- Triangular number 2 is 1 -- Triangular number 3 is 3 -- Triangular number 4 is 6 -- Triangular number 5 is 10 example = runEff $ \io -> evalState 0 $ \st -> do for_ [1..5] $ \i -> do n <- get st let msg = "Triangular number " <> show i <> " is " <> show n effIO io (putStrLn msg) st += i where st += n = modify st (+ n) (This is not a trick question. Simon Peyton Jones described Haskell as "the world's finest imperative language" [1], and I agree. This code is written using https://hackage.haskell.org/package/bluefin https://hackage.haskell.org/package/bluefin) [1] Tackling the Awkward Squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell