functional-programming

While or Tail Recursion in F#, what to use when?

Ok, only just in F# and this is how I understand it now : Some problems are recursive in nature (building or reading out a treestructure to name just one) and then you use recursion. In these cases you preferably use tail-recursion to give the stack a break Some languagues are pure functional, so you have to use recursion in stead ...

How to write an enumeration of all computable functions?

Motivation: I'd like to be able to use toy functional programming in languages without first-order functions, by using natural numbers instead of functions. A universal function is a function f : N -> (N -> N), equivalently f : N * N -> N that enumerates all possible computable functions. In other words, there's a number k such that f(k...

Is there a scala identity function?

If I have something like a List[Option[A]] and I want to convert this into a List[A], the standard way is to use flatMap: scala> val l = List(Some("Hello"), None, Some("World")) l: List[Option[java.lang.String]] = List(Some(Hello), None, Some(World)) scala> l.flatMap( o => o) res0: List[java.lang.String] = List(Hello, World) Now o =>...

SML/NJ - Pattern Matching an Dynamic Typing

Is it possible to write functions with dynamically typed input parameters? I tried pattern matching, but apparently it does not work like this. I wish to do something like this: fun firstStr (0,n:string) = n | firstStr (b:string,n:string) = if b>n then n else b; Thank you. ...

Is it possible to write tacit functions in F#

Tacit or point-free style programming allows one to create functions without regard to their arguments. Can this be done in F#? ...

When should one use actors to solve a concurrency problem?

I have been looking at the Actor Model for a while now. To me it seems to be just another approach towards concurrent programming with its own upsides and downsides. It certainly does not guarantee deadlock-free environment. A can wait for a message from B while B waits for a message from A. I don't know if we can say the actors approac...

Why the tuple type can not be inferred in the list recursion?

I want to refine the raw text by using regular expression, given a list of (patten,replacement) tuple. I tried to use the patten matching on the list element but failed, the error showed that "This expression was expected to have type string * string list but here has type 'a list". How can I fix this problem? Thanks a lot. Codes ar...

Please refactor my macro in Scheme

I am learning hygiene and I tried to make a simple for loop in Scheme. I want to support three kinds of constructs as shown in example below (for i = 1 : (< i 4) : (++ i) (printf "Multiplication Table for ~s\n" i) (for j = 1 to 5 (printf "~s * ~s = ~s\n" i j (* i j)))) I want to also support for loops with filters like this:...

Erlang (Functional Programming) vs Object Oriented Programming in terms of thinking

I am learning Erlang and I am trying to create a very sample blog program. However my mind currently is trapped in the OO world (var p = new Post(); p.Title = ""; p.Save();). I would like to understand some basic thinkings in Erlang. Instead of creating Post object what I should do in terms of data structure (p.Title, p.DateCreated, p.Bo...

Can somebody please explain this continuation in scheme?

I am learning continuations but I can't wrap my head around this code. Why does it go into infinite loop? (let ((cont #f)) (call/cc (lambda (k) (set! cont k))) (cont #f)) ...

Clojure: How to create a function at runtime

I want to generate a fn totally at runtime (i.e. the name and the arg symbols are decided at runtime, not in code) What's the best way to achieve this ? For example how can I implement the following function ? (defn gen-fn [name arg-symbols body] ... ... which would be used like this: (gen-fn "my-func-name" (symbol "x") (symbol "y...

Can I have a co-routine of three functions using continuations in Scheme?

Is it possible to add another function procC in here so that the sequence of evaluation is procA->procB->procC->procA ... ? (define (procA another-fun) (let loop ((n 5)) (display "In Proc A \n") (set! another-fun (call/cc another-fun)) (when (> n 0) (loop (- n 1))))) (define (procB another-fun) (let loop ((n 5)) ...

How to make Haskell compute the correct polymorphic type?

I just realized how useful the little on-function can be. Ex: orderByLength = sortBy (compare `on` length) But unfortunately, the inferred types can be somewhat counter-intuitive. According to the very definition f `on` g = \x y -> f (g x) (g y) one could e.g. replace (==) `on` length with \x y -> (length x) == (length y) B...

How to use 'oneof' in quickCheck (Haskell)

I am trying to write a prop that changes a Sudoku and then checks if it's still valid. However, I am not sure how to use the "oneof"-function properly. Can you give me some hints, please? prop_candidates :: Sudoku -> Bool prop_candidates su = isSudoku newSu && isOkay newSu where newSu = update su aBlank aCandidate aCandi...

Can I start web development with a functional language?

I'd like to start doing some forms of web development, with the aim of building web apps that could eventually grow into start-up products. And for a long time, I have been very curious about functional programming, and somewhere in my heart secretly believing that higher up you go in the abstraction level, the more power you have (hidde...

Is erlang considered managed or do you have to handle memory/pointers?

Is erlang considered managed or do you have to handle memory/pointers? ...

Book that teaches functional programming

Is there a book that teaches functional programming, without being a tutorial for an FP language? I am looking for more theoretical stuff. ...

Doubly Linked List in a Purely Functional Programming Language

How does one go about doing doubly linked lists in a pure functional language? That is, something like Haskell where you're not in a Monad so you don't have mutation. Is it possible? (Singly linked list is obviously pretty easy). ...

What causes this Standard-ML type error?

I was trying to make a tail-recursive version of this very simple SML function: fun suffixes [] = [[]] | suffixes (x::xs) = (x::xs) :: suffixes xs; During the course of this, I was using type annotations on the paramaters. The following code shows this, and causes a type error (given below), whereas if I simply remove the type annot...

When would it make sense to pass a function to a function?

Ok, so it is possible to pass a function to another function. http://stackoverflow.com/questions/148982/passing-a-function-to-another-function-in-actionscript-3 This is obviously very powerful, but a more important question is, when would it make sense to do so, as there are performance overheads whenever you call another function? ...