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
(define (make-add name val)
(let (f nil)
(setq f (string "(define (" name " x) (+ " val " x))"))
(setq name (eval-string f))
name))
(make-add "sum-10" 10)
out: (lambda (x) (+ 10 x))
(sum-10 3)
out: 13
(setq j (array 256 '(-1)))
(setq str "abc")
(char (str 1))
;-> 98
(integer? (char (str 1)))
;-> true
(setf (j 98) 2)
;-> 2
(setq idx (char (str 1)))
;-> 98
(setf (j idx) 2)
;-> 2
(setf (j (char (str 1))) 2)
;-> ERR: string expected : 2
(setf (j (char (str 1))) "2")
;-> "2"
; julian day = 0 on monday 1 january 4713 B.C. (-4712 1 1)
;; @syntax (gdate-julian gdate)
;; @description Convert gregorian date to julian day number (valid only from 15 ottobre 1582 A.D.)
;; @param <gdate> gregorian date (year month day)
;; @return julian day number (int)
;; @example
;; (gdate-julian '(2019 11 11)) ==> 2458799
;; (gdate-julian '(2019 11 12)) ==> 2458800
;; (gdate-julian '(-4712 1 1)) ==> 38
(define (gdate-julian gdate)
(local (a y m)
(setq a (/ (- 14 (gdate 1)) 12))
(setq y (+ (gdate 0) 4800 (- a)))
(setq m (+ (gdate 1) (* 12 a) (- 3)))
(+ (gdate 2) (/ (+ (* 153 m) 2) 5) (* y 365) (/ y 4) (- (/ y 100)) (/ y 400) (- 32045))))
;; @syntax (jdate-julian jdate)
;; @description Convert julian date to julian day number (valid only until 4 ottobre 1582 A.D.)
;; @param <jdate> julian date (year month day)
;; @return julian day number (int)
;; @example
;; (jdate-julian '(2019 11 11)) ==> 2458812
;; (jdate-julian '(2019 11 12)) ==> 2458813
;; (jdate-julian '(-4712 1 1)) ==> 0
(define (jdate-julian jdate)
(local (a y m)
(setq a (/ (- 14 (jdate 1)) 12))
(setq y (+ (jdate 0) 4800 (- a)))
(setq m (+ (jdate 1) (* 12 a) (- 3)))
(+ (jdate 2) (/ (+ (* 153 m) 2) 5) (* y 365) (/ y 4) (- 32083))))
;; @syntax (julian-gdate jd)
;; @description Convert julian day number to gregorian date (valid only from 15 ottobre 1582 A.D.)
;; @param <jd> julian day number (int)
;; @return gregorian date (year month day)
;; @example
;; (julian-gdate 2458799) ==> (2019 11 11)
;; (julian-gdate 2458800) ==> (2019 11 12)
;; (julian-gdate (gdate-julian '(2019 11 12))) ==> (2019 11 12)
(define (julian-gdate jd)
(local (a b c d e m)
(setq a (+ jd 32044))
(setq b (/ (+ (* 4 a) 3) 146097))
(setq c (- a (/ (* b 146097) 4)))
(setq d (/ (+ (* 4 c) 3) 1461))
(setq e (- c (/ (* 1461 d) 4)))
(setq m (/ (+ (* 5 e) 2) 153))
(list
(+ (* b 100) d (- 4800) (/ m 10))
(+ m 3 (- (* 12 (/ m 10))))
(+ e (- (/ (+ (* 153 m) 2) 5)) 1))))
;; @syntax (julian-jdate jd)
;; @description Convert julian day number to julian date (valid only until 4 ottobre 1582 A.D.)
;; @param <jd> julian day number (int)
;; @return julian date (year month day)
;; @example
;; (julian-jdate 2458812) ==> (2019 11 11)
;; (julian-jdate 2458813) ==> (2019 11 12)
;; (julian-jdate 0) ==> (-4712 1 1)
(define (julian-jdate jd)
(local (a b c d e m)
(setq a 0)
(setq b 0)
(setq c (+ jd 32082))
(setq d (/ (+ (* 4 c) 3) 1461))
(setq e (- c (/ (* 1461 d) 4)))
(setq m (/ (+ (* 5 e) 2) 153))
(list
(+ (* b 100) d (- 4800) (/ m 10))
(+ m 3 (- (* 12 (/ m 10))))
(+ e (- (/ (+ (* 153 m) 2) 5)) 1))))
;; @syntax (julian-weekday jd)
;; @description Find the day of week (number) of a julian day number
;; @param <jd> julian day number (int)
;; @return day of week number (ISO: Mon=1 ... Sun=7) (int)
;; @example
;; (julian-weekday (gdate-julian '(1900 3 15))) ==> 4
;; (julian-weekday (gdate-julian '(1821 5 5))) ==> 6
;; (julian-weekday (jdate-julian '(1400 1 1))) ==> 4
(define (julian-weekday jd) (+ (% jd 7) 1))
;; @syntax (gdate-diff gdate1 gdate2)
;; @description Calculate the difference between two gregorian dates
;; @param <gdate1> first gregorian date (year month day)
;; @param <gdate2> second gregorian date (year month day)
;; @return (date1 - date2 = interval of days) (int)
;; @example
;; (gdate-diff '(2012 11 28) '(2010 4 22)) ==> 951
(define (gdate-diff gdate1 gdate2)
(- (gdate-julian gdate1) (gdate-julian gdate2)))
;; @syntax (gdate-add gdate num-days)
;; @description Adds days to a gregorian date
;; @param <gdate> gregorian date (year month day)
;; @param <num-days> days to add (int)
;; @return gregorian date (year month day)
;; @example (gdate-add '(1980 3 15) 10) ==> (1980 3 25)
(define (gdate-add gdate num-days)
(julian-gdate (+ (gdate-julian gdate) num-days)))
;; @syntax (gdate-sub gdate num-days)
;; @description Subtracts days from a gregorian date
;; @param <gdate> gregorian date (year month day)
;; @param <num-days> days to sub (int)
;; @return gregorian date (year month day)
;; @example
;; (gdate-sub '(1980 3 15) 31) ==> (1980 2 13)
(define (gdate-sub gdate num-days)
(julian-gdate (- (gdate-julian gdate) num-days)))
(pow 3 0.33)
;-> 1.436977652184852
(pow -3 0.33)
;-> 1.#IND
3^0.33 = 1.436977652184852
-3^0.33 = -1.436977652184852
(define (pow-ext x n)
(if (< x 0)
(sub 0 (pow (sub 0 x) n))
(pow x n)))
(pow-ext 3 0.33)
;-> 1.436977652184852
;(pow-ext -3 0.33)
;-> -1.436977652184852
(define (select-array arr lst-idx)
(array (length lst-idx)
(map (fn(x) (arr x)) lst-idx))
)
(setq lst '(3 5 6 7 1 9))
(setq vec (array (length lst) lst))
(select-array vec '(0 1))
;-> (3 5)
(select-array vec '(0 1 -1))
;-> (3 5 9)
(select-array vec '(-1 -2 -3 0 1 2))
;-> (9 1 7 3 5 6)
(select-array vec '(3 3 3))
;-> (7 7 7)
(select-array vec '(0 1 6))
;-> ERR: array index out of bounds in function map : 6
;-> called from user function (select-array vec '(0 1 6))
(array? (select-array vec '(4 1)))
;-> true
;List with 100000 elements (from 1 to 100000)
(silent (setq tlist (sequence 1 100000)))
;List with 10000 indexes (from 0 to 9999 randomized)
(silent (setq ind (randomize (sequence 0 9999))))
(time (select tlist ind) 100)
;-> 4312.832
(define (select-list lst lst-idx)
(array-list (select-array (array (length lst) lst) lst-idx)))
(setq lst '(3 5 6 7 1 9))
;-> (3 5 6 7 1 9)
(select-list lst '(0 1))
;-> (3 5)
(select-list lst '(0 1 -1))
;-> (3 5 9)
(select-list lst '(-1 -2 -3 0 1 2))
;-> (9 1 7 3 5 6)
(select-list lst '(3 3 3))
;-> (7 7 7)
(select-list lst '(0 1 6))
;-> ERR: array index out of bounds in function push : 6
;-> called from user function (select-array (array (length lst) lst) lst-idx)
;-> called from user function (select-list lst '(0 1 6))
(list? (select-list lst '(4 1)))
;-> true
(time (select-list tlist ind) 100)
;-> 328.041
; define an array
(setq ar (array 256 '(-1)))
; define a string
(setq str "bar")
; define an index
(setq idx (char (str 0)))
;-> 98
(number? idx)
;-> true
; update array
(setf (ar idx) 555)
;-> 555
(number? (char (str 0)))
;-> true
; update array fail
(setf (ar (char (str 0))) 555)
;-> ERR: string expected : 555
(define (user-symbols)
(local (_func _other)
(setq _func '())
(setq _other '())
(dolist (_el (symbols))
(if (and (lambda? (eval _el))
(not (= _el 'user-symbols)))
(push _el _func -1))
(if (and (not (lambda? (eval _el)))
(not (primitive? (eval _el)))
(not (protected? _el))
(not (global? _el))
(not (= _el '_func))
(not (= _el '_other))
(not (= _el '_el)))
(push _el _other -1))
)
(list _func _other)
)
)
; from a fresh REPL of newLISP
(user-symbols)
;-> ((module) ())
(define (rember-f test?)
(lambda (a l)
(cond
((null? l) '())
((test? (first l) a) (rest l))
(true (cons (first l) ((rember-f test?) a (rest l)))))))
((rember-f =) 'tuna '(shrimp salad and tuna salad))
ERR: invalid function : (test? (first l) a)
(shrimp salad and salad)