[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

/fpt/ - Functional Programming Thread

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 150
Thread images: 7

File: a higher order function.png (3KB, 278x87px) Image search: [Google]
a higher order function.png
3KB, 278x87px
Happy new year!

Resources:
>Erlang
http://learnyousomeerlang.com/content
>Elixir
http://chimera.labs.oreilly.com/books/1234000001642/index.html
http://elixir-lang.org/learning.html
>F#
http://fsharp.org/learn
>Haskell
https://en.wikibooks.org/wiki/Haskell
https://0x0.st/pbp.pdf
>Common Lisp
http://gigamonkeys.com/book
http://www.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
http://www.braveclojure.com/foreword/
The joy of Clojure
>Scheme
SICP
Essentials of Programming Languages
How to Design Programs:
http://www.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
https://realworldocaml.org/
http://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
https://guide.elm-lang.org/
>PureScript
http://www.purescript.org/learn/
>>
>>58268853
Put the link to the old thread
>>
Previous thread: >>58244769

>>58268964
It's archived
>>
>>58268976
>It's archived
It seems everyone had fun in the New Year night.
>>
>>58269003
I went to sleep and woke up and it was gone
>>
>no anime image
sage
>>
File: karen haskell.png (818KB, 1280x719px) Image search: [Google]
karen haskell.png
818KB, 1280x719px
>>58269110
sorry anon
>>
File: bbe778ff39ce5c0dadc549c474025c4c.jpg (893KB, 4128x2322px) Image search: [Google]
bbe778ff39ce5c0dadc549c474025c4c.jpg
893KB, 4128x2322px
>>58268853
Image is wrong. The function is an argument, not a parameter.
>>
>>58269939
>The function is an argument, not a parameter.
herewego.jpg
>>
>>58270249
jay pee gee
>>
File: 89HISXK.jpg (94KB, 640x640px) Image search: [Google]
89HISXK.jpg
94KB, 640x640px
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.
>>
>>58271430
It's not one of the "safe languages", so I imagine you generally consider some kind of fault to be inevitable
>>
>>58271430
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 <- forkIO (childTasks dialog)
response <- dialogRun dialog

The dialog is ran after the searching is started on another thread. Therefore, if an error occurs on the searching, for example if there is no internet connection, potentially it could return the signal to close the dialog before it has actually appeared, I believe.

I'm not sure what would happen if this occurred, and I imagine it is very unlikely to occur as the other thread has a few lines of code to run before it returns the single, whereas there is only one line here.

Not sure if I can avoid this or what would happen if it does occur.
>>
>>58271430
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.
>>
File: download.jpg (16KB, 279x181px) Image search: [Google]
download.jpg
16KB, 279x181px
Hi I wish to know the best / most useful Functional Programming language, and some resources for it that are competent.
>>
>>58274499

haskell
>>
>functional programming language
>functional

I've never seen a bigger oxymoron in my life
>>
>>58274546
>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
>>
>>58274541
>doesnt give any reasons
>>
>>58273248
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.
>>
>>58274546
I've never seen a bigger moron in my life
>>
why functional programming is shit? especially f#
>>
>>58274499
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)
>>
>>58275153
because your feeble brain can't comprehend it sorry not sorry ;)
>>
>>58275153
F# is unironically one of the best programming langugaes in existence.
>>
>>58275153
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?
>>
>>58275153
Shoo, Pajeet
>>
>>58275295
name literally one bad thing about F#
>>
>>58275348
Shitty type system, shitty module system, .NET baggage, etc.

F*, on the other hand...
>>
>>58275406
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.
>>
>>58275434
Like Windows GUI programs?
>>
>>58276037
That's one example yes.
>>
>>58275348
>
[1; 2; 3] : int list

>
[|1; 2; 3|] : int []
>>
>>58277065
And what's the problem exactly?
>>
>>58277097
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
>>
>>58277129
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.
>>
>>58277174
it's retarded that they decided to mix C# and ML syntax like that. it's like list<int> vs. int list.
>>
>>58277558
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.
>>
>>58277616
at most*
>>
>>58277616
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
>>
>>58277498
Just what I need. Link?
>>
>>58277675
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).
>>
>>58277709
https://www.amazon.com/Type-Theory-Formal-Proof-Introduction/dp/110703650X

