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 ))
[ extra module from newlisp forum ]
[ contrib ] (contexts)
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 (contexts) (filter (lambda (x) (context? (eval x))) (symbols 'MAIN)) )
or
(define (contexts) (filter context? (map eval (symbols))) )
-
- Posts: 429
- Joined: Tue Nov 11, 2003 2:11 am
- Location: Brisbane, Australia
more pre-defines
(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)))
(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)))
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:
Lutz
(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))))
[add-on] loop or endless
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.
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.