/fpt/ - Functional Programming Thread

Next-level programming thread.
They say, to become fluent in a foreign language, one must stay in that country.
Stay in the thread, become fluent in functional programming!
Last one: Resources:
>Elixir
chimera.labs.oreilly.com/books/1234000001642/index.html
elixir-lang.org/learning.html
>Elm
guide.elm-lang.org/
>Erlang
learnyousomeerlang.com/content
>F#
fsharp.org/learn
>Haskell
en.wikibooks.org/wiki/Haskell
>Lisps
>>Common Lisp
gigamonkeys.com/book
Common Lisp Recipes
Land of Lisp
An Introduction to Functional Programming Through Lambda Calculus
>>Clojure
braveclojure.com/foreword/
>>Scheme
SICP
Essentials of Programming Languages
Art of the Propagator
Little Schemer
The Seasoned Schemer
The Scheme Programming Language by Kent Dybvig
Realm of Racket
Lisp in Small Pieces
>OCaml
realworldocaml.org/

Feel free to add more.

Other urls found in this thread:

en.wikibooks.org/wiki/Haskell/Lists_and_tuples
paulgraham.com/avg.html
twitter.com/NSFWRedditVideo

Thank you for using an anime image!

Is the book in the OP better than learn you a haskell?

Probably. LYAH is a bit out of date.

No love for Scala?

Good Scala books:
Functional Programming in Scala (Chiusano and Bjarnason)
Atomic Scala (Eckel and Marsh)
Programming Scala (Wampler and Payne)
Programming in Scala (Odersky, Spoon and Venners)

Also, The Joy of Clojure is another very good Clojure book.

I'll be adding those.

>No love for Scala?
Yes, exactly

bump

So im reading through haskell book in OP and its pretty good. Are there any tutorials/books/youtube vids where someone makes something useful with haskell? For e.g if i were to learn c# i could just have fun by making keyloggers or whatever, and find bunch of youtube vids, on the other hand haskell vids seem to be just recordings of some talks and pretty abstract. I dont mind learning abstract its just i'd like to see concrete, practical examples

I hope you are still here, but the biggest difference between functional and imperative is that imperative is focused on assigning values to things, and these assignments change how your program works. Functional on the other hand, is focused on doing as many things as humanly possible while changing as little as possible.

The idea is that it is easier to reason about a system that doesn't rely on pieces of data that anything could have written to. This is because there are a very large number of possible combinations you need to consider if you do. Sometimes unexpected combinations of these values (called "state") can cause subtle bugs you don't realize, and can sometimes crop up in completely unrelated parts of the system and cause some serious headaches later.

Functional tries really hard to limit the state you need to a bare minimum.

However, generally speaking, imperative code is faster than functional code. This means historically the norm in game development is imperative, because highly-optimized engine code and graphics programming can be more optimized with it. However, this is not a rule--I think a good thing to do is to use the tools that you like the most, because game development is faaaar from a short process, and you don't want to lose motivation partway through.

So, I suggest you should use the things you like using the most.

So, for imperative, pros:
- It's faster
- Easy to reason with on a small scale (This is why you should keep your inheritance shallow)
- Amazingly easy to model real-life things with imperative styles
- Tools for working with these languages often border on wizardry

Imperative cons:
- Holy-shit levels of state permutations to account for
- The wrong state in the wrong place hurts unexpected amounts
- Very hard to test in comparison to functional code
- All of this means technical debt is far easier to accrue, be careful!

For functional, pros:
- Ridiculously easy to test
- Many kinds of bugs become impossible naturally
- You can experiment/play with your code in much more useful/fun ways
- Abstraction is far more reliable, particularly as you layer it

And functional cons:
- Tools for these languages are usually harder to get used to
- You have to separate code that changes anything from everything else
- Harder to organize
- Not used nearly as often as imperative

For recommendations, I would say to pick up on an imperative language first, and then move to a functional one. That way you can better understand what state actually means to you, and get acquainted with all the neat tools and ways of solving problems that both sides use.

For the two languages, I would say that Java and Clojure make a good pair. As a web developer and aspiring game developer, you can seriously benefit from both languages. Here is my reasoning for the two:

For Java:
- There is lots of demand for Java in web development, so it is worth it even if you get nothing else out of it
- It shows you Class-based Object Oriented
- It gives you a tremendous head start for learning C++, the industry standard for game development
- There is a lot of very nice game development libraries for Java
- If you make games with it, they are cross-platform by default
- Java development environments are some of the best in existence

And for Clojure:
- It allows you to build programs from scratch as you are running them
- It has amazing ability to let you experiment with anything and everything at any time
- A lot of tutorials and books show you how to write games, as well as explain game development
- You get concurrency for nearly free
- It shows how to work with a functional language, while letting you use your Java knowledge
- ...and speaking of, it runs everywhere Java does
- ...and you can use Java code in Clojure, as well as vice versa
- ...or even embed one language in the other, so you can use them at the same time if you want
- Clojure is surprisingly amazing for web dev, so it is worth it even if nothing else comes of it

Write a -interpreter/compiler