also on libgen if you are a pirate.
>>
What is the smallest FP language?
>>
>>58277752
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.
>>
>>58277786
Unfortunately, despite being a Haskell programmer, I do not make $300k :(
>>
>>58277797
Iota or Jot.
>>
>>58277866
>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 /g/ 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.
>>
>>58277952
Fortunately for you, the pirate edition is free.
>>
>>58278071
Yeeeeeaaahhbuddy. λΠ, here we come.
>>
File: 1479321954293.png (7KB, 377x330px) Image search: [Google]
1479321954293.png
7KB, 377x330px
>>58277972
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?
>>
>>58278241
LazyK?
>>
>>58278241
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:
http://www.kevinalbrecht.com/code/joy-mirror/j02maf.html
http://tunes.org/~iepos/joy.html
>>
>>58278394
>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?
>>
>>58278700
in haskell, that is
>>
>>58277994
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.
>>
>>58278700
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)
>>
>>58281964
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
>>
>>58282059
yeah, good catch.
I've never seen ?? in haskell, i'll have to investigate. Thanks bud.
>>
>>58282095
(??) is lens-style, it's just an operator version of flip
>>
What's an easy and useful fuctional language?
Something like python but functional
>>
>>58282934
Erlang
>Something like python
Haskell and F#
>>
>>58281954
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?
>>
>>58284240
Depends. What are the specifics of your problem?
>>
>>58284241
https://en.wikipedia.org/wiki/Autism
>>
>>58284299
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.
>>
>>58284241
Generally the use of principles derived from (typed or untyped) lambda calculus -

Higher order functions, type systems, pattern matching
>>
>>58284325
Sounds like something you would use a mapM over the state monad for, if your state really needs to be arbitrary
>>
>>58284325
[TypeB] -> StateT [TypeA] TypeC
?
>>
>>58268853
Functional programming is a meme.
>>
>>58285642
a good meme
>>
>>58284241
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).
>>
I have a problem understanding beta reduction. Why does (lambda v.z)z reduce to z?
>>
>>58288437
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')
>>
>>58288437
>>58288499
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
>>
>>58288521
It's 'k z', muthafuckaaaa
>>
>>58268853
When is someone going to put functional language features into a language that doesn't have syntax like ass?
>>
>>58288499
>>58288521
Got it. Thanks! I guess my confusion arose from the fact that the return value and the parameter are the same...
>>
>>58288499
>>58288521
What would this look like in Haskell?
>>
>>58288747
\v -> z

\_ -> z

const z

pure z
>>
>>58288561
They tried that with Haskell, but they accidentally the whole eager.
>>
>>58288761
>pure z
>fire up GHCi, it works
wat

I assume this has something to do with (->)'s instance of Applicative?
>>
>>58288781
Yes. pure is K and <*> is S!
>>
>>58288761
>>58288781
Just looked it up. My face when the definitions for 'pure' and '<*>' for functions are literally 's' and 'k'.

What does it mean?

>>58288876
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
>>
>>58289087
That would be the same thing you dumbass.
>>
>>58288876
WWWWHHHHYYYYYYY
>>
>>58289157
not all sigmas are existentials
>>
>>58289178
Σ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.
>>
>>58289166
turn on MonadComprehensions and write all your lambdas like this [ | x <- id ]


>>58289230
would you call (n, (some) Vect n Int)
an existential?
>>
>>58289254
I'd call it a pair which inhabits a sigma type (existential).

I guess, strictly speaking, sigma types are only existentials if the second type is a proposition, but under Curry-Howard that's always the case anyways.
>>
>>58289277
I always thought of existentials as the Haskell kind, but I guess Haskell's are limited.

