view lists.lisp @ 0:81137f478b5c

initial examples and some of my own functions/macros
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 04 Oct 2019 11:49:00 +0200
parents
children a3dca97e5f3b
line wrap: on
line source

;; Reverse list

(defun my-reverse (l)
  (if (= 0 (length l)) '() (append (my-reverse (cdr l)) (list (car l)))))

(defun my-foldl (f init l) ;; tail-recursive but not lazy
  (if l (my-foldl f (funcall f init (car l)) (cdr l)) init))

(defun my-foldr (f l init) ;; lazy but not tail-recursive
  (if l (funcall f (car l) (my-foldr f (cdr l) init))))

(defun my-map (f l)
  (if l (cons (funcall f (car l)) (my-map f (cdr l))) nil))

(defun my-mapf (f l)
  (defun folder (acc e) (cons (funcall f e) acc))
  (my-reverse (my-foldl #'folder nil l)))

(defun my-range (from to)
  (defun _my-range (acc from to) ;; for tail recursion
    (if (eql from to) acc (_my-range (cons to acc) from (- to 1))))
  (_my-range () (- from 1) (- to 1)))

(defun my-append (a b) ;; not tail-recursive
  (if a (cons (car a) (my-append (cdr a) b)) b))

(defmacro my-dotimes ((var up-to) &rest body)
  `(mapcar #'(lambda (,var) ,@body) (my-range 0 ,up-to)))

(defmacro my-let (bindings &rest body)
  (defun _names () (mapcar #'car bindings))
  (defun _vals () (mapcar #'cadr bindings))
  `(funcall #'(lambda (,@(_names)) ,@body) ,@(_vals)))

(defmacro my-let* (bindings &rest body)
  (if (not bindings)
      `(funcall (lambda () ,@body))
      `(my-let (,(car bindings)) (my-let* ,(cdr bindings) ,@body))))