[ extra module from newlisp forum ]

Started by newdep, March 02, 2004, 03:03:56 PM

Previous topic - Next topic

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 ))
-- (define? (Cornflakes))

newdep

#1
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))) )
-- (define? (Cornflakes))

nigelbrown

#2
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

#3
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

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



Lutz

newdep

#5
(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)))
-- (define? (Cornflakes))

Lutz

#6
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:



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

#7
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.
-- (define? (Cornflakes))