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:
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:
Some cool F# things:
(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:
code:
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:
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. Cool functional stuff written by goons:
--- 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 |
|
# ? Apr 30, 2015 07:49 |
|
|
# ? May 3, 2024 02:20 |
|
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.
|
# ? Apr 30, 2015 08:19 |
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. Please do, I'd love to add it to the OP.
|
|
# ? Apr 30, 2015 08:39 |
|
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.
|
# ? Apr 30, 2015 12:24 |
|
Starring this thread
|
# ? Apr 30, 2015 13:26 |
|
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.
|
# ? Apr 30, 2015 13:47 |
|
Clojure is really cool. You can put that in the OP if you want.
|
# ? Apr 30, 2015 16:00 |
|
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?
|
# ? Apr 30, 2015 16:21 |
|
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.
|
# ? Apr 30, 2015 16:27 |
|
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.
|
# ? Apr 30, 2015 16:46 |
|
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.
|
# ? Apr 30, 2015 17:06 |
|
What's a monad?
|
# ? Apr 30, 2015 18:23 |
|
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. 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:
Some cool F# things:
|
# ? Apr 30, 2015 18:51 |
|
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.
|
# ? Apr 30, 2015 18:52 |
|
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.
|
# ? Apr 30, 2015 18:53 |
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:
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.
|
|
# ? Apr 30, 2015 20:13 |
|
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 would definitely like to see more stuff like this!
|
# ? Apr 30, 2015 20:15 |
Updated the OP with cool stuff from this page. Thanks HappyHippo, giogadi, and Arcsech!
|
|
# ? Apr 30, 2015 20:24 |
|
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#.
|
# ? May 1, 2015 01:01 |
|
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.
|
# ? May 1, 2015 01:06 |
|
Might be worth throwing Racket under the lisps. Its a scheme dialect with a really cool learning UI and some great libs.
|
# ? May 1, 2015 05:43 |
|
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.
|
# ? May 1, 2015 06:22 |
|
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:
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.
|
# ? May 1, 2015 06:49 |
|
Glad to see this thread was created. Not glad to see impure, functional-ish languages included.
|
# ? May 1, 2015 09:18 |
|
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)
|
# ? May 1, 2015 10:59 |
|
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.
|
# ? May 1, 2015 12:41 |
|
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.
|
# ? May 1, 2015 12:52 |
|
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.
|
# ? May 1, 2015 17:05 |
|
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.
|
# ? May 1, 2015 17:37 |
|
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. 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.
|
# ? May 1, 2015 17:59 |
|
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?
|
# ? May 1, 2015 19:21 |
|
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?
|
# ? May 1, 2015 19:33 |
|
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
|
# ? May 1, 2015 20:09 |
|
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.
|
# ? May 1, 2015 22:29 |
|
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. Thank you for this. "You are wrong" posts are quite annoying when the person posting provides no reasoning, and CoC seems to love them.
|
# ? May 1, 2015 22:37 |
|
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.
|
# ? May 2, 2015 00:29 |
|
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?
|
# ? May 2, 2015 02:10 |
|
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?
|
# ? May 2, 2015 02:13 |
|
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.
|
# ? May 2, 2015 02:17 |
|
|
# ? May 3, 2024 02:20 |
|
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:
code:
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:
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 |
# ? May 3, 2015 16:07 |