/fpt/ - Functional Programming Thread

Happy new year!

Resources:
>Erlang
learnyousomeerlang.com/content
>Elixir
chimera.labs.oreilly.com/books/1234000001642/index.html
elixir-lang.org/learning.html
>F#
fsharp.org/learn
>Haskell
en.wikibooks.org/wiki/Haskell
0x0.st/pbp.pdf
>Common Lisp
gigamonkeys.com/book
cs.cmu.edu/~dst/LispBook/book.pdf
Paul Graham's ANSI Common Lisp
On Lisp
Common Lisp Recipes
Land of Lisp
An Introduction to Functional Programming Through Lambda Calculus
>Clojure
braveclojure.com/foreword/
The joy of Clojure
>Scheme
SICP
Essentials of Programming Languages
How to Design Programs:
ccs.neu.edu/home/matthias/HtDP2e/
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/
ocaml.org/learn/tutorials/
>Scala
Functional Programming in Scala (Chiusano and Bjarnason)
Atomic Scala (Eckel and Marsh)
Programming Scala (Wampler and Payne)
Programming in Scala (Odersky, Spoon and Venners)
>Elm
guide.elm-lang.org/
>PureScript
purescript.org/learn/

Other urls found in this thread:

amazon.com/Type-Theory-Formal-Proof-Introduction/dp/110703650X
kevinalbrecht.com/code/joy-mirror/j02maf.html
tunes.org/~iepos/joy.html
en.wikipedia.org/wiki/Autism
twitter.com/SFWRedditGifs

Put the link to the old thread

Previous thread: It's archived

>It's archived
It seems everyone had fun in the New Year night.

I went to sleep and woke up and it was gone

>no anime image
sage

sorry user

bump

Image is wrong. The function is an argument, not a parameter.

>The function is an argument, not a parameter.
herewego.jpg

jay pee gee

bump

Hey I have a problem understanding Erlang:

So it's got three main design goals:
- fault tolerancy
- concurrency
- hotswapping code to preserve uptime

Shouldn't the first point be just "make good software"? Why rely on writing code that eventually crashes?

And I don't understand BEAM's implementation of the second point: so they call them processes but they're not OS processes and they're not threads even though they are pretty much threads.

The third thing I get. Gotta keep emergency lines running.

It's not one of the "safe languages", so I imagine you generally consider some kind of fault to be inevitable

no matter how good your software is, there will be some faults (in hardware, network, underlying software implementation). Erlang wants you to handle these kinds of things with the minimum possible damage, which might mean crashing a thread and starting it over rather than the conventional process of trying to correct what went wrong directly.
meanwhile, Erlang processes are pretty much software threads. they're extremely light weight in comparison to OS processes. the threads can be distributed over multiple OS processes IIRC (can definitely be spread across multiple cores)

I'm writing a GUI for my program.

I am doing an operation interacting with the internet. I pop up a button after search has been pressed, allowing the user to cancel it. Once the search has finished I emit the signal to close the dialog which is waiting on a response in the main thread.

The issue is here:
childThread

I'm also learning Elixir and Erlang so I can't really give you an authoritative answer but here's my take on it.

With certain systems, especially telcos, failure cannot be completely eliminated. Even very good reaches failures at some points, and when you factor in millions of users, different hardware and potentially different versions of software, you have a recipe for failure.

So instead of trying to catch all the errors, make layers of code. The outer layer should be as fault tolerant as possible, but you let the inner layers crash while safeguarding state.

"Good software" is a fallacy. Whether you know it or not you're talking about defensive programming, but defensive programming is not good for these kinds of systems.

As a side note, not all Erlang code is supposed to crash. At least some part of your code should be fault tolerant, otherwise it wouldn't be any different than other programming languages. And that's the beauty of it. With OTP, you can have a very lean fault tolerant ring supervising many chunks of code that you let crash. By reducing the complexity, the system becomes more manageable. By safeguarding state on the less critical chunks, the reliable part of the system can restart the crashed chunks as if nothing had happened. And this is how you get 99.999999% uptime.


As for the processes, they aren't OS processes but they do run on all the threads with very little overhead. I don't know how they are implemented by BEAM, but I know they work like that.

Hi I wish to know the best / most useful Functional Programming language, and some resources for it that are competent.

haskell

>functional programming language
>functional

I've never seen a bigger oxymoron in my life

>retard needs to butt in and show the whole world he is a retard
kinda wish this stopped happening, but then again it's an easy way to weed out the fools

>doesnt give any reasons

