Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

so you don't like parenthesis

Name: Anonymous 2007-08-01 23:41 ID:Y7H5mdu/


(defun split-on (symb list)
    (with var pos = (position symb list)
          var before = (take pos list)
          var after = (rest (drop pos list))
       do (values before after)))
      
(defun dsplit (symb list)
    (if (member symb list)
        (with
            vars (before after) = (split-on symb list)
            do (cons before (dsplit symb after)))
        (list list)))
(defmacro $ (&rest list)
    (with var split = (dsplit '$ list)
          do (reduce (lambda (e tot) (append e (list tot))) split :from-end t)))


so now you can say stuff like
($ remove-if #'even? $ append a $ mapcar #f' list)
instead of
(remove-if #'even? (append a (mapcar #f' list)))

fairly unimpressive, I know.

(defmacro p/1 (f &rest body)
    (let ((args (gensym)))
        `(lambda (&rest ,args)
            ,(append `(apply (function ,f)) body (list args)))))

(defmacro p/ (&rest body)
    (cons 'compose (mapcar (p/1 cons 'p/1) (dsplit '$ body))))

so now you can say
(/p + 1 $ * 2)
instead of
(lambda (x) (+ 1 (* 2 x)))

(notice this syntax looks a lot like haskell partial application + function composition: f x . g y . h z)

Ok, actually, I'm not sure if this highly unhigienic macro is a good idea, but whatever. looks fun enough.

Name: Anonymous 2007-08-02 0:04 ID:vo/LODUn

also, easy to use semi-currying, just in case you want it (I really don't know why though):

(defmacro defun-as (name fn)
    `(setf (symbol-function ',name)
        ,fn))
(defun curry (f argsnum)
    (lambda (&rest args)
        (with var diff = (- argsnum (length args))
              var appl = (lambda (&rest rest-args) (apply f (append args rest-args)))
            do (if0 diff
                (apply f args)
                (curry appl diff)
                ))))
(defun-as +c (curry #'+ 2))

so
>(+c 1 2)
returns 3, as expected, and
>(funcall (+c 1) 2)
also returns 3.

Also I should check for negatives, but please forgive me, I need sleep.


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List