functional-programming

Functional way to get a matrix from text

I'm trying to solve some Google Code Jam problems, where an input matrix is typically given in this form: 2 3 #matrix dimensions 1 2 3 4 5 6 7 8 9 # all 3 elements in the first row 2 3 4 5 6 7 8 9 0 # each element is composed of three integers where each element of the matrix is composed of, say, three integers. So this example should...

Transformation of a list of positions to a 2D array of positions using functional programming (F#)

How would you make the folowing code functional with the same speed? In general, as an input I have a list of objects containing position coordinates and other stuff and I need to create a 2D array consisting those objects. let m = Matrix.Generic.create 6 6 [] let pos = [(1.3,4.3); (5.6,5.4); (1.5,4.8)] pos |> List.iter (fun (pz,py) ...

Can immutable be a memory hog?

Let's say we have a memory-intensive class like an Image, with chainable methods like Resize() and ConvertTo(). If this class is immutable, won't it take a huge amount of memory when I start doing things like i.Resize(500, 800).Rotate(90).ConvertTo(Gif), compared to a mutable one which modifies itself? How to handle a situation like th...

Why is the Clojure Hello World program so slow compared to Java and Python?

Update As suggested by many people, it looks like this was because of the fact that clojure code was first compiled and then executed. AOT compilation should help offset that. Given I found the practical Clojure AOT compilation process a little tricky to solve (classpath issues, directory issues et al), I've written up a small step by s...

How well do zippers perform in practice, and when should they be used?

I think that the zipper is a beautiful idea; it elegantly provides a way to walk a list or tree and make what appear to be local updates in a functional way. Asymptotically, the costs appear to be reasonable. But traversing the data structure requires memory allocation at each iteration, where a normal list or tree traversal is just po...

Higher order functions in C

Is there a "proper" way to implement higher order functions in C. I'm mostly curious about things like portability and syntax correctness here and if there are more than one ways what the merits and flaws are. Edit: The reason I want to know how to create higher order functions are that I have written a system to convert PyObject lists...

Function syntax puzzler in scalaz

Following watching Nick Partidge's presentation on deriving scalaz, I got to looking at this example, which is just awesome: import scalaz._ import Scalaz._ def even(x: Int) : Validation[NonEmptyList[String], Int] = if (x % 2 ==0) x.success else "not even: %d".format(x).wrapNel.fail println( even(3) <|*|> even(5) ) //prints: Failu...

Which programming language or a library can process Infinite Series ?

Which programming language or a library is able to process infinite series (like geometric or harmonic)? It perhaps must have a database of some well-known series and automatically give proper values in case of convergence, and maybe generate an exception in case of divergence. For example, in Python it could look like: sum = 0 sign =...

Scalaz Kleisli question

There is a trait called Kleisli in the scalaz library. Looking at the code: import scalaz._ import Scalaz._ type StringPair = (String, String) val f: Int => List[String] = (i: Int) => List((i |+| 1).toString, (i |+| 2).toString) val g: String => List[StringPair] = (s: String) => List("X" -> s, s -> "Y") val k = kleisli(f) >=> k...

Scalaz: request for use case for Cokleisli composition

This question isn't meant as flame-bait! As it might be apparent, I've been looking at Scalaz recently. I'm trying to understand why I need some of the functionality that the library provides. Here's something: import scalaz._ import Scalaz._ type NEL[A] = NonEmptyList[A] val NEL = NonEmptyList I put some println statements in my func...

How do I get Mathematica to thread a 2-variable function over two lists, using functional programming techniques?

Lets say I have a function f[x_, y_], and two lists l1, l2. I'd like to evaluate f[x,y] for each pair x,y with x in l1 and y in l2, and I'd like to do it without having to make all pairs of the form {l1[[i]],l2[[j]]}. Essentially, what I want is something like Map[Map[f[#1, #2]&, l1],l2] where #1 takes values from l1 and #2 takes value...

Why a main game loop is necessary for developing a game?

I find that many game development requires a main game loop, but I don't know why it is necessary. Can't we implement an event listener, and response to every user actions. Also, the animation can be played when a event occurs. What is the purpose for making a main game loop. ...

Using MonadPlus in FRP.Reactive.FieldTrip

I'm studying FRP at this moment through FieldTrip adaptor. And hit the problem with strange way of frames scheduling and integration. So now I'm trying to build own marker Event for aligning Behaviour stepping. So... flipflop :: Behavior String flipflop = stepper "none" (xflip 2) where xflip t0 = do t <- withTimeE_ (atTime ...

Is there a good book specialized in exercises in dynamic programming?

Is there a good book specialized in exercises on finding recurrence cause i want to train on (dynamic programming) cause i find myself a lot knowing that a problem can be solved dynamically but i can't find the recurrence function . ...

Transient collections for Scala?

Clojure has a very nice concept of transient collections. Is there a library providing those for Scala (or F#)? ...

Alter a function as a parameter before evaluating it in R?

Is there any way, given a function passed as a parameter, to alter its input parameter string before evaluating it? Here's pseudo-code for what I'm hoping to achieve: test.func <- function(a, b) { # here I want to alter the b expression before evaluating it: b(..., val1=a) } Given the function call passed to b, I want to ad...

Haskell: Why is it saying my function type is off?

I wrote a little Haskell program to find the area of a triangle, primarily to practice custom types, but it keeps throwing the following error on compile: areafinder.hs:7:4: Couldn't match expected type `Triangle' against inferred type `m b' In a stmt of a 'do' expression: putStr "Base: " In the expression: do { putS...

State Monad, why not a tuple?

I've just wrapped my head around monads (at least I'd like to think I have) and more specifically the state monad, which some people that are way smarter then me figured out, so I'm probably way of with this question. Anyway, the state monad is usually implemented with a M<'a> as something like this (F#): type State<'a, 'state> = State...

Why would one use a functional language in an otherwise Imperative project?

Why would one use a functional language in an otherwise Imperative project? ...

Zip elements with odd and even indices in a list

I want to zip even and odd elements in a list to make a list of pairs, like that: ["A", "B", "C", "D", "E", "F"] -> [("A", "B"), ("C", "D"), ("E", "F")] What is the most concise expression to do this in elegant in functional way? ...