This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
Show posts Menu
(define (add-nums a b)
(+ a b)
(setq add10 (add-nums 10))
(add10 3) ; => 13
(define (add-nums a b)
(+ a b))
(autocurry add-nums)
; example usage
(rc4-encrypt (rc4-encrypt "Hello!" "World"))
; => "Hello!"
(define (truncate lst size)
(let ((len (length lst)))
(if (<= len size)
(append lst (dup nil (- size len)))
(chop lst (- len size)))))
(define-macro (define-struct params)
(let ((items (args))
(struct-name (params 0)))
(eval (expand (quote
(define (struct-name)
(truncate (args) n)))
(list
(list 'struct-name struct-name)
(list 'n (length items)))))
(dolist (item items)
(eval (expand (quote
(define (item-getter-setter struct value)
(if value
(set-nth idx struct value)
(struct idx))))
(list
(list 'item-getter-setter (sym (string struct-name "-" item)))
(list 'idx $idx)))))))
(define-struct (point) x y z)
(setq point1 (point 2 4 6))
(println "point1 = (" (point-x point1) "," (point-y point1) "," (point-z point1) ")")
;; nltests.lsp -- testing macros
;; Copyright (C) 2007 Samuel Fredrickson.
(context 'nltests)
; used to report which tests and sub-tests failed.
(setq *test-name* '())
; set to true if you want tests to report which tests failed
(setq *report-failures* true)
; set to true if you want tests to report which tests passed
(setq *report-passes* nil)
; prints a failure if allowed
(define (report-failure test)
(if *report-failures*
(println *test-name* ": " test " FAILED!"))
nil)
; prints a pass if allowed
(define (report-pass test)
(if *report-passes*
(println *test-name* ": " test " passed"))
true)
; reports the status of a test.
(define (report test)
(if (eval test)
(report-pass test)
(report-failure test)))
; tests tests, returns nil on fail and true on pass.
(define-macro (check tests)
(apply and (map report tests)))
(context 'MAIN)
; defines a new test.
(define-macro (define-test params)
(eval (expand
'(define params
(let ((nltests:*test-name* (append nltests:*test-name* '(_name))))
(nltests:check tests)))
(list
(list 'params params)
(list '_name (params 0))
(list 'tests (args))))))
(load "nltests.lsp")
(define-test (test-+)
(= (+ 5 9) 14)
(= (+ 4 3) 7))
(define-test (test-*)
(= (* 4 5) 20)
(= (* 3 2) 6))
(define-test (test-math)
(test-+)
(test-*))
(setq foo "bar")
(setq bar '(1 2 3 ,foo foobar)) ; => (1 2 3 "bar" foobar)
(define-macro (unless condition)
(let ( (body (args)) )
(eval '(if (not ,condition) ,body))))
(collect (n (sequence 1 10))
(* n 2))
; => (2 4 6 8 10 12 14 16 18 20)
; This macro provides a classic defun.
; If I remember CL correctly, if an argument is prefixed with &, then it is not
; evaluated; that is hom CL makes macros. This macro checks if the argument name
; has a & at the start, and if it does it does not evaluate it. This makes it
; easy if you want to write a macro that needs no evaluate some arguments.
(define-macro (defun _name _args)
(let (_body (args))
; go through to arguments
(dolist (_arg _args)
; evaluate argument unless prefixed with &
(unless (= (first (string _arg)) "&")
(push (list 'eval _arg) _body)))
; create macro function
(set _name (append (lambda-macro) (list _args) _body))))
(defun test (v1 &v2)
(println "Got " v1 " and " &v2))
(test 37 (+ 40 2))
; => "Got 37 and (+ 40 2)"
# makefile for newLISP v. 9.x.x UTF-8 on 64 bit LINUX and 64-bit memory pointers tested on AMD64
#
# Note, that readline support may require different libraries on different OSs
#
OBJS = newlisp.o nl-symbol.o nl-math.o nl-list.o nl-liststr.o nl-string.o nl-filesys.o
nl-sock.o nl-import.o nl-xml.o nl-web.o nl-matrix.o nl-debug.o nl-utf8.o pcre.o
CFLAGS = -Wall -pedantic -Wno-uninitialized -Wno-strict-aliasing -Wno-long-long -c -O3 -DSUPPORT_UTF8 -DLINUX -DNEWLISP64 -DREADLINE
CC = gcc
default: $(OBJS)
$(CC) $(OBJS) -g -lm -ldl -lreadline -o newlisp
strip newlisp
.c.o:
$(CC) $(CFLAGS) $<
$(OBJS): primes.h protos.h makefile_linux64LP64_utf8