If I am maybe just intermediate level with imperative languages like C++, Java and Python, is it still worth me trying to pick up a functional language or am I better off just sharpening my c++ skills or something?

nobody is better off with c++

Why cant you do both? Its also possible that learning new paradigm might improve your c++ skills right?

> web developer and aspiring game developer, you can seriously benefit from Java and Clojure
> Java and Clojure
> game development
Wew, lad. Also you forgot to mention that most modern imperative languages like C#, Swift and Rust implement most of useful concepts from functional programming, making them the best from both worlds.

I'd say it's definitely worth it, because it gives you more ways to solve problems which can definitely be applied to C++.

"How to Design Programs" should be high up the Scheme list.

Except people who want to be employable (although other languages would get you more money I assume)

I guess so

Yeah I'll continue with it then

The main problem I have is finding things to make, so I don't end up coding a lot and stop improving, so I started learning Haskell a little because of it. Hopefully I can learn some more and think of some cool things to make

I'll be adding this.

Yes, I suggest Clojure for game development.

Even the book the OP suggests for Clojure is about making games, plus there are some very nice libraries for doing just that. You don't even need to target the JVM for this. I mean, a lot of ClojureScript demos are games too, and it does that quite well.

To make things even better, it all has a ton of other benefits for the person asking.

Those were my reasons.

>Also you forgot to mention that most modern imperative languages like C#, Swift and Rust implement most of useful concepts from functional programming, making them the best from both worlds.
Well Java implements a lot of that too. Not on the level of Rust, mind you, but it is enough to be quite nice for some more functional code in the language.

However, none of these languages implement all of the most useful functional concepts IMO.

Then again, I am biased.

>Hopefully I can learn some more and think of some cool things to make
I'm sure computers have annoyed you before, right?
A lot of these annoyances can make some pretty nice little practice projects.

If you have a hard time thinking of a project, try solving one of these..!

>I'm sure computers have annoyed you before, right?
I've recently started doing this (sorting folders, etc) but I haven't ran into anything major. Maybe i just subconsciously ignored them because I thought they would be too difficult

Nice one.

Any haskell resources to get me going in a weekend? I know C, Python, Java, Go and a bit of Common Lisp.

Where can I download Haskell Programming from First Principles?

The link on libgen is an out of date version.

version on libgen is from 2016, surely it cant be that big of a difference?

bump

Why is this allowed?

>looking through Haskell book
>mfw this language sucks me in
Well, I should relax and let Haskell suck me whole?

yes

Is Haskell a meme?

Just looks like a bunch of jarble, and i have been programming for 3 years.

Never touched a purely functional language before.

It's a really nice programming language

Meme doesn't mean what you think it means. I get it, you're new here, but please at least try to learn what the words used here actually mean.

Will learning lambda calculus help me learn functional programming or should I not bother?

why did you retards make a separate thread for your meme languages

refer to

Yes, it will help immensely
The principles of FP are largely those of lambda calculus, + type systems

But generally learning an FP language means learning lambda calculus.
Haskell for instance.

Cool. Is there a recommended lambda calculus book or something?

I would honestly just recommend learning an FP language.

Here is the lambda calculus:

e,a,b : x
: \x.e
: a b


i.e. an expression such as e, a or b is either a variable (such as x), a lambda abstraction (bind a variable x in expression e) or an application of two expressions

that's it

yay, i learned functional programming

now you need to learn how to use it, and type systems

look at the haskell wikibook in OP

will learning a fp language also improve my lambda calculus? seems you would already have to be somewhat skilled in it to pick up the language.

it's more like lc is the basis of fp

if you can program you can probably figure out LC anyway
learning haskell will involve learning LC

pure LC, by itself, isn't very useful as a language (but it is turing complete, so it can do anything)

How does one make Brainfuck compiler in LC?

I need women "programming" images please, thank you.

A lot of effort.
Pick an FP language and learn it

bump

I might be dumb.
en.wikibooks.org/wiki/Haskell/Lists_and_tuples
Why cons8 [True,False] doesn't work?

I'm on chapter 3 of learn you a haskell for great good, should I switch to the link in the OP?

What do you mean?

const [True, False] is a function returning [True, False]

LYAH is fine

It's cons8, not const. The types are not the same, lists contain elements of the same type.

But tuples do?
What's the difference in their machine realisation?

Yes, and tuples with different element types are different tuple types, eg

(True, False) :: (Bool, Bool)
((), False) :: ( (), Bool)
(3, "hello") :: Num a => (a, String)

i never understood that "Num a =>" thing

It's a constraint
You can think of it as meaning "where a is a numeric type"

So for instance
The type of (+) is
Num a =>
a -> a -> a

i.e., a -> a -> a, where a has a Num instance (you should learn about type classes later)

ah that makes sense

Can you set multiple constraints?

Can someone explain to me why some people on Sup Forums consider functional programming better than OOP?

Yes.
It's the same syntax as a tuple type.

(Num a, Eq a, Ord b) =>

OOP isn't very good, and hasn't got any real basis other than trying to vaguely turn language into programming language ("nouns and verbs")

FP has strong foundations

