functional-programming

Functionally traversing a tree in C#

Consider the following extension method in c#, Traverse: IEnumerable<T> Traverse<T>( this IEnumerable<T> source, Func<T, IEnumerable<T>> fnRecurse ); This method allows one to recurse through a tree as defined by T and whatever function causes T to return its subnodes. Now consider the following impleme...

Functional Data Structures in Java

Does the Java standard library have any functional data structures, like immutable Sets, Lists, etc., with functional update? ...

How to populate IEnumerable in Lambda expression?

I just cannot get this to work, would appreciate if someone can help. So I get back an XML result from a database which looks like: <matches> <issuer client_name="MTR" score="6" match_list="MTR CORPORATION LIMITED"/> <issuer client_name="PEOPLE''S REPUBLIC OF CHINA" score="4" match_list="DEMOCRATIC PEOPLE'S REPUBLIC OF KO...

In F#. Is it possible to overload constructor of an abstract type?

If yes could you give an example of a type with parameterless and "parameterfull" constructor. Is that something you would recommend using or does F# provide some alternative more functional way. If yes could you please give an example of it? ...

Still Having issues with this one. Help:)

#include <iostream> #include <string> #include <iomanip> using std::cout; using std::cin; using std::endl; using std::string; using std::setprecision; using std::fixed; //function prototypes void getInput(string &, double) ; void calcFedTaxes ( double , double, double &, double &); void calcnetPay ( double &, d...

Scheme: Implementing n-argument compose using fold

I'm trying to find the "best" implementation of a multi-argument "compose" in Scheme (I know it's a builtin in some implementations, but assume for the moment I am using one that doesn't have this). For a 2-argument compose function I have this: (define compose (lambda (f g) (lambda x (f (apply g x))))) This has the advan...

Haskell Noob question: What's wrong with my append function?

Hi guys, I'm trying to write a Haskell append function... Here's what I have: myappend :: [a] -> [a] -> [a] myappend [] x = x myappend [x:xs] y = x : myappend xs y But it's giving me an error: Occurs check: cannot construct the infinite type: a = [a] When generalising the type(s) for `myappend' So, obviously there's something...

Clojure: How to find out the arity of function at runtime ?

Given a function object or name, how can I determine its arity? Something like (arity func-name) . I hope there is a way, since arity is pretty central in Clojure ...

Getting started with Standard ML

I'm looking for some kind of "ML for beginners" guide - google has led me to some obscure mailing lists or way-over-my-head texts so far. The problem is, I have zero functional programming experience so far, and wrapping my head around the concepts has turned out far more difficult than expected. For example, the task I'm trying to do n...

What is ASP.net in terms of FP?

Maybe I'll look strange. Feel free to edit etc. What is ASP.net in terms of FP? I'm learning FP at the moment and feel rather confused because i cannot divide technology which I use everyday, literally ASP.Net - into some logical parts in terms of FP. Now I treat all ASP.Net machine is like a big state monad, for which I usually use ...

Recommendation for a good functional algorithms book

I'm looking for a good algorithms reference for functional programming. I have this book: but all the examples are written in a procedural pseudocode. It's a fantastic book, though! I also have this one: Although all the examples are in Standard ML and Haskell, it's not really a complete reference since it doesn't cover some of th...

What is a proper way to manage flexible, typed, immutable data structures in Scala?

Right now I have classes like: abstract class Record { // Required fields val productCode:Option[String] val price:Option[Double] // Optional fields val notes:Option[String] = None val used:Option[Boolean] = Option(false) } Then create them: val r = new Record { override val productCode = Option("abc") override val p...

Is it possible to use unary function instead of binary in `flip`?

The type of the Prelude function flip is: flip :: (a -> b -> c) -> b -> a -> c I.e., it takes one binary function and two arguments. The type of the Prelude function id is: id :: a -> a But the type of flip id is: flip id :: a -> (a -> b) -> b How is it possible to apply flip to id when id is a unary function and flip requires ...

Examples of functional programs 'writing themselves' via type analysis

(Background: I've been thinking about doing a presentation on F# and functional programming. From experience, I think that the 'wow' factor of pattern matching and type inference is not necessarily enough to counteract the 'help!' factor of "where are my curly brackets and semicolons, my code is going to fall off the edge!". Which got me...

Managing a shared resource with functional programming style in JavaScript

How does the functional programming paradigm deal with shared resources that need to maintain state? I have a JavaScipt program that allows access from multiple callers to a shared resource, a authenticated HTTP session in this case. The resource has bunch of state information associated with it including things like credentials, cache...

Is functional Clojure or imperative Groovy more readable?

OK, no cheating now. No, really, take a minute or two and try this out. What does "positions" do? Edit: simplified according to cgrand's suggestion. (defn redux [[current next] flag] [(if flag current next) (inc next)]) (defn positions [coll] (map first (reductions redux [1 2] (map = coll (rest coll))))) Now, how about this vers...

Patterns for functional, dynamic and aspect-oriented programming

We have a very nice GoF book (Design Patterns: Elements of Reusable Object-Oriented Software) about patterns in Object Oriented Programming, and plenty of articles and resources in the web on this subject. Are there any books (articles, resources) on patterns(best practices) for functional programming? For dynamic programming in langua...

Trying to make sierpinski triangle generator in a functional programming style

I have a function in Scala, and the same function in JavaScript, but I don't think it is in a functional style. def drawSurroundingTriangles(startx : Double, starty : Double, width : Double) { var newwidth = width/2; var newstartx = startx + newwidth / 2; var newstarty = starty - newwidth; drawTriangle(newstartx, newstarty, newwidth...

Ways to get the middle of a list in Haskell?

I've just started learning about Functional Programming, using Haskel. I'm slowly getting through Erik Meijer's lectures on Channel 9 (I've watched the first 4 so far) and in the 4th video Erik explains how tail works, and it fascinated me. I've tried to write a function that returns the middle of a list (2 items for even lengths, 1 fo...

F#: Typewriter effect in silverlight

Hello all, I recently implemented an animated Typewriter effect in Silverlight using F#. My code works, but I'd like to see if anyone has recommendations on ways the code might be improved. i.e. Would it be a good or bad idea to do it with Seq.iter or Seq.map? Any opinions and comments are welcome! I also thought I'd document the code i...