Re: Purely applicative^Wfunctional programming?



Reply7471859353@xxxxxxxxxxxxx writes:

> "(1) A purely applicative program uses the following procedures
functions
> exclusively:
> CAR CDR COND CONS EQ? LET LAMBDA LIST PAIR? QUOTE."

LET is not a primitive:

(defmacro mlet (bindings &body body)
`((lambda ,(mapcar (lambda (b) (if (consp b) (car b) b)) bindings) ,@body)
,@(mapcar (lambda (b) (if (consp b) (cadr b) nil)) bindings)))

(macroexpand-1 '(mlet ((a (* 3 5)) (b 42) (c) d) (cond (c a) (d b))))
-->
((LAMBDA (A B C D) (COND (C A) (D B))) (* 3 5) 42 NIL NIL) ;
T


LIST is not a primitive:

(list a b c) === (cons a (cons b (cons c ())))



> HOWEVER, A REPLACE-CAR is required at some level for hardware control.
> ( A.K.A. RPL-CAR, RPLCAR, etc. ( RPLCDR is/(may be) a subclass.))

No.

1- Who needs hardware? With these primitive functions you can simulate
a virtual machine on which you can implement these primitive
functions. Therefore you don't need hardware: just run it over
itself.

2- You can setup the hardware so as when you boot, it reads input and
builds a list passed as argument to the boot LAMBDA, and when this
function returns, the result is processed by the hardware and
output, like a Turing Machine. Eg. the hardware can do all the I/O
out of this minimal lisp.



(defun mini-apply (fun args)
...
)
(defun mini-eval (expr)
...
)

(defun sexprp (form)
(cond
((symbolp form) t)
((consp form) (every (function sexprp) form))
(t nil)))

(defun %make-integer (value) (cons (quote (())) value))
(defun %make-char (value) (cons (quote ((()))) value))
(defun %make-string (value) (cons (quote (((())))) value))
(defun %make-cons (value) (cons (quote ((((()))))) value))
(defun %make-symbol (value) (cons (quote (((((())))))) value))

(defun get-value (tagged-value) (cdr tagged-value))
(defun get-tag (tagged-value) (car tagged-value))

(defun %integerp (tagged-value) (eq (get-tag (%make-integer nil))
(get-tag tagged-value)))
(defun %charp (tagged-value) (eq (get-tag (%make-char nil))
(get-tag tagged-value)))
(defun %stringp (tagged-value) (eq (get-tag (%make-string nil))
(get-tag tagged-value)))
(defun %consp (tagged-value) (eq (get-tag (%make-cons nil))
(get-tag tagged-value)))
(defun %symbolp (tagged-value) (eq (get-tag (%make-symbol nil))


(defun convert-input (input)
(cond
((null input) '())
((consp input) (%make-cons (cons (convert-input (car input))
(convert-input (cdr input)))))
((integerp input) (%make-integer (%make-list integer
:initial-element '())))
((characterp input) (%make-char (%make-list (char-code integer)
:initial-element '())))
((symbolp input) (%make-symbol (map 'list (function convert-input)
(symbol-name input))))
((stringp input) (%make-string (map 'list (function convert-input) input)))
(t (error "Invalid input"))))


(defun convert-output (output)
(cond
((null output) '())
((%consp output) (cons (convert-output (car (get-value output))
(cdr (get-value output)))))
((%integerp output) (length (get-value output)))
((%charp output) (code-char (length (get-value output))))
((%symbolp output) (make-symbol (map 'string (function convert-output)
(get-value output))))
((%stringp output) (map 'string (function convert-output)
(get-value output)))
(t (error "Internal error: invalid output"))))


(defun hardware ()
(loop
(let ((program (progn (princ "Program: ") (read)))
(input (progn (princ "Input data: ") (convert-input (read)))))
(assert (sexprp program))
(let ((output (mini-apply program (list input))))
(print (convert-output))))))


> True or false?

True.

--
__Pascal Bourguignon__ http://www.informatimago.com/
Litter box not here.
You must have moved it again.
I'll poop in the sink.
.