Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Continuations in Clojure

I read somewhere where rich hickey said:

"I think continuations might be neat in theory, but not in practice"

I am not familiar with clojure.
1. Does clojure have continuations?
2. If no, don't you need continuations? I have seen a lot of good examples especially from this guy. What is the alternative?
3. If yes, is there a documentation?

like image 224
unj2 Avatar asked Jul 23 '09 17:07

unj2


People also ask

Does Clojure have continuations?

While Clojure does not have first-class continuations or coroutines built-in as a core feature, it is possible to implement your own. For example, core. async is a Clojure library that implements the CSP (Concurrent Sequential Processes) model. It uses a go macro to transform the code within to a state machine.

What are continuations in Lisp?

What are Continuations? A continuation can be considered a program that is frozen in action. You can save this object for as long as you like, and when you call it, it will restart the computation taking place when it was created.

What are the functions of the continuation?

A continuation is a callback function k that represents the current state of the program's execution. More precisely, the continuation k is a function of one argument, namely the value that has been computed so far, that returns the final value of the computation after the rest of the program has run to completion.


1 Answers

When talking about continuations, you’ll have to distinguish between two different kinds of them:

  • First-class continuations – Continuation-support that is deeply integrated in the language (Scheme or Ruby). Clojure does not support first-class continuations.

  • Continuation-passing-style (CPS) – CPS is just a style of coding and any language supporting anonymous functions will allow this style (which applies to Clojure too).

Examples:

-- Standard function double :: Int -> Int double x = 2 * x  -- CPS-function – We pass the continuation explicitly doubleCPS :: Int -> (Int -> res) -> res doubleCPS x cont = cont (2 * x) 
; Call print (double 2)  ; Call CPS: Continue execution with specified anonymous function double 2 (\res -> print res) 

Read continuation on Wikipedia.

I don’t think that continuations are necessary for a good language, but especially first-class continuations and CPS in functional languages like Haskell can be quite useful (intelligent backtracking example).

like image 50
Dario Avatar answered Oct 04 '22 22:10

Dario