Yes, the Erlang virtual machine uses an M:N threading model, where M is an arbitrary amount of userspace threads and N is an arbitrary (you usually specify how many) amount of OS threads.
This means it supports parallel computations, where as it used to only support concurrency on a single core unless you ran multiple instances of the virtual machine.

I've never seen a bigger moron in my life

why functional programming is shit? especially f#

Just fucking program in whatever you want, all languages are shit. COMPUTING IS SHIT

also lern 2 proofs in code (idris) or use a proof assistant (coq)

because your feeble brain can't comprehend it sorry not sorry ;)

F# is unironically one of the best programming langugaes in existence.

F# is garbage but functional programming in general is basically flawless.

Assuming that the web application in question was reasonably efficient, what kind of load would it take to max out the average 4 core VPS (think DO/Vultr) with a web app written with Elixir + Phoenix? What about a dual-core dedicated box?

Shoo, Pajeet

name literally one bad thing about F#

Shitty type system, shitty module system, .NET baggage, etc.

F*, on the other hand...

those first two things are just symptoms if the last thing. I agree they are problems, but working well with .NET is a HUGE advantage for all kinds of shit.

Like Windows GUI programs?

That's one example yes.

>[1; 2; 3] : int list
>[|1; 2; 3|] : int []

And what's the problem exactly?

that would be like Haskell having
[1, 2, 'a'] :: (Int, Int, String)
(4, 5, 6) :: Int * Int * Int

the syntax makes literally no sense and works to confuse you even more

Why? That Haskell code makes no sense. The F# code does. What exactly are you finding confusing about it?

int [] is a hold over from C#, you would typically write
[|1; 2; 3|] : int array

Though you could never write code like that anyways because the compiler already knows the type of [|1; 2; 3|]

I just want to say that this book is awesome. A great formal introduction to the typed lambda calculi, the lambda cube, system F, propositions as types, etc. If you don't have the academic background, it is very thorough and holds your hand so even if you're at babby level you should be able to follow along.

it's retarded that they decided to mix C# and ML syntax like that. it's like list vs. int list.

It's a minor annoyance at both, and it serves a very reasonable purpose.

it's not like you're going to see int [] and wonder what type that might be. Not that that's what you would write anyways.

at most*