It's not better, it's just better than false OOP languages like Java, where "everything is an object' isn't entirely true. The only reason Java doesn't support multiple inheritance, is because they were too lazy to solve the diamond case, so they came up with the hack called interfaces. It's not just my opinion here, even Uncle Bob thinks the same. SmallTalk did OOP right. Python does OOP right.

I am complete noob and i just started learning c# (so OOP), i have to admit, f# sure looks sexy, but i still can't find any characteristics of functional languages that makes it more preferable than OOP languages

Do you use LINQ?
Do you use lambdas?
Do you use that async syntax?
Do you use tuples?

Functional programming is incomprehensible to code monkeys, with their tiny brains. It makes us feel superior.

Where is JAVASCRIPT????
fucking weeaboo

Not programming
Not a language
Not functional
Not functional

Look up the Facebook Spam Filter. It's written in Haskell. Look up the Ocaml teams at Facebook. Functional programming works better in a lot of cases(not all), so it's used where preferable.

>Do you use LINQ?
Yes, very useful

>Do you use lambdas?
same as above

>Do you use that async syntax?
written first yesterday and fucked up, UI thread is still freezing, i have to learn way more about async/await

>Do you use tuples?

no

those are all functional

I use those in c#, i don't get it

C# is not a strict OOP language, it has a lot of very useful functional elements brought over from F#

those are functional features of C#

So, linq was in f# before c#? interesting, i didn't know that

I don't think that F# had LINQ originally, but that's aside the point.

only retards don't understand js
got news for you , it is a language
of course it's functional, JS can do everything, that's why ur butthurt

does LINQ have a function that takes a single element and converts it into an enumerable?

JS isn't functional in either sense of the word
a language is, by definition
"the method of human communication, either spoken or written, consisting of the use of words in a structured and conventional way."
>structured

Javascript has no place in this thread. Leave.

Tuple can contain any types but it's fixed.
List is free but it can contain only one type.

Why?
Why it's so useful?
Can I contain any type and not be fixed?
>inb4: list of tuples

The only function that would work on such a type would be the length function or (:).

You're better off just using natural numbers.

To explain that, you need to start realising that what you can do to a value depends on what its type is.
You can add integers.
What if you had to make something that works with ANY type?
Well, you can't add literally any type. So that's out of the question.
You can't display it as a string.
You can't do anything, because you don't know anything about it.
All you can work with is the structure.

It's like having a list of unit values.

>All you can work with is the structure.
I mean if you've got a list of any type.


You CAN have useful heterogeneous lists, using existential types. But it's rarely what you want.

The thing about existential quantification is it's like this:

data Showable = forall a. Show a => Showable a
instance Show Showable where
show (Showable x) = show x


Since we can only put "things that can be shown" into the box (the Showable constructor), then we know, by discovering the showable constructor, how to show that item.
It's like OOP style polymorphism.

So in this example,

Showable 3 and Showable False both have the same type. So you can put them in a list

[Showable 3, Showable False, Showable "Hello"]

Haskell can compile to C, right? Is there any way for me to see the C code equivalent of a Haskell program?

Maybe you should try looking at the C-- code instead

{-# OPTIONS_GHC -ddump-cmm #-}

pragma, or just the flag -ddump-cmm

You can also look at the core language
GHC has like 3 intermediate languages or something

In what irl situations and by irl I mean work, have you used functional programming?

paulgraham.com/avg.html

Now STFU

exactly the same situations i've used OOP and imperative programmign

So why should I learn functional if it is the same? Does it just feel better to write and read or what?

It is a new way of thinking. You shouldn't always think so narrowly. Sometimes, learning a new language makes you a better programmer, just because it broadens your thinking space.

Just a few of the companies using clojure along with datomic

Amazon
Netflix
Apple
Deutsche Bank
Salesforce
Soundcloud
Staples

and many more.

Functional programming is still used loads. But they are behind the curtains doing the job big companies need. And not making a fuss or trying to look fancy for nu-coders in starbucks yacking about small startups.

Just a quick question, I need a function that takes the difference of two functions.
f :: a -> b
g :: a -> b
[\code]
I could always write a lambda expression diff f g = \x -> f x - g x but was wondering if there was a more haskell idiomatic way to define diff?

Strangely I never thought about it that way. So where do you recommend to start? What language, what book?

Nobody uses Lisp these days, but it is the most powerful language on Earth, and it was created 50+ years ago. Get a weekend in Lisp, write some recursive functions, get to macros, and then you can safely get to more modern functional languages like Haskell, Clojure or OCaml(used extensively at Facebook), or Erlang(if you want to write highly concurrent architectures - Golang is for normies, doesn't even come close).

>Nobody uses Lisp
>Clojure
Yeah what?

I meant Common Lisp

I'm not sure but that looks sort of common
There's definitely a function for doing something like

f x - f y
f x * f y

But I'm not sure about your case. Maybe in Arrows?

Oh shit, I just thought.
If you've got profunctors,

dimap f (lmap g)

this is also the same as

lmap f . rmap (lmap g)


(->) is a Profunctor
rmap (lmap g) is because
a -> (b -> c)
is a nested profunctor, so you're saying "with the right hand side (b -> c), lmap it"