functional-programming

Define self referencing Scala class (recursive class)

How to define this class in scala data NestedList a = Elem a | List [NestedList a] This in Haskell means a NestedList is a Type which can contain either Elem or another NestedList. Is it possible to do these kind of recursive definitions in scala? Actually this is what I am trying to acheive Check Problem 7 here in this page. Update...

How to perform pattern matching with vararg case classes?

I have a set of case classes like this abstract class Shape case class Rectangle(width: Int, height: Int) extends Shape case class Location(x: Int, y: Int, shape: Shape) extends Shape case class Circle(radius: Int) extends Shape case class Group(shape: Shape*) extends Shape where basically Group is an array of shapes. I need t...

C# Dictionary Composition

Let's say I have an arbitray list of A class A { string K {get;set;} string V {get;set;} } ... List<A> theList = ... Is there an easy way to compose a dictionary from theList? (something like the following) Dictionary<string, string> dict = magic(x => x.K, x => x.V, theList) I don't want to write the following code: var d = new D...

Why is Haskell used so little in the industry?

It is a wonderful, very fast, mature and complete language. It exists for a very long time and has a big set of libraries. Yet, it appears not to be widely used. Why ? I suspect it is because it is pretty rough and unforgiving for beginners, and maybe because its lazy execution makes it even harder ...

"Functional programming" has a clear meaning, but does "functional language"?

I understand very clearly the difference between functional and imperative programming techniques. But there's a widespread tendency to talk of "functional languages", and this really confuses me. Of course some languages like Haskell are more hospitable to functional programming than other languages like C. But even the former does I/O...

Explaining functional programming to object-oriented programmers and less technical people

What are some good examples that I can use to explain functional programming? The audience would be people with little programming experience, or people who only have object-oriented experience. ...

Functional equivalent of if (p(f(a), f(b)) a else b

I'm guessing that there must be a better functional way of expressing the following: def foo(i: Any) : Int if (foo(a) < foo(b)) a else b So in this example f == foo and p == _ < _. There's bound to be some masterful cleverness in scalaz for this! I can see that using BooleanW I can write: p(f(a), f(b)).option(a).getOrElse(b) But ...

Which kind of program composition do you prefer to work on projects and why?

I work at an internet company and we make PHP and .Net (C #) sites / applications our principle is to get all colleagues to a higher level but also the efficiency is important there are 10 web developers and searching for the best mix to make applications like parallel programming / more developers one a project we also have used some...

calculate array element persistence algorithm

I have a large array 15x15x2200. THis is merely a collection of 15x15 sparse matrices depicting the links between 15 nodes and how they vary over 2200 time units. I need to calculate for how much time does each link persist. By this I mean, suppose A[4,11] is 0 till time unit 5 and stays 1 till time unit 20 and then becomes 0 and again b...

How to make this code more functional ?

Hi I am a newbie in functional programming. I just tried solving the following problem : [ a rough specification ] e.g.1: dividend : {3,5,9} divisor : {2,2} radix = 10 ans (remainder) : {7} Procedure : dividend = 3*10^2+5*10^1+9*10^0 = 359 similarly, divisor = 22 so 359 % 22 = 7 e.g.2: dividend : {555,555,555,555,555,555,555,555,55...

Closure conversion and separate compilation of higher-order function calls

Is there a standard way of dealing with the interaction between separate compilation and different kinds of closure conversion when compiling higher-order function calls? I know of three function-like constructs that are distinctly compiled in most programming languages: closures, (top-level) functions, and C++-style function objects. S...

Do any functional languages support divide-and-conquer natively?

I'm sorry that I couldn't figure out a way to express the question more clearly in the headline, but essentially, it's this: almost all functional languages have constructs that allow you to process a variable list of arguments through tail recursion, as in this Erlang-ish pseudocode that sums up a list of numbers: sumup(0,A) -> A. sum...

Is List.partition guaranteed to preserve order?

I've noticed it seems to behave this way, but I don't want to rely on it if it's not intentional. Here's the code in question: let bestValuesUnder max = allValues >> List.partition (fun value -> value < max) >> function | ([], bad) -> [List.min bad] | (good, _) -> good // |> List.sortBy (fun value -> -value) ...

In a functional language, how to conditionally select elements to be used in zip- or zipWith-style function?

I am familiar with standard zipWith functions which operate on corresponding elements of two sequences, but in a functional language (or a language with some functional features), what is the most succinct way to conditionally select the pairs of elements to be zipped, based on a third sequence? This curiosity arose while scratching o...

Is it possible to decorate include(...) in djano's urls with login_required?

I have a few restricted areas on the site, for which I would like to specify login_required decorator. However I would like to do that once per inclusion in main urls.py, not per individual url in included urls.py So instead of: /private/urls.py: (r'^profile/$', login_required(profile)), I'd do something along the lines: /urls.py ...

Function object in C++

Hi, I was asked about one question I totally had no idea. What's the another name for function object in C++ 1) Actions 2) Predicates 3) Operators 4) Use case. I am confused about the point the question is supposed to deliver. I searched online a lot but no clue about that. ...

What is the difference between scala's case class and class

I searched in google to find the differences between the two, every one mentions that when you want to do pattern matching on the class use case class else use classes and also mentioning some extra perks like equals and hascode overriding. But are these the only reasons why one should use a case class instead of class? I guess there sho...

How can I write a function that returns a list of keys in a nested table?

I have a hierarchically nested associative array. It looks like this: A = { B = { C = {}, D = {}, }, E = { F = { G = {} } }, H = {} } I would like to write a function that returns the "ancestors" of each key. So: f("A") = {"A"} f("B") = {"B","A"} f("C") =...

What implementation of Scheme would you recommend to learn the language?

I am going to read the classic "Structure and Interpretation of Computer Programs" link text and it utilizes the Scheme language to teach its material. Does any one have advice about what Scheme would be easy to use (on Windows) and complete? Thanks! ...

Haskell n-ary tree traversal

Hi, I'm pretty new to Haskell and I'm trying to work out how to traverse a n-ary tree. As output I'm looking to get a list of Leaf values (as the branches have no value), so for testtree this would be: 4,5 My definition so far is: data Tree a = Leaf a | Branch [Tree a] deriving (Show) travTree :: Tree a -> [a] travT...