the language is designed poorly though. another example of this is namespaces vs. modules (the distinction is awkward at best in F#). the way the language is designed fails to understand what it's trying to achieve and makes a lot of weird edge cases or feature overlap like this. don't get me wrong, it has a lot of really great stuff, but it also is missing a good goal in its design. it's not clear if we're getting a new syntax for C# or a new language that's supposed to be better

Just what I need. Link?

The goal is very clear. To create a functional first multi paradigm langugae (much like OCaml) for .NET.

I agree modules are a bit weird. But they are like that so that they work well with .NET. The F# team very carefully avoided anything that wasn't genuinely going to gel well with the .NET framework. better to have a small amount of good shit, then try to half bake loads of stuff (Scala).

amazon.com/Type-Theory-Formal-Proof-Introduction/dp/110703650X

also on libgen if you are a pirate.

What is the smallest FP language?

it just feels out of place to design an ML dialect that doesn't make any decisions that go against the imperative parts of .NET. I guess that doesn't make it bad, but that's what stops it from being a great language. the object system is what makes me really dislike it.

Unfortunately, despite being a Haskell programmer, I do not make $300k :(

Iota or Jot.

>but that's what stops it from being a great language
Sure, but it depends by what metric. You can assess a langugae purely on its technical merits, and then of course OCaml and Haskell will be superior to F# by miles. But programmers tend to forget just how much of programming is googling for some libraries and gluing them together with some code. Ecosystem is HUGE in terms of how much actual real world work you can get done with a langugae. F# gets a very handsome portion of the benefits of those other langugaes, while have a very comfortable way of using the entire .NET ecosystem and targeting all .NET platforms.

By comparison, many on Sup Forums have convinced me that OCaml is a superior langugae technically, but I can't think of a compelling reason to learn because I just don't know what I'd use it for except as a curiosity. The ecosystem is too poor to use for anything that would be interesting for me.

Fortunately for you, the pirate edition is free.

Yeeeeeaaahhbuddy. λΠ, here we come.

They are too pure so there's barely any use for them.
Literally too pure to be used.

Any small FP language but bigger than Iota?

LazyK?

I thought you wanted the smallest language, not the smallest practical language.

After Iota and Jot, you get the SKI calculus, then lambda calculus.

Most minimal practical language? Maybe McCarthy's LISP. Have a look at Joy, too:
kevinalbrecht.com/code/joy-mirror/j02maf.html
tunes.org/~iepos/joy.html

>I thought you wanted the smallest language, not the smallest practical language.
Well, I wasn't clear enough.
Thanks.

if I kill a thread with killThread, how can I kill its children as well? Do I have do unmasking so I can catch the exception in the function I'm calling and kill the children from there before killing itself?

in haskell, that is

maybe. I just feel like I'm needlessly getting caught up fiddling with the language when I write F# code. as an example, I had to write an instance of ICommand in my code to get something working with WPF. not only was it a lot of extra code, but it was also messy because it didn't really fit well with the rest of the code but I couldn't separate it out into its own module either. the whole thing would've been avoided by the OCaml object system which would've made that code probably 30-40 lines shorter simply because I could avoid having to define this whole new type.

Ah, I just needed to use an MVar list to pass the new threads to the parent. Don't even need to bother removing them when they are done as killthread just does nothing on an already completed thread.

Working in haskell, if i've got a function that takes two values, I can use arg1 `f` arg2[\code]. Is it considered good practice to replace a lambda over arg2 with this sort of function? Which is prefered?
(x -> f arg1 x)
(`f` arg2)

shit, reformatted above question

Working in haskell, if i've got a function that takes two values, I can use arg1 `f` arg2.Is it considered good practice to replace a lambda over arg2 with this sort of function? Which is prefered?
(x -> f arg1 x)
(`f` arg2)

Did you mean to write

(\x -> f x arg2)
(`f` arg2)

?

If so, I would prefer the latter. Btw, another thing you could write is flip f arg2 or even f ?? arg2

yeah, good catch.
I've never seen ?? in haskell, i'll have to investigate. Thanks bud.

(??) is lens-style, it's just an operator version of flip

What's an easy and useful fuctional language?
Something like python but functional

Erlang
>Something like python
Haskell and F#

Yes, (`f` arg2) is preferred.

>Haskell
How do I iterate through lines of a file while accumulating state and on certain lines yielding new objects?

Can someone explain what "functional programming" actually is?

an example maybe?

Depends. What are the specifics of your problem?

en.wikipedia.org/wiki/Autism

Certain lines of the file contain data that I want to use in transforming other lines.

i.e. Lines of Type A contain useful data that I will use later and thus want to keep, and lines of Type B I want to transform, using the data I extracted from lines of Type A.

Is there a nice way of doing this? State monad seemed to be a nice way to do it but I'm not sure if it's a good route.

Generally the use of principles derived from (typed or untyped) lambda calculus -

Higher order functions, type systems, pattern matching

Sounds like something you would use a mapM over the state monad for, if your state really needs to be arbitrary

[TypeB] -> StateT [TypeA] TypeC
?

Functional programming is a meme.

a good meme

It's not very well defined: worse so that 'declarative' and 'imperative', but better than 'OOP'.

The language should be at least roughly based on the lambda calculus (i.e., most values are functions) or one of its typed derivatives. An alternative is combinatory logic.
Functional languages are organised around expressions (with evaluate to a value) rather than the statements of imperative languages (with are just instructions to the machine).
First-class functions (including anonymous) are a must. Higher-order functions are nearly so (consider Joy as a counter-example).
Less strictly, most (but not all) functional languages tend to have well-thought-out, strong, static type systems. They might also emphasise the idea of purity (no side effects).

bump

I have a problem understanding beta reduction. Why does (lambda v.z)z reduce to z?

lambda v.z
or
\v.z
this is like saying
"given v, return z"

(assuming v doesn't appear _inside_ of z, i.e. z is LITERALLY 'z')

In fact, (\v. z) _ returns z WHATEVER you apply to it, asusming v doesn't occur in z.
This is the equivalent of Haskell's const z

It's 'k z', muthafuckaaaa

When is someone going to put functional language features into a language that doesn't have syntax like ass?

Got it. Thanks! I guess my confusion arose from the fact that the return value and the parameter are the same...

What would this look like in Haskell?

\v -> z
\_ -> z
const z
pure z

They tried that with Haskell, but they accidentally the whole eager.

>pure z
>fire up GHCi, it works
wat

I assume this has something to do with (->)'s instance of Applicative?

Yes. pure is K and is S!

Just looked it up. My face when the definitions for 'pure' and '' for functions are literally 's' and 'k'.

What does it mean?

God fucking damnit, you beat me to it by like 30 seconds.

What does it meeeeeaaaan?

>there isn't an OS where files and their extensions are sigma types
>there isn't even an OS where files and their extensions are existentials

That would be the same thing you dumbass.

WWWWHHHHYYYYYYY

not all sigmas are existentials

Σx:A.B(x)

"There EXISTS x:A such that B(x) is true". Where truth just means there's an inhabitant of that type.