That said an exists quantifier would be nice.
>>
>haskell
>no higher order unification
>no impredictive types
>no partial type applications
>>
>>58274541
i mean haskal
>>
>>58291297
Who needs that shit when you've got zygohistomorphic prepromorphisms?
>>
>>58291516
zygohistomorphic prepomorphisms are useless
>>
>>58291702
What the hell else are you going to do when you really need both semi-mutual recursion and history and to repeatedly apply a natural transformation as you get deeper into the functor?
>>
>>58291900
it's not even type level
>>
>>58291913
Neither is your mother.
>>
Could anyone show how abs(x) function would look in point-free notation?
>>
>>58292210
abs
>>
>>58292235
But there's no guards
>>
>>58292265
You wanted the point-free form. That's point-free.
>>
>>58292210
For what type?
>>
>>58292025
Kek
>>
>>58292292
But it won't work the same way as point-full function.
>>
>>58292337
Let be integers
>>
>>58274499
Haskell.
>>
>>58292406
abs = join (liftM2 if' (< 0) negate)

http://pointfree.io/
>>
>>58268853
Is Ocaml or Rust functional?
>>
>>58292440
>join
>liftM2
>negative
What kind of magic is this?
>>
>>58292406
>>58292440
presumably if' being ifThenElse as a function

also
>join for (->) a puts the same input on both sides
will have to remember that one

>>58292472
OCaml is functional, Rust is more functional than similar languages
>>
>>58292393
Yes, it will:

map abs [(-1),(-2)..(-5)]
=> [1,2,3,4,5]
>>
>>58292440
>>58292487
This was the one I had lined up:

ap (ifff =<< (>= 0)) negate
>>
>>58292487

pure x = \_ -> x
fmap f g = f . g
f <*> g = \x -> f x (g x)
join f = \x -> f x x
f >>= g = \x -> g (f x)
>>
>>58292611
So many combinators.
>>
>>58292488
join (+) 5
10


Fuck me, that's glorious.
>>
Anybody using autocomplete for Haskell in Vim? What do you use?
>>
>>58292647
The reason I'm so happy is that I was literally just looking for this exact function:
join ((:) . head) [1..5]
[1,1,2,3,4,5]

I wanted a point-free form of 'dup' for implementing a concatenative language.
>>
>>58292687
i've used
zip <*> tail

before
>>
>>58292705
This kind of thing just makes my head explode.
>>
>>58292757
(f <*> g) x = f x (g x)
(zip <*> tail) xs = zip xs (tail xs)
>>
>>58292770
I could use that pattern a lot.

(zip <*> map values) keys = zip keys (map values keys)
>>
>mfw i see a haskell nerd working on production code
Seriously, just kill yourselves before you actually get a real job.
>>
>>58292849
'values' should be 'value', but you get the idea.
>>
>>58292757
zipWith const >=> (==)


WEW
>>
>>58292472
Rust incorporates functional elements, but it's still predominantly imperative. Especially, it does not do tail recursion optimization which is pretty important for functional programming.
>>
>>58292896
(f >=> g) a b = g (f a b) b
>>
>>58292611
Shouldn't that be:
f >>= g = \x -> g (f x) x
… on your last line?
(Also, being a newfag, how do I format code as such?)
>>
>>58293296
yes, probably

<code></code> but with square brackets
>>
>>58293296
>>58293369
the really interesting thing about the (->) instance is that >>= and <*> are almost exactly the same thing, except
1) arguments are flipped
2) (second of >>=, or first of <*>) argument has its arguments flipped
>>
>>58293418
Also, it's basically the Reader monad
>>
>>58293418
What would be the Applicative equivalent of >=>?
>>
>>58293463
it is the reader monad

>>58293486
dunno but here's the literal one

>>58293122
>>
>>58288893
>What does it mean?
The way I interpret the results is that it's a natural conclusion of Applicative being designed to let you lift arbitrary computational structure into some Applicative, whether that applicative is SK (i.e. (->) instance) or something else.

Since SK is the (pretty much) most minimal set of combinators that let you embed arbitrary computationhal structure, it's a good fit for the basis of Applicative.

‘s’ in SK is basically the primitive that lets you thread your environment (third parameter) into both halves of a computation, and ‘k’ in SK is basically the primitive that lets you ignore the environment for a part of the computation.

Together, you can thread the environment (parameter) into arbitrary places, which is exactly the power that lets you transform arbitrary functions (i.e. lambda terms with the parameters appearing in arbitrary places) into SK.
>>
>>58293486
liftA2 (.) would be the fully flipped version of (>=>)
>>
i ironically use rust
>>
>>58294024
nice
but rust doesn't belong here
>>
>>58294210
it supports functional programming
>>
>>58291297
>no higher order unification
>no partial type applications
Because it doesn't have lambdas at the type level in the first place. I imagine that would fuck up type classes.
>>
>>58292210
if' c x y = if c then x else y
abs = flip (flip if' negate) id . (< 0)) <*> id
>>
>>58271430
>Shouldn't the first point be just "make good software"? Why rely on writing code that eventually crashes?

naive sjw detected.

go create some "problematic language" tickets in readme.txt files
Thread posts: 150
Thread images: 7


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.