4 ms·
Ask HN: As somewhat of an old timer, I have a couple of questions about FP, not really worthy of creating their own thread: 1. I learned a rule (in my Pascal t
by analog31 12y ago
Ask HN: As somewhat of an old timer, I have a couple of questions about FP, not really worthy of creating their own thread:
1. I learned a rule (in my Pascal textbook), "avoid globals." Is FP just an embodiment of that principle?
2. I do a lot of programming involving hardware, often homemade. Hardware has state, such as whether a lamp is turned on or off. Does this just mean FP is inappropriate, or are there techniques out there that accrue the benefits of FP in systems that necessarily have state?
- abraxasz 12y agoI'm not a specialist of FP, so please take what I say with a grain of salt. Before answering your questions, here's a preliminary point: 0) The problem is that FP is not really defined anywhere, or rather, everyone has its own definition. I was wondering about the definition of FP about a month ago, and discussed it with my roommate who's a phd student in programming languages. The conclusion of this 2-hour long conversation was: well FP is kind of a fuzzy concept. The concepts of FP languages, typed languages, static languages, etc.. all tend to conflate. Some people will consider that Scheme, Python, Ruby, Scala, Haskell, OCaml, R are all FP languages. Some will say that only Haskell and OCaml really are. Whatever.. Keeping that in mind, here some attempts at answers: 1) In a very loose sense, I guess that you can say that, and to make it possible, you need some constructs in your language, the minimum requirements being higher order functions, and maybe closures. I'd say that's the minimum, but again I'm no specialist. But then if you want to push this "no globals" moto to its logical extreme, you get to a point where you can't even use a "print" function, since it has some side effects. Haskell does, and solves the problem with monads. But to have monads you need a very particular type system, which leads to the question of wether you need a type system to go FP all the way? I don't know. Someone more knowledgeable should answer that 2) I don't know about hardware. But like I said, FP can deal with state. In fact, even hardcore FP as embodied by Haskell can deal with state. It's not trivial to wrap your head around the concept, but it's worth giving it a shot.
- noelwelsh 12y ago1. I think "avoid globals" is a reasonable way into FP, but FP itself involves a lot more than just avoiding coupling via global state. I would say the main components are - nested definitions; - lexical scoping; - first class functions; - static typing; and - a whole bunch of programming techniques (e.g. monads) that have grown around them. 2. You certainly can use FP ideas in low-level programming but you'll have difficulty finding a FP language that targets your hardware. There are a few small Scheme implementations that might work. Rust is viable if we're talking 32-bit CPUs, not 8-bit. (I imagine the Rust developers would not claim Rust is a functional language, but it has absorbed a great many ideas from functional languages. I prefer to talk about modern programming languages instead of functional programming languages. Rust, Haskell, and Scala are all modern and have many features in common. Only Haskell claims to be a pure FP language though.)
- vram22 12y ago>- static typing; I'm not an expert on the theory or design of programming languages, but isn't static typing orthogonal to FP?
- _Robbie 12y agoI will discuss Haskell, since I am not very familiar with other FP languages. In Haskell static typing is more useful than in other languages. This is because in Haskell the type signature of a function gives an upper bound of what the function does. In an impure language, a function may change any accessible mutable values and/or perform IO. These possible side effects mean that in an impure language the type of a function is a very weak indicator of what the function actually does.
- vram22 12y agoSo you're saying that static typing helps FP and hence is sort of a part of it? Thanks for the answer.
- noelwelsh 12y agoYes and no. A lot of the action in modern programming languages is in type systems. Static typing is synonymous with the dominant group of programming languages that subscribe to the "functional" label. There are other languages, such as Scheme, that are not statically typed that also subscribe to this label but they don't have the same mindshare at present. It's important to keep in mind that FP does not name some formally defined concept like the number 3 or the function cos. Rather it's a name given to a group of people and programming languages that share a common ancestry and world view. The definition is fuzzy and can be split many ways.
- vram22 12y agoInteresting, thanks. >It's important to keep in mind that FP does not name some formally defined concept like the number 3 or the function cos. Got it.
- mhaymo 12y ago1. More generally I'd say it's "minimise mutable state". 2. Almost all programs are necessarily stateful, but most of the code within them does not have to be. As such, all FP languages support working with state, e.g. Scala which is essentially just Java with more powerful types and easier creation of immutable values and lambdas, so you can program with state as easily as you can in Java. And Haskell, which separates stateful functions by type, so that the programmer and the compiler know which functions have state and which do not.
- Xurinos 12y ago1. I think we should challenge the definition of "global variable". In what context is the variable global? The traditional definition is that the context is the whole program. However, in imperative OOP [1] code, I find that people just learned to hide their global variables in class instances. The whole program cannot access them, right? but every method in the class can and does, and all those mutations and side effects can lead to the kind of spaghetti code we all hated when there was no OOP. It's just global variables with extra sugar. And we can make copies of them, so the code is maybe one step better. Imperative OOP often feels like one is pivoting behavior around the data. FP feels like the data is flowing from one transformation to the next. There is the "functional" part of it, too... where functions are highly composable. One creates new functions by combining old functions, and these may carry along with them important context (closures). 2. From what I've learned of Haskell -- and I'm a Haskell noob -- it lets you work with the side effecty real world. You can alter your hardware state. The key is that it specifically flags such side effects and encourages separation between more "pure" code and code that is "tainted" with the side effects. [1] OOP is an abstraction and compatible with pure FP. You can model your class instances such that they are immutable and get all the benefits of OOP. FP contrasts better with imperative programming.
- dllthomas 12y agoFP doesn't ignore state, FP reifies state and state transitions and allows you to talk about them more explicitly (and, if you have static types, use your type system to reason about them). 2 is no reason at all to avoid FP. That said, I agree with others here that there might not be an FP language that is a good fit, mostly because they have some trouble giving good guarantees about memory usage (particularly true of Haskell, with its lazy evaluation, but gets to be the case whenever you're passing a lot of closures around).
- pjc50 12y agoFP doesn't just avoid global variables, it avoids local variables a lot of the time: you compute things from the function's arguments directly. f(a,b,c) always returns the same thing for the same values of a, b, and c and does not modify them. FP hides state behind the concept of monads. This tends to make state-heavy systems harder. Often small embedded systems follow the "state machine" design pattern.