Name:
Anonymous
2012-07-23 3:49
JUST POST SOME FUCK ING LISP!!!!
(defun generate-expr (&key fn-list term-list max-depth method)
(if (or (= max-depth 0)
(and (eq method :grow)
(< (random 1.0)
(/ (length term-list) (+ (length term-list) (length fn-list))))))
(nth (random (length term-list)) term-list)
(let* ((fn (nth (random (length fn-list)) fn-list))
(arity (length `(swank-backend:arglist ,fn))))
(cons fn (loop for i from 0 to arity
collect (generate-expr :fn-list fn-list :term-list term-list :max-depth (1- max-depth) :method method))))))
Name:
Anonymous
2012-07-24 0:31
;; a linear-combination is a list of terms, sorted by term<?
(def (linear-combination-to-normal-form l)
(filter (lambda (t1) (not (eq? (term-scalar t1) 0)))
(merge-sort-uniq term<? term=? combine-terms l)))
(def (linear-combination-sum l1 l2)
(filter (lambda (t1) (not (eq? 0 (term-scalar t1))))
(merge-uniq term<? term=? combine-terms l1 l2)))
(def (linear-combination-product l1 l2)
(evenly-reduce linear-combination-sum
(map (lambda (l1-term)
(linear-combination-to-normal-form
(map (lambda (l2-term)
(multiply-terms l1-term l2-term))
l2)))
l1)))
(def (linear-combination-invert l)
(map (lambda (current-term)
(term (- (term-scalar current-term))
(term-product-list current-term)))
l))
;; nested sums will remain in the product.
(def (product->term args)
(letrec ((helper (lambda (args accumulation-scalar accumulation-product-list)
(cond ((null? args) (cond ((null? accumulation-product-list) (number->term accumulation-scalar))
(else (term accumulation-scalar (merge-sort expr<? accumulation-product-list)))))
((number? (car args)) (helper (cdr args)
(* accumulation-scalar (car args))
accumulation-product-list))
(else (helper (cdr args) accumulation-scalar (cons (car args) accumulation-product-list)))))))
(helper args 1 '())))
(def (product->linear-combination args)
(evenly-reduce linear-combination-product (map expr->linear-combination args)))
(def (number->term num)
(term num '()))
(def (symbol->term symbol)
(term 1 (list symbol)))
(def (sum->linear-combination args)
(evenly-reduce linear-combination-sum (map expr->linear-combination args)))
(def (expr->linear-combination e)
(cond ((number? e) (list (number->term e)))
((symbol? e) (list (symbol->term e)))
((list? e) (cond ((null? e) '())
((eq? (car e) '*) (product->linear-combination (cdr e)))
((eq? (car e) '+) (sum->linear-combination (cdr e)))
((eq? (car e) '-) (linear-combination-invert (expr->linear-combination (cadr e))))))))