Select in Lisp
Common Lisp doesn’t have built-in support for concurrent programming like Go’s goroutines and channels. However, we can use threads and queues to achieve similar functionality. Here’s an example that mimics the behavior of the original Go code:
In this Lisp version:
We use the
bordeaux-threads
library for thread creation and thecl-cont
library for theselect
macro, which provides functionality similar to Go’sselect
.We define two queues
q1
andq2
instead of channels.We create two threads that sleep for 1 and 2 seconds respectively, then enqueue values to the queues.
We use a
dotimes
loop to dequeue values from both queues, similar to thefor
loop in the Go version.The
select
macro is used to wait on both queues simultaneously, printing each value as it arrives.
To run this program, you would typically save it to a file (e.g., select-example.lisp
) and load it into your Lisp environment. The output would be similar to:
Note that the total execution time is only about 2 seconds since both the 1 and 2 second sleeps execute concurrently.
This example demonstrates how to implement concurrent operations and selection between multiple sources in Common Lisp, achieving functionality similar to Go’s select statement and goroutines.