Tag Info

New answers tagged

0

The hardest part of writing parallel code is to stop one thread from reading data that is being updated by anther thread. A common solution to this is to use immutable objects, so that once an object is created it is never updated. But in real life data has to be change, therefore “persistence” data is used, where every update returns a new object – ...


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 ...


0

Pure functional code is thread-safe by default. That, by itself, is already a huge win. In other programming languages, designing blocks of code that are completely thread-safe can be really, really challenging. But a pure functional language forces you to make everything thread-safe, except in the few places where you explicitly do something that's not ...


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 ...


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 ...


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 ...


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 * ...


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 ...


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


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 ...


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 ...


-1

There's a project for this and other ML books https://code.google.com/p/book-samples-in-fsharp/ where they have converted much of the sample code already. It's been noted in a comment that Google Code is going read-only very soon so worth grabbing that code now. I don't think they will kill it entirely at short notice but you can't tell. I wouldn't ...


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 ...


0

Looking at the slide, its less a system of functions that returns multiple outputs and more a message passing system. If you write an object that can receive and return messages, it can return multiple outputs by simply returning multiple messages. In their case though, the messages that are returned are ordered, unlike most message-passing systems that are ...


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 ...


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 ...


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 ...


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 ...


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 ...



Top 50 recent answers are included