functional-programming

Side-effects in closures, are they still purely functional?

Being relatively new to functional programming, I expend lots of energy wondering “is this the functional way to do things?” Obviously recursion vs. iteration is pretty straightforward and it’s obvious that recursion is the functional way of doing things. But take closures for instance. I’ve learned about closures using Lisp and I under...

Efficient persistent data structures for relational database

I'm looking for material on persistent data structures that can be used to implement a relational model. Persistence in the meaning of immutable data structures. Anyone know of some good resources, books, papers and such? (I already have the book Purely Functional Data Structures, which is a good example of what I'm looking for.) ...

Do you use Template Method Pattern in programming languages with closures/delegates/function pointers ?

I have been going back and forth between C# and Java for the last 8 years. One thing that strikes me is that I have completely stopped using the "Template Method" design pattern in C#. Actually, in C# I Have come to think of this pattern as an anti-pattern. http://en.wikipedia.org/wiki/Template_method_pattern Coming back to Java, I ...

Nested functions: Improper use of side-effects?

I'm learning functional programming, and have tried to solve a couple problems in a functional style. One thing I experienced, while dividing up my problem into functions, was it seemed I had two options: use several disparate functions with similar parameter lists, or using nested functions which, as closures, can simply refer to bindin...

What's a good way to rewrite this non-tail-recursive function?

For some reason, I am having trouble thinking of a good way to rewrite this function so it uses constant stack space. Most online discussions of tree recursion cheat by using the Fibonacci function and exploiting the properties of that particular problem. Does anyone have any ideas for this "real-world" (well, more real-world than the Fi...

Functional alternative?

Hi all, As I continue my quest of learning functional programming, I've come to wonder if there may be alternatives to my default "procedural" way of thinking. To be more specific, I'm looking at a function I wrote. Here is what it does: Swap two elements of an unordered list of numbers, such that one of the elements is now in the r...

#inject and slowness

I've often heard Ruby's inject method criticized as being "slow." As I rather like the function, and see equivalents in other languages, I'm curious if it's merely Ruby's implementation of the method that's slow, or if it is inherently a slow way to do things (e.g. should be avoided for non-small collections)? ...

Algebraic types in haskell

How can I simplify an expression using basic arithmetic? ...

convert an exression to a string representation?

Consider the following Haskell code: module Expr where -- Variables are named by strings, assumed to be identifiers: type Variable = String -- Representation of expressions: data Expr = Const Integer | Var Variable | Plus Expr Expr | Minus Expr Expr | Mult Expr Expr d...

Primitive recursion

how will i define the function 'simplify' using primitive recursion? simplify :: Expr -> Expr ... simplify Simplify an expression using basic arithmetic, e.g. simplify (Plus (Var "x") (Const 0)) = Var "x" ...

How can I evaluate an expression?

How can I evaluate an expression, given a list of values for the variables it contains? eval::[(Variable,Integer)]->Expr->Integer Example: eval[("x",2), ("y",4)](Mult(Plus(Var "x") (Const))(Var "y"))= 12 ...

F#: This expression should have type 'unit', but has type 'ConsoleKeyInfo'

This is a simple question, but I'm having trouble tracking down an answer since F# is so new. I just wanted to pause in an F# console application, so I wrote: Console.ReadKey() But this gives the warning: This expression should have type 'unit', but has type 'ConsoleKeyInfo'. Any help would be greatly appreciated. Thanks, - Lee ...

How can I write an addition of two items in a string representation?

For example I want to add two expressions e1 and e2 toString (Plus e1 e)= ?? I am guessing it would be something like toString (Plus e1 e)= ((toString e1) ++ "+" ++ (toString e2)) ...

Does Functional Programming Replace GoF Design Patterns?

Since I started learning F# and OCaml last year, I've read a huge number of articles which insist that design patterns (especially in Java) are workarounds for the missing features in imperative languages. One article I found makes a fairly strong claim: Most people I've met have read the Design Patterns book by the Gang of Four....

Why should I use F#?

I write client-server based business applications using .Net and C#. Given this, how would F# enable me to write better code? "Better" in any sense, e.g. faster coding, faster execution, better reliability, more flexibility. In short, why should I use F#? ...

How do I convert a variable to a string?

For example so that it works like this toString (Var x)= "x" ...

Medium-size Clojure sample application?

Is there a medium-sized Clojure sample application that could be used as a "best-practices" example, and a good way to see what such an application would look like in terms of code and code organization? A web application would be particularly interesting to me, but most important is that the program do something commonly useful (blog, b...

Are Databases and Functional Programming at odds?

I've been a web developer for some time now, and have recently started learning some functional programming. Like others, I've had some significant trouble apply many of these concepts to my professional work. For me, the primary reason for this is I see a conflict between between FP's goal of remaining stateless seems quite at odds wi...

F# XML parsing

this c# code is probably not the most efficient but gets what I want done. How do I accomplish the same thing in F# code? string xml = " <EmailList> " + " <Email>[email protected]</Email> " + " <Email>[email protected]</Email> " + " </EmailList> "; XmlDocument xdoc = new XmlD...

Pipe forwards in C#

Continuing my investigation of expressing F# ideas in C#, I wanted a pipe forward operator. For anything wrapped in a IEnumerable, we already have it, as you can .NextFunc() to your heart's content. But for example if you have any fold-like reduction at the end, you can't feed the result of that into a function. Here are two extension m...