functional-programming

OCaml: What is the different between `fun` and `function` keywords?

Sometimes I see code like let (alt : recognizer -> recognizer -> recognizer) = fun a b p -> union (a p) (b p) Or like: let hd = function Cons(x,xf) -> x | Nil -> raise Empty What is the difference between fun and function? ...

How do I use map() to apply an operation to each element of a hash in Perl?

I've got a piece of code that works fine. It basically loops through each element of a hash using foreach() and applies a transformation to it using a regular expression, like so : foreach my $key ( keys( %{$results} ) ) { $results->{$key}{uri} =~ s/\".*\/(.*\.*(gif|jpe?g|png))\"/\/resources\/uploads\/$1/gi; } $results is a hashref...

Why is determining if a function is pure difficult?

I was at the StackOverflow Dev Days convention yesterday, and one of the speakers was talking about Python. He showed a Memoize function, and I asked if there was any way to keep it from being used on a non-pure function. He said no, that's basically impossible, and if someone could figure out a way to do it it would make a great PhD t...

What is wrong with this lambda expression?

I am really struggling to understand why, when I change my code to use a lamdba expression, it doesn't work. This code works and prints on console: object dummy = new object(); InterServer.ExecuteDataReader(new InterServerRequest(ServerID.a_01, "dbo.getbooks") { Params = new Dictionary<string, object> { { "To...

Is there an efficient index persistent data structure with multiple indexes

I am looking for an efficient indexed persistent data structure. I typically work in .NET and am aware of FSharp's Map however that implementation and most others I am aware of only provide a single 'index', the left side of the mapping. Basically here is the scenario public class MyObject public int Id { get; } public int Gro...

Choosing a syntax for list generating expressions

C# has generator functions which have syntax like: IEnumerable<int> GetNats(int max) { for (int i=0; i < max; ++i) yield return i; } A feature I am interested in for my programming language (a simple object-oriented programming similar to Java, Scala, ActionScript, and C#) are generator expressions. These are essentially syn...

Instantiate array in F# ??

In C# I'd write something like MyType arr = new MyType[10]; to alloc arr as array which has 10 items of type MyType. How to do the same in F# ?? let mutable arr = ????????????? ...

Parallel Map (Generator) Operator

I am interested in defining a parallel map operator for my language. It transforms a list into a new list given an expression. It would have a syntax similar to a generator. Unlike generators in C# and Python, it would potentially be evaluated in parallel, if the compiler desires (e.g. there is an idle core, and the list is really big). ...

Mutable Data in OCaml

I've created a mutable data structure in OCaml, however when I go to access it, it gives a weird error, Here is my code type vector = {a:float;b:float};; type vec_store = {mutable seq:vector array;mutable size:int};; let max_seq_length = ref 200;; exception Out_of_bounds;; exception Vec_store_full;; let vec_mag {a=c;b=d} = sqrt( c*...

What is the best way to initialize all the elements of a JTable with Clojure?

Sorry if this is a bit of a noob question but I am still getting used to functional programming. I want to write a simple Sudoku solver as an exercise. One of my plans is to create a JTable with 9 rows and 9 columns and initialize them all with the string "123456789" as a starting position. If I have a TableModel I can define a functi...

Is this a better (more functional way) to write the following fsharp code?

Hi, I have pieces of code like this in a project and I realize it's not written in a functional way: let data = Array.zeroCreate(3 + (int)firmwareVersions.Count * 27) data.[0] <- 0x09uy //drcode data.[1..2] <- firmwareVersionBytes //Number of firmware versions let mutable index = 0 let loops ...

Scheme/Lisp nested loops and recursion

I'm trying to solve a problem in Scheme which is demanding me to use a nested loop or a nested recursion. e.g. I have two lists which I have to check a condition on their Cartesian product. What is the best way to approach these types of problems? Any pointers on how to simplify these types of functions? I'll elaborate a bit, since m...

functional programming: immutable data structure efficiency

Hi, I don't understand, how FP compilers make the code dealing with immutable data structures fast, not blow up stack, etc. For example, insert operation in tree, it has to copy the whole tree before adding the new node and return the copied tree, versus the imperative couterpart that only needs to add a pointer to the new node. If the ...

Issues with maps and their entries in Scala

I have a recursive function that takes a Map as single parameter. It then adds new entries to that Map and calls itself with this larger Map. Please ignore the return values for now. The function isn't finished yet. Here's the code: def breadthFirstHelper( found: Map[AIS_State,(Option[AIS_State], Int)] ): List[AIS_State] = { val exten...

lambda calculus for functional programming

in lambda calculus (λ x. λ y. λ s. λ z. x s (y s z)) is used for addition of two Church numerals how can we explain this, is there any good resource the lambda calculus for functional programming ? your help is much appreciated ...

Can I zip more than two lists together in Scala?

Given the following Scala List: val l = List(List("a1", "b1", "c1"), List("a2", "b2", "c2"), List("a3", "b3", "c3")) How can I get: List(("a1", "a2", "a3"), ("b1", "b2", "b3"), ("c1", "c2", "c3")) Since zip can only be used to combine two Lists, I think you would need to iterate/reduce the main List somehow. Not surprisingly, the f...

Find max value in Java with a predefined comparator.

I have a List<Foo>, and a compare() method taking two Foo objects and returning the 'greater' one. Is there a built-in Java method that takes the list and finds the largest one? ...

Selection sort in functional Scala

I'm making my way through "Programming in Scala" and wrote a quick implementation of the selection sort algorithm. However, since I'm still a bit green in functional programming, I'm having trouble translating to a more Scala-ish style. For the Scala programmers out there, how can I do this using Lists and vals rather than falling back i...

Clojure: Doesn't the ability to use Java objects with state defy the whole idea of functional P?

I thought the whole idea was to have only computation with NO state and NO side effects. Now if a Clojure app(or even worse, a reusable Clojure library ) can use and create any Java object, how can I be sure I don't get side effects or state ? ...

Anyone ever flip (<$>)

I found defining the following (%) = flip fmap I can write code like the following: readFile "/etc/passwd" % lines % filter (not . null) to me it makes more sense then the alternative filter (not . null) <$> lines <$> readFile "/etc/passwd" Obviously its just a matter of order, does anyone else do this? is there a valid reason ...