
Can I do FP in my language? - spinningarrow
http://www.lispcast.com/fp-in-my-language
======
randcraw
I'm very tempted to explore a coding style in C and C++ using a more
functional idiom. I think both languages could benefit greatly if made more
transparently 'intensional' \-- more restricted use of in:out function
arguments, explicit return variables, minimal pointer arithmetic, more
explicit pointer expressions, etc.

Has anyone attempted to devise a FP-style guide for non-FP languages? It could
be fun to reimagine their now-antiquated idioms spoken in a more intuitive
and/or legible accent.

~~~
crimsonalucard
Just make every variable immutable. That is the only style guide you need to
make the program part of the fp paradigm. The rest of the "functional
patterns" flow naturally out of this restriction.

~~~
joncampbelldev
If the standard data structures in your language are mutable this will not
save you. Luckily many languages like Java have nice third party persistent
collection libraries

~~~
crimsonalucard
You can treat the variables as immuteable even if they aren't...

------
gaius
The answer is "not if you don't have first-class functions" in your language.

~~~
crimsonalucard
This is not true at all. The only technical requirement for you to follow the
functional paradigm is immutability.

Keep in mind that passing a first class function into another function as a
parameter is not that far off from passing the result of that function as a
parameter.

~~~
gaius
How do you figure? map() for example, is similar to but not identical to
for... next.

~~~
crimsonalucard
You can still use recursion to loop. First class functions are a convenience
to functional programming not a requirement. Map and reduce and other higher
order functions are also not a strict requirement for fp.

Without first class functions an immutable program can still be turing
complete. I would say that if a language only supports recursion and is
immuteable, then that is all you need for the language to be part of the fp
paradigm.

~~~
fnord123
Where did you get this definition?

~~~
crimsonalucard
Intuition and experience. The definition naturally occurs to you over time,
I've never seen the isomorphism between immutability and functional
expressions illustrated though. However, it is explicit that every functional
language is immutable as a requirement.

I'm sorry to say that if you don't understand this, you haven't really
understood what functional programming is... You're just a guy who understands
higher order functions.

~~~
jcelerier
> Intuition and experience.

...

> I'm sorry to say that if you don't understand this, you haven't really
> understood what functional programming is...

you can't be serious can you ?

~~~
crimsonalucard
I am dead serious. If you don't understand this:

>The only technical requirement for you to follow the functional paradigm is
immutability.

Then you don't understand functional programming. I can't take you seriously
if you claim otherwise.

Obviously, you highly disagree with me. Feel free to elaborate rather than
waste my time with obviously rhetorical questions.

~~~
jcelerier
> The only technical requirement for you to follow the functional paradigm is
> immutability.

So are you saying that for instance both CaML and LISP aren't FP ? Because
both are certainly not about immutability and you can write mutable messes in
either.

~~~
crimsonalucard
[https://www.wikiwand.com/en/Functional_programming](https://www.wikiwand.com/en/Functional_programming)

Read the first line:

"In computer science, functional programming is a programming paradigm—a style
of building the structure and elements of computer programs—that treats
computation as the evaluation of mathematical functions and avoids changing-
state and mutable data. It is a declarative programming paradigm, which means
programming is done with expressions[1] or declarations[2] instead of
statements."

Key phrase: "avoids changing-state and mutable data."

You can use C++ as if it was a functional language just like you can use LISP
and CaML as if variables were mutable. However in both cases you are bending
the language to follow a paradigm that is NOT functional.

------
whipoodle
> People say they're doing FP in JavaScript. What they mean is they sprinkle
> reduce and map around and they use some pure functions. There's a lot of
> value in that. But otherwise their code is procedural. They have not learned
> the paradigm, only the features.

Yeah, but that's probably okay. I feel like there's a strong implication here
that following the paradigm has inherent value but I'm not sure why I should
think that's the case.

Basically, I agree with the claim above that "there's a lot of value in that."
:)

~~~
crimsonalucard
I disagree with you. Map and reduce achieve the same result as a for loop so
why not use a for loop? The end result is the same it's just a bit cooler or
convenient to use map in place of a for loop, that's the only reason why you
would use one instead of the other in a imperative program.

The functional paradigm fundamentally alters the way the entire program is
constructed, and you can do it without map reduce or filter. You really need
to play with haskell or lisp to see the light.

~~~
dragonwriter
> Map and reduce achieve the same result as a for loop so why not use a for
> loop?

Map and reduce are more clear as to intent for the subset of what a for loop
does that they, together, do, and that clarity and limitation has practical
advantages for things like parallelization and distribution, which is why
MapReduce is an important distributed computation model and ForLoop isn't.

> The functional paradigm fundamentally alters the way the entire program is
> constructed

Programming paradigms are useful at levels other than “entire program”, which
is the reason many modern languages are multiparadigm.

~~~
jcelerier
> and that clarity and limitation has practical advantages for things like
> parallelization and distribution,

and how many of these "parallelized and distributed" map and reduces actually
exist out in the wilderness of javascript code ?

------
crimsonalucard
This article is describing something that is very true but in the end it
doesn't teach you how to do FP in an otherwise imperative language. This is
surprising as it's just one single devastatingly simple rule you need to
follow in order to do functional programming:

After you declare and assign a value to a variable NEVER change it again.
That's it. Immutability. Almost every other functional programming pattern
flows naturally from this restriction.

People already know the definition of functional programming and that is a
functional program is a program that consists of a single expression and that
the complexity of this single expression arises from the fact that this
expression is the composition of other smaller functions...

What they don't know is that if you take an imperative program and make every
variable immutable and construct your program following this restriction the
final result will be identical to a functional program! The two concepts are
isomorphic!

Try it if you don't believe me.

~~~
kwhitefoot
I do this for my own code but 20 years of persuasion has failed to convince
any of my colleagues.

------
bradknowles
FP? Floating Point?

I should hope you can do floating point math in just about any language.

Yeesh.

;)

