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 |
|
|
# ¿ Apr 28, 2024 02:54 |
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 |
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 |
Updated the OP with cool stuff from this page. Thanks HappyHippo, giogadi, and Arcsech!
|
|
# ¿ Apr 30, 2015 20:24 |
Thanks, this is great. I added it to the OP. Also, drat, I really need to get into Elm more!
|
|
# ¿ May 3, 2015 20:59 |
Real World Haskell has some exercises as well.
|
|
# ¿ May 7, 2015 02:30 |
So apparently facebook is re-writing their content classification code in Haskell. Pretty interesting to see the language in use by one of the bigger tech companies around!
|
|
# ¿ May 14, 2015 05:27 |
Speaking of interesting math-y books, can anyone recommend a good book on category theory? I've been meaning to read up on it more.
|
|
# ¿ May 15, 2015 05:48 |
I'm not really interested in reading it for functional programming purposes--I just thought this thread would be a good place to ask since there is an overlap between the two subjects.
|
|
# ¿ May 15, 2015 13:51 |
I've been thinking about implementing Conway's Game of Life in Haskell. Does anyone have any recommendations for graphics libraries to use?
|
|
# ¿ May 30, 2015 00:30 |
Testiclops posted:Gloss is pretty nice I'm looking over it now and it seems pretty promising. Thanks!
|
|
# ¿ May 30, 2015 01:54 |
Haskell question: I want to upgrade ghc to 7.10, because a lot of the new stuff looks pretty sweet. What's the best way to do this without breaking everything I've installed with cabal? If that's not possible, what's the best way to fix everything I break? My Haskell installation is the one provided through https://www.haskell.org/platform/, and it's version 2013.2.0.0 if that matters.
|
|
# ¿ Jun 14, 2015 21:15 |
gonadic io posted:(.).(.) is real, and occasionally useful. I had never seen that operator before, but after sitting down with a pen and paper and figuring out what it does, that's really useful! I'll have to remember that.
|
|
# ¿ Jun 25, 2015 22:35 |
gonadic io posted:I'm trying to cut back a little on how pointfree my code is. Is this mostly out of readability concerns? Another question I've been pondering: does pointfree style affect performance through memoization? This article seems to indicate that it does, but I'm not sure I fully understand what it's saying.
|
|
# ¿ Jun 26, 2015 08:44 |
fart simpson posted:You're the problem. Guilty as charged.
|
|
# ¿ Jun 26, 2015 08:48 |
drat, those are some nice error messages.
|
|
# ¿ Nov 19, 2015 18:17 |
Is there an up-to-date tutorial on regexes in Haskell? It seems like every tutorial is out-of-date, and the documentation on Hackage is fairly lacking.
|
|
# ¿ Nov 19, 2015 22:55 |
Barnyard Protein posted:Your Elm posts always make me want to do more with Elm. I've done a little, but not enough. Same here. I've never really had any desire to do web stuff, but fart simpson's elm posts make it look so slick.
|
|
# ¿ Dec 17, 2015 18:49 |
Munkeymon posted:Yep, that's it. I thought I was being all clever there by avoiding implicitly creating a bunch of basically-useless single-element lists. I think that would get compiled away anyways, but if not I think this would work: code:
VikingofRock fucked around with this message at 23:52 on Dec 30, 2015 |
|
# ¿ Dec 30, 2015 23:44 |
Munkeymon posted:Am I just super bad at this or am I finding the compiler bugs? Both? Not sure what's up with that error message, but I think your problem is that in the last line, func value (partial left) is a b, and (partial right) is also a b, so you can't apply `func` to them.
|
|
# ¿ Dec 31, 2015 20:59 |
Charlie Mopps posted:For people wanting to learn Haskell i can recommend the book Haskell Programming From First Principles. Its quite expensive ($60) and still in early access, but its up to ~1000 pages already. Its pretty much the only book thats up to date and explains Haskell from the basis. Its goal is to explain Haskell in such a way that you can learn and understand any Haskell library thats out there, instead of teaching you say web dev with a framework or whatever. A sample can be found here. Added this to the OP. Thanks for the recommendation!
|
|
# ¿ Jan 2, 2016 05:03 |
Hughmoris posted:I dabble with programming as a hobby, mainly in Perl and Python. Trying my hand at functional programming with Haskell. I've read in multiple places that functional programming makes concurrency/parallel programming much easier, and that functional programming scales really well. Most fundamentally, lack of mutable state means that you don't have to worry about data races (except in things like file access), and it allows for *very* lightweight threads. Also, the more expressive syntax lets you experiment with parallelization more simply (e.g. replacing calls to `map` with calls to `parmap`).
|
|
# ¿ Mar 25, 2016 00:09 |
Lists in Haskell are singly-linked lists, which can be pretty inefficient if you have to traverse them more than once. If you have a lot of lists (or Strings, which are really just lists of chars), you might want to try replacing them with a more efficient data structure (e.g. Vectors or ByteStrings). Other than that, it's hard to diagnose efficiency problems without seeing any code.
|
|
# ¿ Apr 11, 2016 02:55 |
xtal posted:That's good to know, I was under the wrong impression because the first step in every tutorial is Template Haskell. Which tutorials were you using? I've been meaning to try out lenses.
|
|
# ¿ Sep 10, 2016 04:36 |
New version of Elm is out.
|
|
# ¿ Nov 15, 2016 07:44 |
Shinku ABOOKEN posted:but... but... but... I liked my primes Me too! I also liked the [1..5] syntax and back ticks. I suppose I can see the argument for removing all three though.
|
|
# ¿ Nov 15, 2016 20:27 |
xtal posted:What is the best language to use on a Raspberry Pi? Haskell runs fine but lens has been compiling for over a day How is ghc cross-compilation? Could you compile it on a different machine and then just move the binaries over?
|
|
# ¿ Jan 8, 2017 19:03 |
|
|
# ¿ Apr 28, 2024 02:54 |
Smoke_Max posted:I just read about the FTP controversy in Haskell and it seemed to me to have been a solid move, but one detail is still nagging me. What's the rationale behind (,)'s Foldable instance? I understand how it works, but why is it even defined? Is it a case of "because we can"? I honestly cannot see much use for it besides creating confusion (length (1, 2) = 1 says hi). Personally, by making well typed code do the wrong thing, all it does for me is make me less confident in the type checker. As far as I know, (,) is Foldable pretty much exactly "because we can". It, and the Foldable instance for Either, are pretty controversial in the Haskell community for exactly the reasons you posted.
|
|
# ¿ Mar 22, 2017 07:10 |