functional-programming

Haskell coding-style: map, fmap or <$> ?

Is there any reason to prefer one of the following notations over the others or is this simply a matter of preference? map toLower "FOO" fmap toLower "FOO" toLower <$> "FOO" As an aside: I realize that <$> is the same as `fmap`. Am I right in the assumption that map is just a less general form of fmap? ...

map over structure with only partial match

I have a tree-like structure of abstract classes and case classes representing an Abstract Syntax Tree of a small language. For the top abstract class i've implemented a method map: abstract class AST { ... def map(f: (AST => AST)): AST = { val b1 = this match { case s: STRUCTURAL => s.smap(f) // structural node for examp...

Type mismatch error. F# type inference fail?

I'm trying to write a method in F# that returns a new instance of a generic type based upon the type of a value passed into the method. In FSI: open System.Collections.Generic type AttributeIndex<'a>() = inherit SortedDictionary<'a, HashSet<int array>>() let getNewIndexForValue (value: obj) : AttributeIndex<_> = match valu...

c++ is_str_empty predicate

std::vector<std::wstring> lines; typedef std::vector<std::wstring>::iterator iterator_t; iterator_t eventLine = std::find_if(lines.begin(), lines.end(), !is_str_empty()); how do I define is_str_empty? i don't believe boost supplies it. ...

Why don't Python's list comprehensions make copies of arguments so actual objects can't be mutated?

Maybe I've been drinking too much of the functional programming Kool Aid, but this behavior of list comprehensions seems like a bad design choice: >>> d = [1, 2, 3, 4, 5] >>> [d.pop() for _ in range(len(d))] [5, 4, 3, 2, 1] >>> d [] Why is d not copied, and then the copied lexically-scoped version not mutated (and then lost)? The poin...

What are the popular 'web-ready' functional programming languages?

Hello, This is a simple question: what are the most popular/used/developed (libraries a plus) functional programming languages that are ready to be used for web development? I don't mind if they're pure languages or not, but I would prefer to exclude such languages as Ruby and Python. I am thinking along the lines of F# or Scheme (and...

Function as an argument to a method

You can do anonymous functions in C# like you can in JavaScript: JavaScript: var s = (function () { return "Hello World!"; }()); C#: var s = new Func<String>(() => { return "Hello World!"; })(); ... In JavaScript you can pass functions to be executed by other functions. On top of that; you can pass parameters to the functi...

F#: Best way to condense a list of option type down to only elements that are not none?

I'm unexpectedly having a bit of trouble with going from a list of 'a option down to a list containing only the elements that are Some. My initial attempt was: let ga = List.filter (fun xx -> match xx with | Some(g) -> true | None -> false) gao But of course, this result type is still 'a option list. I do...

How to define a variable based on an if/then/else statement

I'm trying to translate some python code to haskell. However I reached a point where I'm not sure how to proceed. if len(prod) % 2 == 0: ss = float(1.5 * count_vowels(cust)) else: ss = float(count_consonants(cust)) # muliplicaton by 1 is implied. if len(cust_factors.intersection(prod_factors)) > 0: ss *= 1.5 return ss I'...

Scala: Filtering based on type

I'm learning Scala as it fits my needs well but I am finding it hard to structure code elegantly. I'm in a situation where I have a List x and want to create two Lists: one containing all the elements of SomeClass and one containing all the elements that aren't of SomeClass. val a = x collect {case y:SomeClass => y} val b = x filterNot ...

Custom control structures in Scala?

There are a number of times I've run into a simple pattern when programming in Java or C++ for which a custom control structure could reduce the boilerplate within my code. It goes something like: if( Predicate ){ Action return Value } that is, a "return if"-type statement. I've tried making functions with signature lik...

If functional languages are really concise, why don't they have a better rank in the language shootout game?

I compared the languages at the language shootout game by their code size only. Here is a summary of what I got (shortest first, grouped by similar score). Python, Ruby, JavaScript, Perl, Lua, PHP, Mozart/OZ OCaml, Erlang, Racket, Go, Scala, F#, Smalltalk Pascal, Clean, Haskell, Common Lisp, C#, Java, C C++, Ada, ATS I wonder why. ...

Comparison Of Nemerle and F# For Functional On .Net

Hi all, Community Wiki Question: Pursuant to this question: What are the benefits of using Scala in .Net? another question comes to mind. Can anyone lay out the comparative advantages (and disadvantages) of Nemerle and F# for functional development on the .Net platform? I've just looked at Nemerle in passing. It sounds like it kind ...

What is the difference between a variable and a symbol in LISP?

In terms of scope? Actual implementation in memory? The syntax? For eg, if (let a 1) Is 'a' a variable or a symbol? ...

Fiddling with point-free code?

I have been learning the Factor and J languages to experiment with point-free programming. The basic mechanics of the languages seem clear, but getting a feeling for how to approach algorithm design is a challenge. A particular source of confusion for me is how one should structure code so that it is easy to experiment with different pa...

Correct usage of mutable/immutable lists

At the moment, Im trying to understand Functional Programming in Scala and I came across a problem I cannot figure out myself. Imagine the following situation: You have two classes: Controller and Bot. A Bot is an independent Actor which is initiated by a Controller, does some expensive operation and returns the result to the Controll...

How to factor a number functionally

For example, if the input is 825 the output expected is (0 1 2 0 1). What this means is: 0 two's, 1 three's, 2 five's, 0 seven's and 1 eleven. Doing this imperatively was quite easy for me. Functional, not so much. Could you please guide me how to go about solving the above problem in a functional way? Note: Fold/reduce ways will be pr...

What is the 'expression problem'?

I have a rough idea about what this is but if someone has an explanation of the 'expression problem' that they think is succinct and intuitive I would love to hear it. ...

Implementing a direct-threaded interpreter in a functional language like OCaml

In C/C++ you can implement a direct threaded interpreter with an array of function pointers. The array represents your program - an array of operations. Each of the operation functions must end in a call to the next function in the array, something like: void op_plus(size_t pc, uint8_t* data) { *data += 1; BytecodeArray[pc+1](pc+1...

Towers of Hanoi with K pegs

The Towers of Hanoi problem is a classic problem for recursion. You are given 3 pegs with disks on one of them, and you must move all the disks from one peg to another, by following the given rules. You must also do this with the minimum number of moves. Here's a recursive algorithm that solves the problem: void Hanoi3(int nDisks, char...