functional-programming

Declaring the type for a templatized function in C#

In c# it's possible to create a list of functions like so: var myList = new List< Func<Foo> >(); This will allow functions (delegates) that take no arguments and return a value of type Foo to be added to the list. So something like: Foo myFunc1() { ... } would be a valid member of that list. My question is, how do I declare the typ...

Functional languages: Real-life Examples

Do functional languages bring anything in the resolution of everyday business problems? Are there any successful projects that have been implemented using a functional language (ideally with a published test case)? ...

How to handle mismatched lists for a Zip algorithm

I'm thinking about writing a Delphi implementation of the functional Zip routine, which takes two lists and outputs a list of pairs of elements. So, feeding it [1, 2, 3] and ['a', 'b', 'c'] would give [(1, 'a'), (2, 'b'), (3, 'c')] as the result. Which is great if both inputs are exactly the same length, like they are in every demo I'v...

What's the difference between functors and "generics"

I'm looking at OCaml's functors. It looks to me pretty identical to the so called generic objects in C++/C#/Java. If you ignore Java's type erasion for now, and ignore the implementation details for C++ templates (I'm interested with the language feature), functors are quite indentical to generics. If I understand it correctly, functor g...

Haskell function composition

I am reading this tutorial on Haskell. They define function composition as the following: (.) :: (b->c) -> (a->b) -> (a->c) f . g = \ x -> f (g x) No examples were provided, which I believe would enlighten me as to what is being defined here. Can someone provide a simple example (with explanation...

Tail calls in architectures without a call stack.

My answer for a recent question about GOTOs and tail recursion was phrased in terms of a call stack. I'm worried that it wasn't sufficiently general, so I ask you: how is the notion of a tail call (or equivalent) useful in architectures without a call stack? In continuation passing, all called functions replace the calling function, and...

Function Table in Scheme using Association List

Hello. I am attempting to build a rudimentary interpreter in Scheme, and I want to use an association list to map to arithmetic functions. This is what i have so far: ; A data type defining an abstract binary operation (define binoptable '(("+" . (+ x y))) ("-" . (- x y)) ("*" . (* x y)) ("/" . (/ x y))) ) The problem is...

functional programming model efficiency (Erlang specific)

Hi I am a newbie in the Erlang world. When I think of how we need to solve the following problem (and there are a long list of similar ones), I think it's really inefficient because we are speaking of a lot of recursion. Apprently, language like C/Java would not need the clumsy recursion to solve this problem, but with Erlang (I guess ot...

Learning Scheme Macros. Help me write a define-syntax-rule

I am new to Scheme Macros. If I just have one pattern and I want to combine the define-syntax and syntax-rules, how do I do that? (define-syntax for (syntax-rules (from to) [(for i from x to y step body) ...] [(for i from x to y body) ...])) If I just have one for, how do I combine the syntax definition and the rule? Thanks...

Unit-safe square roots

I just wondered how it is possible to write a user-defined square root function (sqrt) in a way that it interacts properly with F#'s unit system. What it should be like: let sqrt (x : float<'u ^ 2>) = let x' = x / 1.0<'u ^ 2> // Delete unit (x ** 0.5) * 1.0<'u> // Reassign unit But this is disallowed due to nonzero const...

Tail-recursion optimization in Oz

In the chapter about function in the Oz tutorial, it says that: similar to lazy functional languages Oz allows certain forms of tail-recursion optimizations that are not found in certain strict functional languages including Standard ML, Scheme, and the concurrent functional language Erlang. However, standard function d...

looking for learning exercise: implement these monads

When learning about new programming subjects I usually follow a pattern: I read about it, I understand it, and then I code up a few examples to make sure I really get it. I've read a lot about monads, and I'm confident that I understand and get them. I'm now at a stage where I'd really like to code up a few monads to solidify my under...

What is your favourite cleverly written functional code?

What are your favourite short, mind-blowing snippets in functional languages? My two favourite ones are (Haskell): powerset = filterM (const [True, False]) foldl f v xs = foldr (\x g a -> g (f a x)) id xs v -- from Hutton's tutorial (I tagged the question as Haskell, but examples in all languages - including non-FP ones - are welco...

Recursive function best practices; What are they?

Hello, What are some other language independent ways of designing recursive functions other than the typical: if (counter < 1) return output; else callSelf(); Do other methods even exist? Whenever viewing examples I always see a version of the code above. Thanks! :) ...

Which functional programming languages have bioinformatics libraries?

Which functional programming languages have bioinformatics libraries easily available? (Don't include multi-paradigm languages such as Ruby) Update: Listing which major functional programming languages don't currently have easy access to bioinformatics libraries is also welcome. ...

Pattern matching zero-argument functions in scala: mystified by warning

I'm playing with scala's distributed actors. Very nice. I have a server which executes incoming function objects. For example, the client has object Tasks { def foo = {Console.println("I am Foo")}; def bar = {Console.println("I am Bar");} } // In client actor... ... server ! Tasks.foo _ ... And the server can pick these up an...

Operator Overloading in Clojure

Even looking closely over documentation on Clojure, I do not see any direct confirmation as to whether or not Clojure supports operator overloading. If it does, could someone provide me with a quick snipplet of how to overload, let's say, the "+" operator to delegate to some predefined method that we can call myPlus. I am very new to C...

Functional Programming for Basic Algorithms

How good is 'pure' functional programming for basic routine implementations, e.g. list sorting, string matching etc.? It's common to implement such basic functions within the base interpreter of any functional language, which means that they will be written in an imperative language (c/c++). Although there are many exceptions.. At leas...

Is there a standard name for this function?

What would you name a function that takes a list and a function, and returns True if applying the function to all elements gives the same result? def identical_results(l, func): if len(l) <= 1: return True result = func(l[0]) for el in l[1:]: if func(el) != result: return False return True Is there ...

Copy text to the client's clipboard using jQuery

The workflow is simple: You click inside a textarea. The text is copied to the client's clipboard. Display notice to the user. How do you do it? ...