functional-programming

What's the name of the problem that relates to optimizing closures on a stack-based system?

I remember hearing about a general optimization problem that relates to function closures, stating that in general it's difficult to optimize the creation of a closure using only stack-based memory management. Do any of you remember the name of this optimization problem, possibly with an example or link to relevant page? ...

How Functional language are different from the language implementation point of view.

There is the whole new paradigm of "functional programming", which needs a total change of thought patterns compared to procedural programming. It uses higher order functions, purity, monads, etc., which we don't usually see in imperative and object oriented languages. My question is how the implementation of these languages differs fr...

reinventing the wheels: Node.JS/Event-driven programming v.s. Functional Programming?

Now there's all the hype lately about Node.JS, an event driven framework using Javascript callbacks. To my limited understanding, its primary advantage seems to be that you don't have to wait step by step sequentially (for example, you can fetch the SQL results, while calling other functions too). So my question is: how is this differen...

Pros. / Cons. of Immutability vs. Mutability

Well, I guess it's clear the direction from which I am trying to reason about this. There is a lot of talk these days about the virtues of immutability (constness) whenever possible. The Concurrent programming in Java book talks a lot about this too. However, all this is just what I've read about. I personally, haven't coded much in Fun...

Any example in which Clojure really shines against Java which is not concurrency/immutability-feature related?

I can perfectly see why Clojure is really good for concurrent programming. I can see the advantages of FP also in this regard. But clearly, not every line of code that we write is part of a thread or needs concurrent access. For those parts of the code (the more simple and sequential piece of code) what is it that Java really missed tha...

F#, another VBA in Office?

Hi. Excel is a great spreadsheet software, a even greater front UI for Business Intelligence in many companies. Often VBA is used to write the extensions, or to call other DLLs. As Excel itself is functional, F# will company Excel very well to write data analysis procedures. I am thinking whether F# or a variant of F# will become ne...

How might you implement design-by-contract in Clojure specifically or functional languages in general?

I'd prefer examples to be in a Lisp variant (bonus points for Clojure or Scheme) since that's what I'm most familiar with, but any feedback regarding DBC in functional lanugages would of course be valuable to the greater community. Here's an obvious way: (defn foo [action options] (when-not (#{"go-forward" "go-backward" "turn-right...

Erlang : Returning from a function

I have a function in which I have a series of individual case statements. case ... of ... end, case ... of ... end, ... etc. I want to return from the function immediately when a particular case condition occurs in one of the case statements - so that the next case statement is not checked, and the function just exits/re...

Generics and Functions in C#

Here is some code, it won't compile but essentially want I want to create is a function that parses as CSV file and then converts the values in the CSV list to a specific type. Func<string, Func<string,T>, IEnumerable<T>> parser =(string csv, Func<string, T> newFunc) => { List<T> items = new List<T>(); string[] a...

What does 'parametrize' do in DrScheme?

I'm trying to make sense of the example code here (below Examples). I don't understand that parametrize construct. The docs for it are here, but they don't help. What does it do? ...

Is there a common name for a function that takes a list of lists and returns a single list containing the contents of those lists?

EDIT: My question was originally "Is there a standard name for a function that flattens a list of lists, but only one level deep?", but Chuck's answer is phrased much closer to what I actually wanted to ask, so I renamed it. All three answers were useful to me, though. Thanks. 'flatten' seems to be a well-accepted name for a function th...

Erlang : Traversing N by N area

I am looking to traverse a NxN area, given the starting points X,Y and the size of the square to traverse. E.g. given X=10,Y=12,Size=2 - i want to generate 10,10 ; 10,11 ; 11,10 and 11,11. I came up with this, but it seems to go on and on infinitely: traverse({X,Y,Xend,Yend}) -> % print X,Y values here.... case (X == Xen...

A List processing problem in F#

I am trying to do problem 12 in Project Euler. numDivisor64 is to calculate number of divisors. I wrote this F# code: let problem12 = {1L..300000L} |> Seq.map (fun x->x*(x+1L)/2L) |> Seq.map numDivisor64 |> Seq.filter (fun x->x>500L) The problem asks to find the number rather than its # of divisors. Besides writing this in a l...

In Functional Programming, is it considered a bad practice to have incomplete pattern matchings

Is it generally considered a bad practice to use non-exhaustive pattern machings in functional languages like Haskell or F#, which means that the cases specified don't cover all possible input cases? In particular, should I allow code to fail with a MatchFailureException etc. or should I always cover all cases and explicitly throw an er...

None-Pure Functional Code Smells

What are some of the common signs your taking the wrong approach in a mixed functional / imperative / OO environment? ...

Does Functional programming allow better runtime compiler optimizations?

NOTE: Already made this a Wiki. I don't care what this question is tagged as, as long as there is a good discussion. I've heard that since in pure functional programs, there are no side effects and values dont mutate, it makes it easier for the compiler to make more runtime optimizations. To what extent is this true? If this is true, m...

An Exercise: map or reduce a map in Python without list comprehensions?

When I started writing this question, I didn't think of the easy solution with nested lists, but now anyway want to find one. Here's an ugly code: fun0( fun1(fun2(fun3(arg1))), fun1(fun2(fun3(arg4))), fun1(fun2(fun3(arg4))), fun1(fun2(fun3(arg4)))) Ouch! Names are given for examples. In the real application, their nam...

How do I write all-but-one function in Scheme/LISP?

Can you guys think of the shortest and the most idiomatic solution to all-but-one function? ;; all-but-one ;; checks if all but one element in a list holds a certain property ;; (all-but-one even? (list 1 2 4)) -> true ;; (all-but-one even? '(1)) -> true ;; (all-but-one even? '(2 4)) -> false Edit: all but EXACTLY one. ...

list comprehension in F#

I am trying to do some list comprehension in F#. And I found this. let evens n = { for x in 1 .. n when x % 2 = 0 -> x } print_any (evens 10) let squarePoints n = { for x in 1 .. n for y in 1 .. n -> x,y } print_any (squarePoints 3) The first still works ok, but the second is outdated. The latest (1.9.7.8) F# compiler...

Are there problems that cannot be written using tail recursion?

Tail recursion is an important performance optimisation stragegy in functional languages because it allows recursive calls to consume constant stack (rather than O(n)). Are there any problems that simply cannot be written in a tail-recursive style, or is it always possible to convert a naively-recursive function into a tail-recursive on...