functional-programming

How do I profile in DrScheme?

How Do I profile my functions using DrScheme? (require profile) (define (factorial n) (cond ((= n 1) 1) (else (* n (factorial (- n 1)))))) (profile factorial) The above code returns Profiling results ----------------- Total cpu time observed: 0ms (out of 0ms) Number of samples taken: 0 (once every 0ms) =============...

immutable java

immutability, any good sources on writing immutable programs in a functional way with java? shifting over to erlang - scala - clojure is not a possibility. ...

Equivalent for inject() in Python?

In Ruby, I'm used to using Enumerable#inject for going through a list or other structure and coming back with some conclusion about it. For example, [1,3,5,7].inject(true) {|allOdd, n| allOdd && n % 2 == 1} to determine if every element in the array is odd. What would be the appropriate way to accomplish the same thing in Python? ...

python list comprehensions; compressing a list of lists?

Hi, guys. I'm trying to find the most elegant solution to a problem and wondered if python has anything built-in for what I'm trying to do. What I'm doing is this. I have a list, A, and I have a function f which takes an item and returns a list. I can use a list comprehension to convert everything in A like so; [f(a) for a in A] But...

Non-determinstic choice with amb-operator

Is it possible to implement McCarthy's amb-operator for non-deterministic choice in C#? Apparently .NET lacks continuation support but yield return could be useful. Would this be possible in other static .NET-languages like F#? ...

is there a functor that derefences a (smart) pointer, upcasts it, and then calls a method on it?

I have class A: public B { ...} vector<A*> v; I want to do for_each(v.begin(), v.end(), mem_fun_deref(B::blah())); (Actually I have: vector<unique_ptr<A>> but it shouldn't matter) I need to upcast and call the member function. ...

Scheme Coding Style Questions

I am confused about the Scheme style for my code. Should I format if forms as: a. if() () () or b. if () () () or c. if () () () Should I format cond clauses as a. cond () () or b. cond () () When do I use a single ; to comment and a double ;;? ...

Functional programming in c++0x, f# style

I've been looking at the new features in c++0x and it really looks like it will be possible to program in a very functional programming style using it. I've gotten use to using the types List, Seq, Array in f# and I see no reason why their members couldn't be ported into some sort of c++0x template. It looks like microsoft is trying to...

What is the exact definition of a closure?

I've read through previous topics on closures on stackflow and other sources and one thing is still confusing me. From what I've been able to piece together technically a closure is simply the set of data containing the code of a function and the value of bound variables in that function. In other words technically the following C func...

How do I use Declarations (type, inline, optimize) in Scheme?

How do I declare the types of the parameters in order to circumvent type checking? How do I optimize the speed to tell the compiler to run the function as fast as possible like (optimize speed (safety 0))? How do I make an inline function in Scheme? How do I use an unboxed representation of a data object? And finally are any of these...

What are the things that optimize speed declaration do in CL?

What are some of the optimization steps that this command does `(optimize speed (safety 0))` Can I handcode some of these techniques in my Lisp/Scheme program? ...

Can I disassemble my code in PLTScheme?

Can I see the translated machine instruction of a scheme function like (disassemble) in LISP? ...

How does functional programming influence your coding style?

The most of programmers nowadays use OOPS concepts for software development. But some of them have exposure to functional programming too. How does functional programming influence your coding style? ...

Describe the Damas-Milner type inference in a way that a CS101 student can understand

Hindley-Milner is a type system that is the basis of the type systems of many well known functional programming languages. Damas-Milner is an algorithm that infers (deduces?) types in a Hindley-Milner type system. Wikipedia gives a description of the algorithm which, as far as I can tell, amounts to a single word: "unification." Is ...

What are the core concepts in functional programming?

In object-oriented programming, we might say the core concepts are: encapsulation inheritance, polymorphism What would that be in functional programming? ...

Real world Haskell programming

Having been an imperative developer for some years now, I had never had the urge to learn functional programming. A couple months ago at last I decided to learn Haskell. It's quite a cool language, but I'm puzzled about how an event driven real app would be programmed in such a language. Do you know of a good tutorial about it? Note: W...

How to add up the elements for a structure in Scheme/Lisp

I have an input which is of this form: (((lady-in-water . 1.25) (snake . 1.75) (run . 2.25) (just-my-luck . 1.5)) ((lady-in-water . 0.8235294117647058) (snake . 0.5882352941176471) (just-my-luck . 0.8235294117647058)) ((lady-in-water . 0.8888888888888888) (snake . 1.5555555555555554) (just-my-luck . 1.3333333333333333)))...

What is zip (functional programming?)

I recently saw some Clojure or Scala (sorry I'm not familiar with them) and they did zip on a list or something like that. What is zip and where did it come from ? ...

How do I make a module in PLT Scheme?

I tried doing this: #lang scheme (module duck scheme/base (provide num-eggs quack) (define num-eggs 2) (define (quack n) (unless (zero? n) (printf "quack\n") (quack (sub1 n))))) But I get this error: module: illegal use (not at top-level) in: (module duck scheme/base (provide num-eggs qu...

What functional language implementations allow threads to run in parallel?

The OCaml GC imposes a global lock that prevents mutators (threads) from running in parallel although they can run concurrently (interleaved). I believe the same is true of SML/NJ and MLton but not PolyML, GHC, F#, Clojure and Scala. What other functional language implementations allow threads to run in parallel? ...