Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute LISP Online

(defun my_map (l f)
    (if l (cons (funcall f (first l)) (my_map (rest l) f)) nil)
)

(defun my_filter (l f)
    (if l
        (if (funcall f (first l))
            (cons (first l) (filter (rest l) f))
            (my_filter (rest l) f))
        nil)
)

(defun my_reduce (l f)
    (let* ((o (first l))
           (r (rest l))
           (s (first r)))
          (if (not o) nil
            (if (not s) o 
                (my_reduce (cons (funcall f o s) (rest r)) f)
))))

(defun gen_exp_ser (x nterm n term)
    (let ((xterm (/ (* term x) (+ 1 n))))         
        (cons term (if (> n nterm) nil (gen_exp_ser x nterm (+ 1 n) xterm))))            
)

(defun cmp_exp_ser (l)
    (gen_exp_ser l 10 0 1)
)

(format t "~{~d~%~} ~5$~%" (cmp_exp_ser 0) (my_reduce (cmp_exp_ser 0) (lambda (x y) (+ x y))))
(format t "~{~d~%~} ~5$~%" (cmp_exp_ser 1) (my_reduce (cmp_exp_ser 1) (lambda (x y) (+ x y))))
(format t "~{~d~%~} ~5$~%" (cmp_exp_ser 2) (my_reduce (cmp_exp_ser 2) (lambda (x y) (+ x y))))

(format t "~{~5$~%~}" (my_map '(0 1 2) (lambda (x) (my_reduce (cmp_exp_ser x) (lambda(a b) (+ a b))))))

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.