4

I want to write a scheme program in an org file. I want to be able to execute the #+SRC blcoks and see the result in #+RESULT: block. However, when I execute the #+SRC_BEGIN/END I get no output. Instead I get a guile REPL and that's it.

These are the config I have:

(setq geiser-default-implementation 'guile)


(org-babel-do-load-languages
'org-babel-load-languages
'((scheme . t)
 (emacs-lisp . t)
 (ruby . t)
 (R . t)
 (python . t)
 (C . t)
 (sh . t)))

There's an interesting detail. When I execute this code

#+SRC_BEGIN
(write "hello")
#+END_SRC

I get this output in the org file:

#+RESULTS:
[blank here]

And another buffer with this:

=> #<unspecified>


"hello world";; -*- geiser-scheme-implementation: guile -*-
(write "hello world")

But the following program produces no output at all, not even the empty #+RESULT: block, except for a new guile REPL with nothing loaded into it.

This is the program (from SICP):

#+NAME rules
#+BEGIN_SRC scheme :tangle yes :noweb yes
(define (deriv exp var)
  (cond ((constant? exp var) 0 )
        ((same-var? exp var) 1)
        ((sum? exp)
         (make-sum (deriv (A1 exp) var)
                   (deriv (A2 exp) var)))
        ((product? exp) 
         (make-sum (make-product (M1 exp)
                                 (deriv (M2 exp) var))
                   (make-product (deriv (M1 exp) var)
                                 (M2 exp)))))) 
#+END_SRC

#+BEGIN_SRC scheme :tangle yes :noweb yes

(define atom?
  (lambda (x)
    (and (not (pair? x)) (not (null? x)))))


(define (constant? exp var)
  (and (atom? exp)
       (not (eq? exp var))))


(define (same-var? exp var)
  (and (atom? exp)
       (eq? exp var)))


(define (sum? exp)
  (and (not (atom? exp))
       (eq? (car exp) '+)))


(define (make-sum a1 a2)
  (list '+ a1 a2))


(define A1 cadr) ;;TODO rewrite these as lambda expressions (lambada (x) (car (cdr x)). something like that.


(define A2 caddr) 


(define (product? exp)
  (and (not (atom? exp))
       (eq? (car exp) '*)))


(define (make-product m1 m2)
  (list '* m1 m2))


(define M1 cadr)


(define M2 caddr)


(define foo '(+ (* a (* x x))
               (* (* b x)
                  c)))


<<rules>>


(deriv foo 'x)


#+END_SRC

Can someone tell me why the program isnt executed and instead a guile REPL is opened for some reason?

Jenia Ivanov
  • 427
  • 4
  • 14
  • Re' `unspecified`: you need to specify the language of the `src` block (otherwise Babel doesn't know what interpreter to use). `:tangle` and `:noweb` are features of literate programming, which you may not need at this moment. What you do need is to make sure that you enabled Scheme using `org-babel-do-load-languages`. – wvxvw Jul 25 '15 at 19:17
  • thanks. I edited the question to add my `org-babel-do-load-languages` – Jenia Ivanov Jul 25 '15 at 19:28
  • The code, the way you have it written, is supposed to be tangled (its pieces will be joined together and printed into a file). You could do the tangling by calling `M-x org-babel-tangle` or by pressing `C-c C-v t` (same thing). See this: http://orgmode.org/manual/noweb.html for more options. – wvxvw Jul 25 '15 at 19:49
  • okay thanks. and how can I immediately execute that code? is it possible to simply open the file and run the scheme interpreter on it? – Jenia Ivanov Jul 25 '15 at 21:07
  • actually I take the last comment back. It a niece feature to tangle and print it to another file. But what I really want, is org-mode to tangle it behind the scenes a print the result of the execution into this file. Is that possible? – Jenia Ivanov Jul 25 '15 at 21:31
  • To try to make sense of it all: in your `(write "hello")` attempt, the `write` function doesn't return anything. This is why the result is `#`. If you wanted it to be `hello`, you need to add a header argument `:results output` to the source block definition. Otherwise it will print the value returned from the last form, serialized to string. Try replacing `(write "hello")` with `(+ 2 2)` to see the result printed. – wvxvw Jul 26 '15 at 07:30
  • And as for Geiser... I've tried to debug your code, and there are many problems with Geiser (not the code). First, it tries to check for balanced parenthesis, but you have unbalanced parenthesis in the comment (near `lambda`), this will fail, but it also inserts the result after the point, instead of inserting it in the proper place and some other minor issues. All in all, you probably want to write to Geiser mainainers giving them this code as an example and ask them to fix the problems. – wvxvw Jul 26 '15 at 08:01

1 Answers1

1

But what I really want, is org-mode to tangle it behind the scenes a print the result of the execution into this file.

For that you need the :results directive as shown below.

BEGIN_SRC scheme :tangle yes :noweb yes :results output

See babel documentation for details of whether to use value instead of output in the :results directive. Value is good if want only the last statement's output.

Emacs User
  • 5,553
  • 18
  • 48