[ extra module from newlisp forum ]

For the Compleat Fan
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

[ extra module from newlisp forum ]

Post by newdep »

A nice enhancement?
if you have more, lets combine them in a new module? forum.lsp

Here is my contribution:


;;
;; The idea is not original but very handy and more readable
;; works on both lists and strings.
;;
;; first, last and rest exist already numbered upto ten for more use.
;;

(define (second x) ( unless (>= 1 (length x)) (nth 1 x) 'nil ))
(define (third x) ( unless (>= 2 (length x)) (nth 2 x) 'nil ))
(define (fourth x) ( unless (>= 3 (length x)) (nth 3 x) 'nil ))
(define (fifth x) ( unless (>= 4 (length x)) (nth 4 x) 'nil ))
(define (sixth x) ( unless (>= 5 (length x)) (nth 5 x) 'nil ))
(define (seventh x) ( unless (>= 6 (length x)) (nth 6 x) 'nil ))
(define (eigth x) ( unless (>= 7 (length x)) (nth 7 x) 'nil ))
(define (ninth x) ( unless (>= 8 (length x)) (nth 8 x) 'nil ))
(define (tenth x) ( unless (>= 9 (length x)) (nth 9 x) 'nil ))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

[ contrib ] (contexts)

Post by newdep »

Also the one from HPW and nigelbrown one of them , very nice...

(define (contexts) (filter (lambda (x) (context? (eval x))) (symbols 'MAIN)) )
or
(define (contexts) (filter context? (map eval (symbols))) )

nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

Post by nigelbrown »

Seeing HPW's definition I see mine was wrong - it will look in the current context while contexts are always defined in context MAIN. The correct def would be:
(define (contexts) (filter context? (map eval (symbols 'MAIN))))

Nigel

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

As I posted first: Was not my lisp, it's from Lutz from original newLISP-tk.tcl where he used it to fill the browser.
Hans-Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

... and which I changed to Nigel's shorter definition

Lutz

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

more pre-defines

Post by newdep »

(define (day-of-week?)
(nth (- (nth 1 (now)) 1)
'( "Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday")))

(define (month-of-year?)
(nth (- (nth 1 (now)) 1)
'("January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December" )))

(define (type-of-system?)
(nth (- (last (sys-info)) 1)
'("linux" "bsd" "osx" "solaris" "cygwin" "win32")))

(define (version?)
(nth 6 (sys-info)))

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

thanks for the tips Norman, here is another one frequently used:

(date (apply date-value (now))) => "Wed Mar 03 11:11:56 2004"

some times it is necessary to convert a date value back to a list:

Code: Select all

;; Convert a date-value back to a list
;;
;; example:
;; (value-date 0) => (1970 1 1 0 0)
;; (value-date 72187) => (1970 1 1 20 7)
;;
;;
(define (value-date val)
    (append
       (slice (now (+ (/ (apply date-value (now)) -60) (/ val 60))) 0 5)
       (list (% val 60)))) 
Lutz

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

[add-on] loop or endless

Post by newdep »

As lutz nicely posted, i think is a good one to have included in the
module (forum.lsp) too

;; forever loop until true, where 'true can be replaced
;; with another 'true option like 'on 'yes to make sure
;; the loop is not broken by local 'true
;
(define-macro (loop _func) (while true (eval _func)))
or
(define-macro (endless _func) (while true (eval _func)))


ps: working on another one which is a little stubburn..

Norman.

Locked