Tag Info

Hot answers tagged

48

The main reason is that referential transparency (and even more so laziness) abstracts over the execution order. This makes it trivial to parallelize evaluation. For example, if both a, b, and || are referentially transparent, then it doesn't matter if in a || b a gets evaluated first, b gets evaluated first, or b doesn't get evaluated at all (because a ...


15

First of all, why not use Array.prototype.some(), which seems to perfectly match what you're actually trying to do. The some() method tests whether some element in the array passes the test implemented by the provided function. Of course, that doesn't really answer your question: whether to use the for loop, or the call to Array.protoype.reduce()? Let ...


10

Because they work well with immutability. Suppose you have two immutable lists, [1, 2, 3] and [10, 2, 3]. Represented as singly linked lists where each item in the list is a node containing the item and a pointer to the rest of the list, they'd look like this: node -> node -> node -> empty 1 2 3 node -> node -> node -> empty ...


6

Consider (some of) what a function pointer has: type and what you can do with it: pass it as an argument call it return it as result assign it to a variable/store it compare it to another pointer of the same type Functions as parameters is only one of the operations on this list, thus is less powerful than function pointers. However, this merely ...


6

These three "primitive control structures" are ways to describe the ways in which a manager (superior) and two (or more) worker (subordinate) procedures coordinate their work. The Join primitive shows that the manager calls A with some arguments, A returns some values, and then the manager calls B with A's results. A's output parameter signature matches ...


5

For most functional languages, neither. There is usually a reducing function which short circuts as you want it to. In F# this is List.exists so you example would be List.exists (fun x -> x > 5) a In Haskell (with partial application of > to make a point free solution) any (> 5) a


5

Given immutability (which is often encouraged and said to be one of building blocks of functional programming) and CQS (which says that commands should not return a value other than void/unit), how do these work together? There are no commands in functional programming. Period. A function's result can only depend on its arguments, and it must be the ...


4

CQS is an idiom from object oriented languages that is designed to help avoid the confusion that can be caused when a function unexpectedly mutates state or interacts with the environment. This is not a problem for a functional language. In functional languages, all such interaction is explicit in the signatures of the functions involved. Hence, CQS serves ...


3

You might look into the Reader or State monads; they can be used for sharing data between (pure, monadic) functions, without resorting to explicit parameters or the like. There's a tutorial series on F# for fun and profit about the State monad (AKA Workflow in F# parlence). I also gave a description of it in this SO answer that appears to be fairly ...


3

Minimised shared state What is it about functional programming that makes it inherently adapted to parallel execution? The pure nature of the functions (referential transparency), i.e. having no side effects, leads to fewer shared objects and hence less shared state. A simple example is; double CircleCircumference(double radius) { return 2 * 3.14 * ...


3

It's hard to be sure exactly what you mean by "purely academic", but I think the answer is mostly "no". As explained in Tackling the Awkward Squad by Simon Peyton Jones (strongly recommended reading!), monadic I/O was meant to solve real problems with the way Haskell used to handle I/O. Read the example of the server with Requests and Responses, which I ...


2

Lets first look at why procedural programming is so bad at concurrent threads. With a concurrent programming model, you are writing sequential instructions that (by default) expect to be run in isolation. When you introduce multiple threads, you need to explicitly control access to prevent concurrent access to a shared variable when those changes may ...


2

It is certainly possible to define an iteration order for x1 <- [1..], x2 <- [1..], ..., xK <- [1..] that will reach any tuple (c1, c2, ..., cK) after some finite amount of steps, and tries each tuple only once. Take any computable bijection between the natural numbers N and the k-tuples of natural numbers, N^k. Such a bijection exists, as the ...


2

Why is IO impure? Because it may return different values at different times. There is a dependency on time that must be accounted for, one way or another. This is even more crucial with lazy evaluation. Consider the following program: main = do putStrLn "Please enter your name" name <- getLine putStrLn $ "Hello, " ++ name Without ...


1

You're right, this is an issue when one wants to reason about purity of functions in a language that permits impure functions. Technically almost all languages allow impurity, but the purely functional ones usually explicitly mark the impure ones in the type system, such that the Haskell function map :: (a -> b) -> [a] -> [b] does, implicitly, ...


1

There is a constraint on the predicate, but it's textually enforced by standard text and may be enforced by a sufficiently capable compiler as Quality-of-Implementation. Many things are only guarded by phrasing in the standard. Some of the rationale is that while its possible to constrain the predicate to ones that take the argument by value or const ...


1

A function pointer (e.g. in C or C++) is only the address of some machine code (computing some function). It does know about any data except the constants embedded in the code, and the static data referenced by it. Conceptually (e.g. in lambda-calculus) a function needs both some code to be computed, and some data to be accessed. So a function usually has ...



Only top voted, non community-wiki answers of a minimum length are eligible