Menu

Show posts

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

Topics - kinghajj

#1
newLISP newS / newLISP and an SCM?
March 13, 2008, 10:30:05 AM
Lutz, do you think that you could start using an SCM (preferably Git) to track changes to newLISP, and hosting a public repo? I'd like to have access to the latest, cutting-edge version of newLISP to help change the code in any way I can, and I think many others would like that, too.
#2
newLISP newS / "Autocurrying?"
February 15, 2008, 11:15:37 AM
I've started to learn F# (a OCaml-like language for .NET), and one feature that really struck me is automatic currying. Here's a newLISP-syntax example of what I mean (it is not valid newLISP code):



(define (add-nums a b)
  (+ a b)
(setq add10 (add-nums 10))
(add10 3) ; => 13


Basically, when you "call" a function without supplying it every argument, it returns a curried function that expects the takes the next arguments.



Could something like this be added to newLISP? I realize that breaking current code is bad, but there could be an "autocurry" macro:



(define (add-nums a b)
  (+ a b))
(autocurry add-nums)


I've tried to implement an autocurry in newLISP, but hit a wall because it's difficult to differentiate between a "nil" passed by default and a "nil" passed by the caller.
#3
newLISP newS / RC4 Encryption for newLISP
December 24, 2007, 12:38:31 AM
http://kinghajj.ath.cx/rc4-nl.tar.bz2">//http://kinghajj.ath.cx/rc4-nl.tar.bz2


; example usage
(rc4-encrypt (rc4-encrypt "Hello!" "World"))
; => "Hello!"


RC4 is much better than a one-time pad for practical applications.



I would have implemented this is a primitive, builtin function, but the documentation doesn't cover that. It'd be much easier to do that then to depend on a library whose name is different on different platforms.
#4
I use a 64-bit Linux distro, so this suggestion might not apply to all versions of newLISP.



I've written a very simple Reverse Polish Notation calculator in C. To store numbers, the data type I chose was "long double." With this, I am able to calculate large numbers such as 2^2048 (*). In newLISP, however, even with a 64-bit build, trying to calculate just 2^1024 returns "inf," and 2^1023 only returns "8.988465674e+307." Is it possible to enable use of "long double" on at least 64-bit builds?



(*) 2^2048 =

32317006071311007300714876688669951960444102669715484032130345427524655138867890

89319720141152291346368871796092189801949411955915049092109508815238644828312063

08773673009960917501977503896521067960576383840675682767922186426197561618380943

38476170470581645852036305042887575891541065808607552399123930385521914333389668

34242068497478656456949485617603532632205807780565933102619270846031415025859286

41771167259436037184618573575983511523016459044036976132332872312271256847108202

09725157101726931323469678542580656697935045997268352998638215525166389437335543

602135433229604645318478604952148193555853611059596230656



And my program happily computes up to 2^16383 (a 4301 digit number); at 2^16384 it returns "inf." I think it's safe to say that 2^16383 is a number that will not practically be needed for some time. In cryptography, however, 2^1024 is a relatively "low" number, so I think newLISP needs to address this issue.
#5
newLISP newS / define-struct
September 14, 2007, 09:04:51 AM
Somebody mentioned wanting a defstruct macro in newLISP, so here's my attempt at implementing one.



(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)))))))


Here's an example usage.



(define-struct (point) x y z)
(setq point1 (point 2 4 6))
(println "point1 = (" (point-x point1) "," (point-y point1) "," (point-z point1) ")")
#6
newLISP newS / newLISP Database
September 12, 2007, 04:32:04 PM
I wrote this up quickly last night and this afternoon.



It's a database library written completely in newLISP. It's very simple, but I'll make it more capable in later releases. I wrote this because I didn't want to deal with SQL. I suppose I could have written a wrapper around the sqlite3 library for newLISP to hide the SQL from me, but that would have been less fun.



See the README file for general information, and the example.lsp file for example usage.



