Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
VikingofRock
Aug 24, 2008




This is a thread for discussing and asking questions about functional programming and functional languages.

What is functional programming?

To quote the Haskell wiki:

quote:

In functional programming, programs are executed by evaluating expressions, in contrast with imperative programming where programs are composed of statements which change global state when executed. Functional programming typically avoids using mutable state.

Additionally, functions are often treated as just-another-value, meaning that functions-of-functions (and functions-of-functions-of-functions, etc) are quite common. This allows you to write very, very general code, to a degree which is uncommon in imperative languages.

What are some functional languages?
Some of the more popular functional languages are:

Additionally, Elm (http://elm-lang.org/) is an up-and-coming functional language which seems very cool and is probably worth a look.

General Functional Programming Resources

Language-Specific Information and Resources
There are excellent threads in this forum for LISP and its dialects, Erlang, and Scala. I won't bother to repeat the information in those threads here.


Haskell is a functional language with a focus on purity--that is, lack of side effects in its functions. This makes Haskell code very easy to reason about and to test, because calling a function with the same arguments will always yield the same result. Haskell is very strongly typed, and you can often tell what a function does just by looking at its type signature. Haskell has a heavy influence from category theory, and as a result Haskell code can be very elegant, but also very confusing for beginners. Fear not though, soon you will understand applicative functors, monoids and monads and will be happily writing beautiful, concise code (at least in theory).

Haskell books:
  • Learn You A Haskell: This book is written in a somewhat snarky tone, which turns some people off, but it's a pretty good introduction to Haskell. In particular, I like the sections where the author lists a series of useful functions for manipulating whatever he has just introduced.
  • Real World Haskell: This book is somewhat more serious and practical than LYAH, and covers a few more useful libraries. It is also an excellent read.
  • Haskell Programming From First Principles: An up-to-date introduction to Haskell, still in early access. Recommended by Charlie Mopps

Other resources:


(contributed by Arcsech)
F# is a functional language descended from the SML/OCaml family and based on Microsoft's .NET platform. This allows easy interop with C# and other .NET applications & libraries, but can lead to some awkwardness as much of the .NET standard library wasn't designed with functional programming in mind. In addition to .NET interop, you get some really awesome languages features such as 1) type providers, which use data files, database connections, or even other programming languages to inform your type system and Visual Studio's autocomplete, 2) Units of Measure, which let you tag primitive types with additional type data to reflect their units and have the compiler do dimensional analysis at compile time, and 3) computation expressions which let you write code in a way similar to Haskell's do-notation, but with more flexibility (and in my opinion, a bit more readable).

F# resources:
  • Try F#: Try F# in your browser!
  • F# For Fun and Profit: A website with a thorough "Why F#?" series, well worth looking through to get a sense of what's available in F#.
  • F# Foundation: A pretty good directory of F# resources.

Some cool F# things:
  • Websharper: Write webapps in 100% F#. F# on the server, HTML combinators to generate HTML, and an F#->JS transpiler (although it can work with normal HTML templates if you so desire)
  • Visual F# Power Tools: While Visual Studio supports F# by default, it's kind of half-assed. This extension makes it a bit more fully-assed and generally improves quality of life quite a bit.


(contributed by fart simpson)

Elm is a pure, strictly evaluated functional language descended from Haskell, with bits and pieces taken from other ML-family languages like F#. It's unique in that it's not a general purpose programming language like the others mentioned in this thread, but rather it's a browser-based, Functional Reactive Programming UI language. There are other FRP implementations, but Elm is the only language I know of dedicated to the concept rather than being a library.

What is Functional Reactive Programming?

Functional Reactive Programming, or FRP, is a relatively new approach to building UI and animation (among other things). It abstracts events and callbacks into first-class, time-varying values that Elm calls Signals. In Elm, you define your program as pure functions that transform inputs to outputs, and then map input Signals over these functions. As a basic "Hello World" type example, the following function takes an (Int, Int) tuple and returns an Element (which just means "something that can be drawn to the screen"):
code:
render : (Int, Int) -> Element
render (x, y) = Graphics.Element.show (x, y)
We can then map the current position of the mouse onto this render function without our render function to produce a Signal Element, which means a time-varying object that is drawn to screen:
code:
-- Mouse.position is a built-in input Signal in Elm of type "Signal (Int, Int)"
main : Signal Element
main = Signal.map render Mouse.position
This program will always show the current mouse position, because as the mouse is moved, the input Signal "Mouse.position" is automatically updated, and the values flow through each function, updating their values in turn.

Every input to your program is handled in the same way, and complex behavior is defined as compositions of these basic building blocks. This paradigm naturally leads to a specific architectural pattern we call The Elm Architecture that ends up looking like this:
code:
-- MODEL (Where you define the data structures that will be used to represent your program's state)

type alias Model = { ... }


-- UPDATE (Where you define the possible actions, map input Signals to these actions, 
--         and write one or more state machines that define how your program state updates on each action)

type Action = Reset | ...

update : Action -> Model -> Model
update action model =
  case action of
    Reset -> ...
    ...


-- VIEW (Where you define how to visually display your program model. 
--       In Elm, you don't define visual transitions as such, rather 
--       you define what the entire output should look like given any possible model)

-- view can also be Model -> Element
view : Model -> Html 
view =
  ...
These sections can be composed together from smaller subsections that live in different modules, with each piece being pure and stateless. At the very end, you tie everything together by mapping input Signals onto the update function. It leads to a very clean style of code that is easy to reason about and test, and the guarantees of the language allow some cool things like the time traveling debugger and the near-total elimination of runtime exceptions.

There's a lot of other stuff to like about Elm. It has a helpful, growing community, an emphasis on making functional programming concepts approachable and easy to learn, a package manager that enforces semantic versioning, a virtual-dom rendering backend that's really fast, real-time modification of currently running code, a simple and powerful FFI, and more.

The best places to learn more are the official website and the mailing list.

:siren:Cool functional stuff written by goons::siren:

---

I don't know enough about the other functional languages to write an introduction for them, but if anyone posts some information, resources, example code, etc for other languages I will add it to this OP.

VikingofRock fucked around with this message at 05:03 on Jan 2, 2016

Adbot
ADBOT LOVES YOU

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

For anyone looking to learn functional programming I wouldn't hesitate to consider Elm. It's very similar to a strictly evaluated, simplified, streamlined Haskell. Unlike many of the other FP languages, it retains the complete purity of Haskell, but it doesn't get bogged down in category theory. And it's a compile-to-js UI language so it's easy to get started (you don't even need to download anything for a while if you don't want to) and you get instant visual feedback. It also has first class support for its own unique take on Functional Reactive Programming, which is a very interesting take on managing interactions and state, sort of like an abstraction on top of callbacks.

I can write a lot more about Elm if anyone's interested.

VikingofRock
Aug 24, 2008




fart simpson posted:

For anyone looking to learn functional programming I wouldn't hesitate to consider Elm. It's very similar to a strictly evaluated, simplified, streamlined Haskell. Unlike many of the other FP languages, it retains the complete purity of Haskell, but it doesn't get bogged down in category theory. And it's a compile-to-js UI language so it's easy to get started (you don't even need to download anything for a while if you don't want to) and you get instant visual feedback. It also has first class support for its own unique take on Functional Reactive Programming, which is a very interesting take on managing interactions and state, sort of like an abstraction on top of callbacks.

I can write a lot more about Elm if anyone's interested.

Please do, I'd love to add it to the OP.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?
Two other resources I found useful for Haskell (once you grasp the basics) are the Typeclassopedia and What I Wish I Knew When Learning Haskell.

MononcQc
May 29, 2007

Starring this thread :getin:

giogadi
Oct 27, 2009

Let's keep the functional dream alive people. A lot of the curious people that come in will be wondering what you can actually *do* with functional programming, so have y'all built anything interesting to share?

I farted around with Elm a bit last year and built a clone of Tetris Attack. You can play it at this link:

http://cs.unc.edu/~luis/tetris-attack/

Press spacebar to begin. Move the cursor with the keyboard arrows and swap the blocks under the cursor with the spacebar. Try to line up at least 3 of the same color in a row to make them vanish, and see how long you survive. Here's the source.

Using Elm for this was just so drat pleasant. I actually had an experimental version of this working with real-time online multiplayer, but Elm's API changed a whole bunch so that code's now broken. When I have time I'd love to take a shot at Elm's WebGL API and see how feasible 3D game dev is in Elm's FRP+MVC programming model.

leftist heap
Feb 28, 2013

Fun Shoe
Clojure is really cool. You can put that in the OP if you want.

Pollyanna
Mar 5, 2005

Milk's on them.


Clojure is pretty drat cool. I'm learning it for the express purpose of wrapping my head around functional programming in general. I wanted to go through Functional Programming in Scala, but I felt like I didn't have the FP background for it. What are some good language-agnostic resources for the basics and theory behind FP?

giogadi
Oct 27, 2009

Pollyanna posted:

Clojure is pretty drat cool. I'm learning it for the express purpose of wrapping my head around functional programming in general. I wanted to go through Functional Programming in Scala, but I felt like I didn't have the FP background for it. What are some good language-agnostic resources for the basics and theory behind FP?

I personally haven't seen any language-agnostic tutorials on FP, but I think that might be because each FP language takes such a different approach to FP. I didn't know anything about FP before jumping into Learn You a Haskell, and I found it to be a super enlightening yet accessible introduction to general FP concepts with Haskell as the driving language.

leftist heap
Feb 28, 2013

Fun Shoe

Pollyanna posted:

Clojure is pretty drat cool. I'm learning it for the express purpose of wrapping my head around functional programming in general. I wanted to go through Functional Programming in Scala, but I felt like I didn't have the FP background for it. What are some good language-agnostic resources for the basics and theory behind FP?

IMO Clojure is "more functional" than Scale anyway. You did the right thing.

crazypenguin
Mar 9, 2005
nothing witty here, move along

By the way, I highly recommend this to anyone who wants to figure out what haskellers are talking about with category theory crap, without the annoying wankery. But do not read this sort of thing before you go learn a bit of haskell. That's like trying to understand dependency injection without understanding objects. That's just a route to thinking it's all incomprehensible.

Unfortunately, both versions (the wiki and the original "issue 13" link at the top) have trade-offs.

I *think* the best way through it these days is to read the wiki one, but only the intro, functor, applicative, monad, and monoid sections. Skip the rest.

Just that much, however, is exceptionally useful to know, even if you don't write haskell. These things are really generic ways of composing little pieces into bigger pieces, and it's extremely helpful when designing things to know about them.

Bloody
Mar 3, 2013

What's a monad?

Arcsech
Aug 5, 2008

VikingofRock posted:

I don't know enough about the other functional languages to write an introduction for them, but if anyone posts some information, resources, example code, etc for other languages I will add it to this OP.
I wrote a thing for F#:


F# is a functional language descended from the SML/OCaml family and based on Microsoft's .NET platform. This allows easy interop with C# and other .NET applications & libraries, but can lead to some awkwardness as much of the .NET standard library wasn't designed with functional programming in mind. In addition to .NET interop, you get some really awesome languages features such as 1) type providers, which use data files, database connections, or even other programming languages to inform your type system and Visual Studio's autocomplete, 2) Units of Measure, which let you tag primitive types with additional type data to reflect their units and have the compiler do dimensional analysis at compile time, and 3) computation expressions which let you write code in a way similar to Haskell's do-notation, but with more flexibility (and in my opinion, a bit more readable).

F# resources:
  • Try F#: Try F# in your browser!
  • F# For Fun and Profit: A website with a thorough "Why F#?" series, well worth looking through to get a sense of what's available in F#.
  • F# Foundation: A pretty good directory of F# resources.

Some cool F# things:
  • Websharper: Write webapps in 100% F#. F# on the server, HTML combinators to generate HTML, and an F#->JS transpiler (although it can work with normal HTML templates if you so desire)
  • Visual F# Power Tools: While Visual Studio supports F# by default, it's kind of half-assed. This extension makes it a bit more fully-assed and generally improves quality of life quite a bit.

Xerophyte
Mar 17, 2008

This space intentionally left blank

Bloody posted:

What's a monad?

It's way of introducing state and contextual data in a functional program without breaking referential transparency, i.e. the compiler can still be safe in knowing that "f(2)" will do the same thing everywhere. Having them lets Haskell be a pure language and still have IO functions that aren't passing around a bunch of buffers everywhere, for instance.

It's also a monoid in the category of endofunctors, of course.

HappyHippo
Nov 19, 2003
Do you have an Air Miles Card?

Pollyanna posted:

Clojure is pretty drat cool. I'm learning it for the express purpose of wrapping my head around functional programming in general. I wanted to go through Functional Programming in Scala, but I felt like I didn't have the FP background for it. What are some good language-agnostic resources for the basics and theory behind FP?

It's not really language-agnostic, but Structure and Interpretation of Computer Programs is a classic textbook which uses Scheme (a lisp). When I had trouble learning Haskell I switched over to that book for a bit and then went back, it was a lot easier after that.

VikingofRock
Aug 24, 2008




Bloody posted:

What's a monad?

I'll take a stab at this. Despite talking primarily about Haskell's monads I'm going to use a more C++-like syntax because I figure that's what non-functional programmers are used to.

Monads (like many things) are defined by how they act when you apply certain functions to them. In Haskell, these functions are called "return" and ">>=". I'll talk about them in a second, but first I want to say that monads are incomplete types by themselves. This is just like how C++'s std::vector needs a template parameter (like <int>) to become a complete type. For our purposes we will say the monad has type Monad and its parameter has type T (so the C++ version would be a Monad<T>).

"return" is a function which takes something of some type T, and gives you a Monad<T>. The C++ vector equivalent would be a template function which takes a T and gives you a vector<T>.

">>=" is a function which is used to chain together operations. It takes a Monad<T> and a function from T to Monad<U>, and returns a Monad<U>.

There are a few rules on how these functions work together:
  • If you pass ">>=" the result of "return(x)" and a function "f", you must get back the same thing as you would passing "x" directly to "f".
  • If you pass ">>=" a Monad<T> and the function "return", you must get back the original Monad<T>.
  • ">>=" must be associative.

And that's it! Anything that fits that description is a monad. They sound pretty weird at first but if you play around with them a bit they'll become pretty clear, and you'll find yourself defining them in other languages because they are useful as hell.

CPColin
Sep 9, 2003

Big ol' smile.

giogadi posted:

Let's keep the functional dream alive people. A lot of the curious people that come in will be wondering what you can actually *do* with functional programming, so have y'all built anything interesting to share?

I farted around with Elm a bit last year and built a clone of Tetris Attack. You can play it at this link:

http://cs.unc.edu/~luis/tetris-attack/

I would definitely like to see more stuff like this!

VikingofRock
Aug 24, 2008




Updated the OP with cool stuff from this page. Thanks HappyHippo, giogadi, and Arcsech!

DimpledChad
May 14, 2002
Rigging elections since '87.
There are basically as many monad tutorials as there are people who have heard the word "monad," but one very detailed tutorial that helped it finally "click" for me is this one by Eric Lippert: http://ericlippert.com/2013/02/21/monads-part-one/. It's accessible to non-Haskell people, as all the code is in C#.

KaneTW
Dec 2, 2011

Yay! A FP thread.

http://www.seas.upenn.edu/~cis194/spring13/lectures.html is probably the best Haskell beginner resource (probably above books) (I can't really recommend LYAH, but RWH is good (but not as good as CIS194)).

https://github.com/bitemyapp/learnhaskell should be in the OP too.

duck monster
Dec 15, 2004

Might be worth throwing Racket under the lisps. Its a scheme dialect with a really cool learning UI and some great libs.

Arcsech
Aug 5, 2008

duck monster posted:

Might be worth throwing Racket under the lisps. Its a scheme dialect with a really cool learning UI and some great libs.

This is true. It's also the best way to get an SICP-compatible scheme (via #lang planet neil/sicp) right now.

brap
Aug 23, 2004

Grimey Drawer

Bloody posted:

What's a monad?

I'm sorry for being another person to talk about this.

A monad is when you put a wrapper around a value and make it so it's only possible to access that value with a callback. It's an extremely simple and general concept when you get to the heart of it. Ever use Nullable<T> in C#? Consider an example like this:

code:
Nullable<int> nullable1 = 4;
Nullable<int> nullable2 = null;

var result1 = nullable1 + 2; // Returns a nullable containing the value 6.
var result2 = nullable2 + 2; // Since this nullable contained a null, this operation returns null.
For result1 and result2, C# is automagically providing a function to the Nullable<int> which, given an int, returns that int plus 2. The nullable monad decides whether or not to make use of the function, depending on whether or not it has a value to provide to the function!

There's a zillion uses of this pattern that will make your programming life better. Look at all the LINQ stuff, Task<T>, Func<T>, etc. if you're in the .NET world.

Flat Daddy
Dec 3, 2014

by Nyc_Tattoo
Glad to see this thread was created. Not glad to see impure, functional-ish languages included.

RobertKerans
Aug 25, 2006

There is a heppy lend
Fur, fur aw-a-a-ay.
A few resources I've found useful:

Dunno how useful this is in reality, but this talk about category theory is interesting and very clear, and clarified much of the thinking behind Haskell for me (I'd struggle with a lot of high school level maths tbh, and this was still great): https://www.youtube.com/watch?v=o6L6XeNdd_k

I found EdEx's FP101 course a very good introduction: the lecturer is Erik Meijer, and it's up in various places in various forms (archived on the EdEx site, github: https://github.com/fptudelft/FP101x-Content, original version for MDSN at http://lambda-the-ultimate.org/node/3642). It's haskell, but the EdEx course had a series of extra bits that translated each lecture into other languages; it's supposed to cover FP principles rather than primarily being a Haskell intro.

Personally, I like FP because it seems more intuitively easy to grasp than OOP, and I came to it via JS: [Clojure guy] Michael Fogus' book Functional Javascript is loving great if you're into that sort of thing (Javascript Allongé is pretty good as well re applying function ideas).

Also I've found Elixir amazing, and I could go on about it for ages, nicest language I've used [in my limited experience], with a fairly amazing web framework (Phoenix) and a really helpful community (it's quite nice having the language creator answering questions when you need help)

giogadi
Oct 27, 2009

Arcsech posted:

This is true. It's also the best way to get an SICP-compatible scheme (via #lang planet neil/sicp) right now.

On a similar note, the incredible book How to Design Programs is also based on Racket. Some people find HtDP to be a more modern and approachable version of SICP.

Also: slightly off-topic, but I just found out the SICP people wrote a book about classical mechanics (SICM) that uses Scheme to teach physics. It's fun because the various laws and formulas are all taught with the intent of being coded up in Scheme, so they strive for zero ambiguity.

MononcQc
May 29, 2007

In the SICP vein (I really enjoyed the classic one) The Lisp-Flavored-Erlang people are currently rewriting the code sample and exercises to work with their variant of the language: http://lfe.gitbooks.io/sicp/

I've put a lot of Erlang resources in the Erlang thread, but I'm ready to rewrite them shorter for this thread if anyone would like that.

9-Volt Assault
Jan 27, 2007

Beter twee tetten in de hand dan tien op de vlucht.

giogadi posted:

On a similar note, the incredible book How to Design Programs is also based on Racket. Some people find HtDP to be a more modern and approachable version of SICP.
The MOOC Introduction to Systematic Program Design is based on this book.

brap
Aug 23, 2004

Grimey Drawer
I spent a lot of time with How to Design Programs in supplement to my regular college programming classes and it was incredibly helpful. It's what I recommend to everyone starting. It really strips away the stuff beginners should not be worrying about and helps beginners build good habits.

I haven't worked with the scheme book people mention often in the same breath but I'm sure it's good too.

Arcsech
Aug 5, 2008

fleshweasel posted:

I spent a lot of time with How to Design Programs in supplement to my regular college programming classes and it was incredibly helpful. It's what I recommend to everyone starting. It really strips away the stuff beginners should not be worrying about and helps beginners build good habits.

I haven't worked with the scheme book people mention often in the same breath but I'm sure it's good too.

HtDP and SICP are often mentioned together but they're really quite different in terms of approach and scope. HtDP is, I would say, appropriate for someone learning programming for the first time - it uses Racket's ability to work with images easily to make it more engaging, it introduces file IO early to make it easier to solve practical problems, and it talks about topics like testing very early as well. SICP favors more difficult algorithmic challenges and is very theory-heavy - I'd recommend it for people who already have some experience with programming to help them go back and thoroughly understand the fundamental concepts of computer science. HtDP is a book about programming, which includes a bit of computer science, while SICP is a book about computer science, which includes a little bit of programming. They're both excellent books targeting slightly different audiences.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Flat Daddy posted:

Glad to see this thread was created. Not glad to see impure, functional-ish languages included.

As someone just starting to look into functional programing, which ones are the "impure" ones? Why are they only "functional-ish"? Why should I avoid them?

Sedro
Dec 31, 2008

Lumpy posted:

As someone just starting to look into functional programing, which ones are the "impure" ones? Why are they only "functional-ish"? Why should I avoid them?
If we only included pure languages this would be a haskell thread

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



Sedro posted:

If we only included pure languages this would be a haskell thread

Sorry but Haskel does output, which is a side effect soooo

Arcsech
Aug 5, 2008

Lumpy posted:

As someone just starting to look into functional programing, which ones are the "impure" ones? Why are they only "functional-ish"? Why should I avoid them?

"Pure" functional languages are those which force side effects (mostly you can think of this as any sort of input or output, although it's broader than that) to be reflected by the type system. Haskell is the only language that's even kinda-sorta mainstream that's purely functional, with some experimental languages like Elm, Agda, and Idris also falling in that category. A language being purely functional brings a lot of tradeoffs that can get pretty complex to explain.

Essentially, anything that's commonly described as functional will be good for learning functional programming, don't worry about it*. The only people who discount "impure" or "functional-ish" languages are basically fundamentalists.


*: However, learning a pure functional language as an intro to functional programming can be good for you as it doesn't allow you to intentionally or accidentally fall back into an imperative mode, instead forcing you to think functionally. However, as an introductory language, I'd recommend Elm - it is purely functional, but it's easy to do neat stuff with it since it runs in a browser and the author of the language puts a heavy emphasis on making it easier to learn. Elm's model of I/O is much easier to wrap your head around than Haskell's, in my opinion, which can be a major stumbling block in Haskell.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Arcsech posted:

"Pure" functional languages are those which force side effects (mostly you can think of this as any sort of input or output, although it's broader than that) to be reflected by the type system. Haskell is the only language that's even kinda-sorta mainstream that's purely functional, with some experimental languages like Elm, Agda, and Idris also falling in that category. A language being purely functional brings a lot of tradeoffs that can get pretty complex to explain.

Essentially, anything that's commonly described as functional will be good for learning functional programming, don't worry about it*. The only people who discount "impure" or "functional-ish" languages are basically fundamentalists.


*: However, learning a pure functional language as an intro to functional programming can be good for you as it doesn't allow you to intentionally or accidentally fall back into an imperative mode, instead forcing you to think functionally. However, as an introductory language, I'd recommend Elm - it is purely functional, but it's easy to do neat stuff with it since it runs in a browser and the author of the language puts a heavy emphasis on making it easier to learn. Elm's model of I/O is much easier to wrap your head around than Haskell's, in my opinion, which can be a major stumbling block in Haskell.

Thank you for this.

"You are wrong" posts are quite annoying when the person posting provides no reasoning, and CoC seems to love them.

Pollyanna
Mar 5, 2005

Milk's on them.


I've been meaning to get into Racket and Elixir more, too. Clojure's taking up most of my side-time, though. I'm considering making a basic lovely web app using Leiningen and Luminus, just to get used to the language. (It helps that Clojure can use Java packages.)

Elixir's next on my list.

Evil_Greven
Feb 20, 2007

Whadda I got to,
whadda I got to do
to wake ya up?

To shake ya up,
to break the structure up!?
Thanks for making this.

I'm also of the opinoin that that HtDP and Racket are great resources, especially for minds unpolluted by learning the drudgery that is typically taught and required.

By the way - now that there's a Functional Programming thread - is there even an OOP thread?

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

Evil_Greven posted:

By the way - now that there's a Functional Programming thread - is there even an OOP thread?

Pretty much all of them?

Evil_Greven
Feb 20, 2007

Whadda I got to,
whadda I got to do
to wake ya up?

To shake ya up,
to break the structure up!?
Ehh... I mean, something like this feels a bit out of place in that thread and doesn't really fit others well, either. Something more oriented towards design philosophy rather than some specific quirk of a language would be much better.

Those bits that are there are very scattered in their respective threads, too.

Adbot
ADBOT LOVES YOU

fart simpson
Jul 2, 2005

DEATH TO AMERICA
:xickos:

VikingofRock posted:

Please do, I'd love to add it to the OP.



Elm is a pure, strictly evaluated functional language descended from Haskell, with bits and pieces taken from other ML-family languages like F#. It's unique in that it's not a general purpose programming language like the others mentioned in this thread, but rather it's a browser-based, Functional Reactive Programming UI language. There are other FRP implementations, but Elm is the only language I know of dedicated to the concept rather than being a library.

What is Functional Reactive Programming?

Functional Reactive Programming, or FRP, is a relatively new approach to building UI and animation (among other things). It abstracts events and callbacks into first-class, time-varying values that Elm calls Signals. In Elm, you define your program as pure functions that transform inputs to outputs, and then map input Signals over these functions. As a basic "Hello World" type example, the following function takes an (Int, Int) tuple and returns an Element (which just means "something that can be drawn to the screen"):
code:
render : (Int, Int) -> Element
render (x, y) = Graphics.Element.show (x, y)
We can then map the current position of the mouse onto this render function without our render function to produce a Signal Element, which means a time-varying object that is drawn to screen:
code:
-- Mouse.position is a built-in input Signal in Elm of type "Signal (Int, Int)"
main : Signal Element
main = Signal.map render Mouse.position
This program will always show the current mouse position, because as the mouse is moved, the input Signal "Mouse.position" is automatically updated, and the values flow through each function, updating their values in turn.

Every input to your program is handled in the same way, and complex behavior is defined as compositions of these basic building blocks. This paradigm naturally leads to a specific architectural pattern we call The Elm Architecture that ends up looking like this:
code:
-- MODEL (Where you define the data structures that will be used to represent your program's state)

type alias Model = { ... }


-- UPDATE (Where you define the possible actions, map input Signals to these actions, 
--         and write one or more state machines that define how your program state updates on each action)

type Action = Reset | ...

update : Action -> Model -> Model
update action model =
  case action of
    Reset -> ...
    ...


-- VIEW (Where you define how to visually display your program model. 
--       In Elm, you don't define visual transitions as such, rather 
--       you define what the entire output should look like given any possible model)

-- view can also be Model -> Element
view : Model -> Html 
view =
  ...
These sections can be composed together from smaller subsections that live in different modules, with each piece being pure and stateless. At the very end, you tie everything together by mapping input Signals onto the update function. It leads to a very clean style of code that is easy to reason about and test, and the guarantees of the language allow some cool things like the time traveling debugger and the near-total elimination of runtime exceptions.

There's a lot of other stuff to like about Elm. It has a helpful, growing community, an emphasis on making functional programming concepts approachable and easy to learn, a package manager that enforces semantic versioning, a virtual-dom rendering backend that's really fast, real-time modification of currently running code, a simple and powerful FFI, and more.

The best places to learn more are the official website and the mailing list.

fart simpson fucked around with this message at 16:09 on May 3, 2015

  • Locked thread