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

Messages - kinghajj

#1
newLISP newS /
March 13, 2008, 09:40:23 PM
http://repo.or.cz/w/newlisp.git">//http://repo.or.cz/w/newlisp.git



Homepage of the newLISP development Git mirror.



If you have Git installed, use this to get the a clone of the repo:


git clone git://repo.or.cz/newlisp.git

Then use this in the repo directory to receive updates:


git pull

I'll update this whenever Lutz releases a new development version.
#2
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.
#3
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.
#4
newLISP newS /
December 24, 2007, 11:51:48 AM
Those are hash functions, not encryption algorithms, but they are still useful.
#5
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.
#6
newLISP newS /
November 10, 2007, 04:14:47 PM
I made some improvements to my define-class module.



  • -The functions documented with newlispdoc.

    -Instead of "inherits", the keyword is now "mixin" or "mixins".

    -Each class can now mixin more than one other class.



http://kinghajj.home.comcast.net/define-class.tar.bz2">//http://kinghajj.home.comcast.net/define-class.tar.bz2
#7
newLISP newS /
November 08, 2007, 09:01:49 PM
OK, Here's the code to my "define-class" macro. There's one little issue with the attribute setters, in that they do not modify the original data, but rather make a new object with the changed attribute. However, because newLISP encourages this sort of style, maybe it will stay this way.



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; define-class.nlsp
;; by Samuel Fredrickson <kinghajj>
;; Version 0.1, 2007-11-08
;;
;; This "macro" (it's really an entire context) lets newLISP programmers write
;; classes in an easier way. The classes and objects produced by this class
;; follow the standard set in
;; http://www.alh.net/newlisp/phpbb/viewtopic.php?t=1955 by Lutz, and uses some
;; techniques by Michael in that same thread.

; The object-colon macro, by Lutz and Michael. This might become a builtin macro
; in the next version of newLISP, and this will be removed if that happens.
(define-macro (: _func _obj)
(let (_data (eval _obj))
(apply (sym _func (_data 0)) (cons _data (map eval (args))))))

(context 'define-class)

; In Michael's example of "FOOP", he uses defaults in constructor arguments so
; that objects can be created without specifying any arguments. Because that is
; valid newLISP code and has good uses, this function takes the variable names
; and their defaults and extracts just the variable names.
(define (extract-class-vars vars)
(map
(lambda (var)
(if (list? var)
(first var)
(if (symbol? var)
item)))
vars))

; Tests if a list is a method.
(define (method? meth?)
(or
(= (first meth?) 'define)
(= (first meth?) 'define-macro)))

; Extracts methdods from a list.
(define (extract-methods lst)
(filter method? lst))

; Tests if a list specifies a superclass.
(define (inherits? inh?)
(= (sym (first inh?)) 'inherits))

; Returns the name of the superclass.
(define (extract-superclass lst)
((filter inherits? lst) 0 1))

; Fills in missing parts of a method.
(define (mess-up-method method)
(letn
((method-type (first method))
(method-name (sym (method 1 0) class-name))
(method-args (cons method-name (1 (method 1))))
(method-body (2 method)))
(cons method-type (cons method-args method-body))))

; Creates the class context and its constructor.
(define (construct-class)
(let
((constructor-name (cons (sym class-name class-name) class-vars-plain)))
(if class-superclass
(new class-superclass class-name)
(context class-name))
(eval
(list
'define
constructor-name
(cons 'list (cons class-name class-vars))))))

; Creates a getter/setter function for a variable.
(define (create-getter-setter var idx)
(let
((getter-setter-name (sym var class-name)))
(set getter-setter-name
(expand
'(lambda-macro (self value)
(if (setq value (eval value))
(set-nth idx (eval self) value)
((eval self) idx))) 'idx))))

; This is the meat of the macro.
(define-macro (define-class:define-class init)
(letn
((clargs (args))
(class-name (init 0))
(class-superclass (extract-superclass clargs))
(class-vars-plain (1 init))
(class-vars (extract-class-vars class-vars-plain))
(class-methods (map mess-up-method (extract-methods clargs))))
(construct-class)
(map eval class-methods)
(dolist (var class-vars)
(create-getter-setter var (+ $idx 1)))))


Here is an example usage, based on Michael's code. The main difference between this and the idea I posted yesterday is that methods here do not implicitly take self, so it must be specified, a la Python.



;; Example usage of define-class, based on code by Michael.
(load "define-class.nlsp")

(define-class (displayable)
(define (string self)
(string self))
(define (print self)
(println (:string self))))

(define-class (point (x 0) (y 0))
(inherits displayable)
(define (string self)
(string "(" (:x self) ", " (:y self) ")"))
(define (move self dx dy)
(point:+ self (point dx dy)))
(define (+) (point:apply + (args)))
(define (-) (point:apply - (args)))
(define (*) (point:apply * (args)))
(define (apply op ags)
(cons point (apply map (cons op (map (fn (e) (1 e)) ags))))))

(define-class (segment (a (point)) (b (point)))
(inherits displayable)
(define (string self)
(string (:string (:a self)) " to " (:string (:b self))))
(define (move self dx dy)
(segment (:move (:a self) dx dy) (:move (:b self) dx dy))))

(define-class (triangle (ab (segment)) (bc (segment)) (ca (segment)))
(inherits displayable)
(define (string self)
(string
(:string (:ab self)) ", "
(:string (:bc self)) ", "
(:string (:ca self))))
(define (move self dx dy)
(triangle
(:move (:ab self) dx dy)
(:move (:bc self) dx dy)
(:move (:ca self) dx dy))))

(println "nMaking three points:")
(:print (set 'a (point)))
(:print (set 'b (point 20 0)))
(:print (set 'c (point 10 5)))

(println "nPoint addition, subtraction, and multiplication:")
(:print (point:+ a b c))
(:print (point:- a b c))
(:print (point:* (point 2 43) '(point 22 1) c))

(println "nMaking a triangle:")
(:print (set 'tri (triangle (segment a b) (segment b c) (segment c a))))

(println "nMove the triangle by delta (30 5):")
(:print (set 'tri (:move tri 30 5)))

(println)
#8
newLISP newS /
November 07, 2007, 11:36:58 PM
Looking at Michael's code, I noticed that there are many redundancies with making classes. How about a macro to define classes?


;; D I S P L A Y A B L E
(define-class (displayable)
   (define (string)
      (string self))
   (define (print)
      (println (:string self))))

;; P O I N T
(define-class (point (x 0) (y 0))
   (inherits displayable)
   (define (string)
      (string (:x self) "@" (:y self)))
   (define (move dx dy)
      (point:+ self (point dx dy)))
   (define (+) (point:apply + (args)))
   (define (-) (point:apply - (args)))
   (define (*) (point:apply * (args)))
   (define (apply op ags)
      (cons point (apply map (cons op (map (fn (e) (1 e)) ags))))))

;; S E G M E N T
(define-class (segment (a (point)) (b (point)))
   (inherits displayable)
   (define (string)
      (string (:string (:a self)) " to " (:string (:b self))))
   (define (move dx dy)
      (segment (:move (:a self) dx dy) (:move (:b self) dx dy))))

;; T R I A N G L E
(define-class (triangle (ab (segment)) (bc (segment)) (ca (segment)))
   (inherits displayable)
   (define (string)
      (string
         (:string (:ab self)) ", "
         (:string (:bc self)) ", "
         (:string (:ca self))))
   (define (move dx dy)
      (triangle
         (:move (:ab self) dx dy)
         (:move (:bc self) dx dy)
         (:move (:ca self) dx dy))))


As you can see, in my idea, all methods take an implicit "self" argument, and getters/setters for all object variables are automatically defined. There is also a simple way to specify inheritance.



I've begun work on a macro to do this, but it might take me a while. Perhaps this macro could be done better in the C API.
#9
newLISP newS /
September 16, 2007, 01:01:35 AM
Quote from: "newBert"
Quote from: "kinghajj"I got a segmentation dump when trying dostring.



> (dostring (c "hello") c)
Segmentation fault (core dumped)

Maybe :
> (dostring (c "hello") (println c))
works better !

;)


But the point is that the code I wrote, while useless, should not crash the program.
#10
newLISP newS /
September 15, 2007, 11:53:54 PM
I got a segmentation dump when trying dostring.



> (dostring (c "hello") c)
Segmentation fault (core dumped)
#11
newLISP newS /
September 15, 2007, 02:02:43 PM
64-bit OSes are usually near-100% compatible with their 32-bit counterparts. The 64-bit versions of Windows, for example, have a special library that re-implements 32-bit Windows on 64-bit Windows, allowing you to run any 32-bit program. I'm sure that Mac OS X will be no different.



The main advantage with 64-bit processors is more memory and more efficient calculations. 64-bit processors have a theoretical limit of 17,179,869,184 GiB of memory, and because the word length is twice as large as a 32-bit processor's, bigger numbers can be more easily transfered. That makes things like video editing, encryption, etc. faster.



For Macs, I'm surprised a 64-bit version of Mac OS X hasn't been out already, since all current Mac processors since the switch to Intel have been 64-bit.
#12
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.
#13
newLISP newS /
September 14, 2007, 10:27:31 AM
Thanks, Lutz. I was wondering this morning how to do that, and using a macro did come to mind, but I was too busy to work on that.
#14
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) ")")
#15
newLISP newS /
September 13, 2007, 02:06:55 AM
Wow, that is a much cleaner version of truncate! Here's a more concise version.



(define (truncate lst size)
  (let ((len (length lst)))
    (if (<= len size)
      (append lst (dup nil (- size len)))
      (chop lst (- len size)))))


I'll use this in the next release. Thanks.