1

This must be very simple, but I haven't been able to find an answer. How can I have org-babel output the results of multiple statements?

Ex.:

#+BEGIN_SRC scheme :exports both
(define square (lambda (x) (* x x)))
(define (smallest-divisor n)
  (define (divides? a b)
    (= (remainder b a) 0))
  (define (find-divisor n test-divisor)
    (cond ((> (square test-divisor) n) n)
          ((divides? test-divisor n) test-divisor)
          (else (find-divisor n (+ test-divisor 1)))))
  (find-divisor n 2))

;; 3 statements follow:
(smallest-divisor 199)
(smallest-divisor 1999)
(smallest-divisor 19999)
#+END_SRC

#+RESULTS:
7

I'd like results to give me all values, as in:

#+RESULTS:
199
1999
7

Please point me to an answer, should this question happen to be a duplicate.

1

The nice thing about org babel is that it has some understanding about the data types for the languages it supports. The solution is to have your scheme code return a list. This seems to work for me:

#+BEGIN_SRC scheme :results list
(define square (lambda (x) (* x x)))
(define (smallest-divisor n)
  (define (divides? a b)
    (= (remainder b a) 0))
  (define (find-divisor n test-divisor)
    (cond ((> (square test-divisor) n) n)
          ((divides? test-divisor n) test-divisor)
          (else (find-divisor n (+ test-divisor 1)))))
  (find-divisor n 2))

;; 3 statements follow:
(list
  (smallest-divisor 199)
  (smallest-divisor 1999)
  (smallest-divisor 19999))
#+END_SRC

#+RESULTS:
- 199
- 1999
- 7
  • Cool, awesome workaround. – dangom May 25 '16 at 19:26
  • 1
    This is not a workaround, this is how it's designed to work. Org-babel's understanding of data types is limited to lists and matrices, or 1- to 2-dimensional arrays. This makes it possible to create something similar to unix pipelines for tabular data. – Anselm Helbig May 25 '16 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.