http://kinghajj.home.comcast.net/nldb.tar.bz2">//http://kinghajj.home.comcast.net/nldb.tar.bz2
#7
newLISP newS / newLISP unit tests
September 10, 2007, 03:57:41 PM
I was reading the book Practical Common Lisp, and found a great implementation of unit tests in Common Lisp. So, I thought I'd re-make them (in my own way, of course,) in newLISP.



nltests.lsp

;; 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))))))


Here's a very simple arithmetic test.

(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-*))


If you want to have fun, try making these tests impossible, then see how the errors are reported. If test-math fails, it tells you the exact hierarchy of the failed test.



Hope you find this useful.
#8
newLISP newS / Contexts as Objects
September 09, 2007, 12:25:16 AM
I wrote a post at http://kinghajj.blogspot.com/2007/09/contexts-as-objects-in-newlisp.html">//http://kinghajj.blogspot.com/2007/09/contexts-as-objects-in-newlisp.html detailing how to use contexts to implement classes and objects in newLISP. I've just discovered this--though I'm probably not the first--so I haven't found out all that you can do with this.



Hope you enjoy it.
#9
newLISP newS / (apply case) doesn't work as expected
September 08, 2007, 12:03:52 AM
These two pieces of code should be equivalent, correct? But they are not. (apply case ...) doesn't work.



(apply case '(1 (1 2)))

(case 1 (1 2))
#10
the newlisp-gs editor does not interpret tabs well. instead of defining a tab to look like 1, 2, 3 or 4 spaces, it seems to use a number above 1.5 but below 2. This is not very aesthetic, and I think that this number should be user-specified in the settings file.



The first function uses tabs, while the second uses spaces.



http://kinghajj.home.comcast.net/newlisp-gs-tab-problem.png">



By the way, what is the official name of this new editor? In windows it's called newlisp-GS, but in Linux it is newlisp-edit.
#11
newLISP newS / Should there be a backquote in newLISP?
August 29, 2007, 07:48:28 PM
In other LISPs, there is a backquote, which temporarily "undos" a quote. Here's an example:



(setq foo "bar")
(setq bar '(1 2 3 ,foo foobar)) ; => (1 2 3 "bar" foobar)


This would be really useful when writing macros. For example, here's a macro for "unless" that uses backquotes.



(define-macro (unless condition)
  (let ( (body (args)) )
    (eval '(if (not ,condition) ,body))))
#12
newLISP newS / Loop that collects results?
July 18, 2007, 10:14:39 AM
Is there a loop that returns a list of the results of each iteration? For example:



(collect (n (sequence 1 10))
    (* n 2))
; => (2 4 6 8 10 12 14 16 18 20)


I'm sure there are other ways to do this, like using map and curry. I could probably implement this as a macro, if there is no current way to do this.
#13
Anything else we might add? / Macro-macro.
July 16, 2007, 05:16:24 PM

; 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))))


Example:

(defun test (v1 &v2)
(println "Got " v1 " and " &v2))

(test 37 (+ 40 2))
; => "Got 37 and (+ 40 2)"
#14
newLISP newS / newLisp and readline
July 15, 2007, 01:28:04 PM
When I first compiled newLisp, the biggest problem I had with the console was that arrow keys did not move back and forth within the line, but instead outputted strange characters. That makes it very hard to play around and test new ideas, especially for beginners who make mistakes.



I did some browsing through the code, and found that if I enabled "readline" then my problem would be fixed. So I added the necessary text to the Makefile to enable readline. This is the Makefile I used, based on "makefile_linux64LP64_utf8", is:

# 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


My question is, shouldn't readline be enabled by default? Not all new newLispers will have knowledge in C programming and Makefiles, so they will not be able to enable it themselves.



P.S. Thanks for writing a program that compiles with no warnings. Too much FOSS has thousands of compiler warnings; you'd think that the programmers would look at those warnings and fix them, so as to make the program safer and more standard, but they don't.