I found it is a bit hard to name intermediate variables in Prolog.
in Scheme, we could use nested expressions
(define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (+ (fib (- n 1)) (fib (- n 2))))))
in Prolog, we have to unnest them
fib(0, 0) :- !. fib(1, 1) :- !. fib(N, OUT) :- N > 1, N1 is N-1, N2 is N-2, fib(N1, F1), fib(N2, F2), OUT is F1+F2.
The unnesting makes naming intermediate variables a bit hard. Apparently,
F2 are not very good names (not readable IMO).
My question is that
Is there any convention for naming intermediate variables? e.g.
Another workaround is looking for a special symbol mechanism.
Scheme r7rs has
|...| symbol notation, so we could write:
(define (fib n) (cond ((= n 0) 0) ((= n 1) 1) (else (let* ((|n-1| (- n 1)) (|n-2| (- n 2)) (|fib_n-1| (fib |n-1|)) (|fib_n-2| (fib |n-2|))) (+ |fib_n-1| |fib_n-2|)))))