functional-programming

A list of functions

Hello, Is there a way to make a list that holds functions? What I'm trying to do is, make a list of some arithmetic operators (+ - * /) so I can easily manipulate their order and apply them to a list of numbers. So, if I have that list, I'd use it like this: (apply (map (lambda (x) x) '(+ - * /)) '(1 2...

What's the point of using monads in an interpreter?

I recently discovered this little scala example called Simple interpreter using monads: object simpleInterpreter { case class M[A](value: A) { def bind[B](k: A => M[B]): M[B] = k(value) def map[B](f: A => B): M[B] = bind(x => unitM(f(x))) def flatMap[B](f: A => M[B]): M[B] = bind(f) } def unitM[A](a: A): M[A] = M(a...

making functions inline avoids closure?

I am reading a blogpost at: http://flyingfrogblog.blogspot.com/2009/07/ocaml-vs-f-burrows-wheeler.html a simple implementation for Burrow Wheeler compressing algorithm: # compare two strings str[i..end,0..i-1] and str[j..end,0..j-1] let cmp (str: _ array) i j = let rec cmp i j = if i=str.Length then 1 else if j=str.Length t...

Using delay/force to implement control structures in the absence of macros?

Using Scheme the programming language, how would one use delay/force to implement control structures without resorting to using macro facilities? Thank you. ...

Functional Programming in C++

Can someone guide me how do functional programming in C++? Is there some good online material that I can refer? Please note that I know about the library FC++. I want to know how to do that with C++ standard library alone. Thanks. ...

Higher-order type constructors and functors in Ocaml

Can the following polymorphic functions let id x = x;; let compose f g x = f (g x);; let rec fix f = f (fix f);; (*laziness aside*) be written for types/type constructors or modules/functors? I tried type 'x id = Id of 'x;; type 'f 'g 'x compose = Compose of ('f ('g 'x));; type 'f fix = Fix of ('f (Fix 'f));; for types but it d...

F# development and unit testing?

I just got started with F#, which is my first functional language. I have been working quasi-exclusively with C#, and enjoy a lot how F# leads me to re-think how I write code. One aspect I find a bit disorienting is the change in the process of writing code. I have been using TDD for years in C# now, and really appreciate to have unit te...

Efficiency of purely functional programming

Does anyone know what is the worst possible asymptotic slowdown that can happen when programming purely functionally as opposed to imperatively (i.e. allowing side-effects)? Clarification from comment by itowlson: is there any problem for which the best known non-destructive algorithm is asymptotically worse than the best known destruct...

What is the difference between scala self-types and trait subclasses?

Self-types seem to be important so I want to know why they are useful. From what I can gather, a self-type for a trait A: trait B trait A { this: B => } says that "A cannot be mixed into a concrete class that does not also extend B". (sidenote: I'm also seeing this fail in the REPL when mixed into an abstract class that does not e...

Monad trait in Scala

(How) is it possible to represent monads in Scala in a generic way (like the Monad typeclass in Haskell)? Is it somehow possible to define a trait Monad for this purpose? ...

How is memory-efficient non-destructive manipulation of collections achieved in functional programming?

I'm trying to figure out how non-destructive manipulation of large collections is implemented in functional programming, ie. how it is possible to alter or remove single elements without having to create a completely new collection where all elements, even the unmodified ones, will be duplicated in memory. (Even if the original collectio...

Map list onto dictionary

Is there a way to map a list onto a dictionary? What I want to do is give it a function that will return the name of a key, and the value will be the original value. For example; somefunction(lambda a: a[0], ["hello", "world"]) => {"h":"hello", "w":"world"} (This isn't a specific example that I want to do, I want a generic function li...

How to efficiently set one bit at a time in an Erlang binary whithout going imperative?

As an exercise I am working on a parallel implementation of the Sieve of Eratosthenes. As part of that I am implementing a sequence of bitmaps, using one bit per number to save memory. Reading bits one at a time appear to work fine, but setting them is slow, especially when I use large binaries. getBit(Bin, N, Size)-> R=Size-N-1, ...

What is the motivation for Scala assignment evaluating to Unit rather than the value assigned?

What is the motivation for Scala assignment evaluating to Unit rather than the value assigned? A common pattern in I/O programming is to do things like this: while ((bytesRead = in.read(buffer)) != -1) { ... But this is not possible in Scala because... bytesRead = in.read(buffer) .. returns Unit, not the new value of bytesRead. S...

User Defined Record Type in CAL?

A simple question I suspect. I have the simple function definition makePatientFixture :: [ { name :: String, age :: Int} ]; makePatientFixture = [ { name = "Dave", age = 41}, { name = "Denise", age = 45}, { name = "Cameron", age = 5} ]; I actually want to define a new type called Patient = { name :: String, age :: Int } This woul...

Architectural Thinking in Functional Languages

My Related Questions box overfloweth with functional programming questions. Having reviewed the most relevant, I'm still curious to hear opinions on the following: How do you think about structuring an application in a functional language? I'm not talking about a language-specific grammar. I'm interested in conceptual organizational pa...

Modelling / documenting functional programs

I've found UML useful for documenting various aspects of OO systems, particularly class diagrams for overall architecture and sequence diagrams to illustrate particular routines. I'd like to do the same kind of thing for my clojure applications. I'm not currently interested in Model Driven Development, simply on communicating how applica...

how many elements on list with scheme

i need to write a function which calculates how many elements on list with scheme language. for example (howMany 'a) returns 0 (howMany '(a b)) returns 1 (howMany '(a (b c))) returns 2 how can i do that? i did not want a working code, just only an idea for do that. so maybe you should consider to remove working codes. :) thank you ...

any() function in Python with a callback

The Python standard library defines an any() function that Return True if any element of the iterable is true. If the iterable is empty, return False. It checks only if the elements evaluate to True. What I want it to be able so specify a callback to tell if an element fits the bill like: any([1, 2, 'joe'], lambda e: isinstance(e,...

Idiomatic clojure for progress reporting?

How should I monitor the progress of a mapped function in clojure? When processing records in an imperative language I often print a message every so often to indicate how far things have gone, e.g. reporting every 1000 records. Essentially this is counting loop repetitions. I was wondering what approaches I could take to this in cloj...