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

Topics - cameyo

#21
(println "Happy New Year " (- 1 (- 2 (- 3 (* 4 (- 5 (+ 6 (* 7 8 9))))))))
#22
A library (181 functions) for recreational mathematics: yo.zip

https://github.com/cameyo42/newLISP-Note">//https://github.com/cameyo42/newLISP-Note
#23
newLISP in the real world / Hash-map e contexts
September 09, 2021, 08:16:14 AM
How to filter only the contexts that represent a hash-map?

Example:

(dolist (_el (symbols))

   (if (context? (eval _el))

       (println (eval _el) {} (length (eval _el)))))

; -> Class 2

; -> MAIN 0

; -> Tree 0

; -> demo 0

; -> myHash 0



(dolist (_el (symbols))

   (if (and (context? (eval _el))

       (not (= _el 'MAIN))

       (not (= _el 'Tree))

       (not (= _el 'Class)))

       (println (eval _el) {} (eval-string (string "(" _el ")")))))

; -> demo ()

; -> myHash (("1" 1) ("20" 20) ("57" 57) ("59" 59) ("81" 81))



Is there a way that doesn't use "eval-string" to display / count the values of a context representing a hash-map? "
#24
Italian translation of "Code Patterns in newLISP":

https://github.com/cameyo42/newLISP-Note">//https://github.com/cameyo42/newLISP-Note

cameyo
#25
newLISP in the real world / sort list of points
May 28, 2021, 11:33:23 AM
How to sort a list of points (x y) with x ascending and y descending?

I have tried this, but don't work:

(define (comp x y) (and (>= (last x) (last y)) (<= (first x) (first y))))

I can't post all the code... Internal Server Error.
#26
newLISP in the real world / List of indexes
May 12, 2021, 05:36:17 AM
How to create a list of indexes of all the elements of a generic list?

Example:

Input: (setq lst '(1 (2 (3 4)) (5 6)))

(lst 0)

1

(lst 1)

(2 (3 4))

(lst 1 0)

2

(lst 1 1)

(3 4)

(lst 1 1 0)

3

(lst 1 1 1)

4

(lst 2)

(5 6)

(lst 2 0)

5

(lst 2 1)

6



Output: List of indexes of lst

((0) (1) (1 0) (1 1) (1 1 0) (1 1 1) (2) (2 0) (2 1))

or

(0 1 (1 0) (1 1) (1 1 0) (1 1 1) 2 (2 0) (2 1))



p.s. can't post formatted code on forum (Internal Server Error)
#27
newLISP in the real world / IDE for newLISP
April 08, 2021, 06:33:49 AM
On the web I found this IDE for newLISP:

https://github.com/DexterLagan/newIDE">//https://github.com/DexterLagan/newIDE

by Dexter Santucci



cameyo
#28
Function to create a function with name and parameters:
(define (make-add name val)
  (let (f nil)
    (setq f (string "(define (" name " x) (+ " val " x))"))
    (setq name (eval-string f))
  name))

Creating a function
(make-add "sum-10" 10)
out: (lambda (x) (+ 10 x))

Using the created function
(sum-10 3)
out: 13

Do you know of another way (instead of using strings) to create a function with another function?
#29
I have the following situation:
(setq j (array 256 '(-1)))
(setq str "abc")
(char (str 1))
;-> 98
(integer? (char (str 1)))
;-> true

Now i want to update a value of the array j:

Using the number 98 works:
(setf (j 98) 2)
;-> 2

Reference with a variable works too:
(setq idx (char (str 1)))
;-> 98
(setf (j idx) 2)
;-> 2

Why the following expressions raise an error?
(setf (j (char (str 1))) 2)
;-> ERR: string expected : 2

Using a string as value works:
(setf (j (char (str 1))) "2")
;-> "2"

Thanks.
#30
A pandigital magic square (found with newLISP) having n^2 distinct pandigital integers and having the smallest pandigital magic sum.



  1035629784 1035728694 1024638795 1024739685

  1025639784 1025738694 1034628795 1034729685

  1035729684 1035628794 1024738695 1024639785

  1025739684 1025638794 1034728695 1034629785



Magic sum = 4120736958 (pandigital)
#31
newLISP in the real world / Project Euler
February 12, 2021, 12:08:12 AM
Project Euler https://projecteuler.net/">//https://projecteuler.net/ added newLISP language.

I have solved 102 problems: 1..102.



cameyo
#32
; 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)))
#33
newLISP in the real world / pow function problem
October 19, 2020, 02:11:55 AM
I have some problems with the pow function:
(pow 3 0.33)
;-> 1.436977652184852
(pow -3 0.33)
;-> 1.#IND

In Mathematica (WolframAlpha):
3^0.33 = 1.436977652184852
-3^0.33 = -1.436977652184852

A simple solution:
(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

Why newLISP result is 1.#IND?
#34
newLISP in the real world / select function for array
October 04, 2020, 12:38:00 AM
"select" function for array:
(define (select-array arr lst-idx)
    (array (length lst-idx)
           (map (fn(x) (arr x)) lst-idx))
)

Some test:
(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

Now test the "select" function of newLISP:
;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))))

Timing:
(time (select tlist ind) 100)
;-> 4312.832

Write a "select" function using "select-array":
(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

Timing:
(time (select-list tlist ind) 100)
;-> 328.041

The user-function "select-list" is faster than the primitive "select".

Where am I doing wrong?
#35
newLISP in the real world / Array or char bug?
September 23, 2020, 03:21:42 AM
I have some problem to update an array:
; 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


Why the last expression raise an error?
#36
newLISP in the real world / Find single number
August 11, 2020, 01:52:12 AM
Given a list of positive integers each number appears twice except one number. Find the single number.

Note: The function has to traverse the list only once (O(n)).
#37
newLISP in the real world / List of user symbols
July 09, 2020, 09:12:16 AM
A function to list the user symbols:
(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) ())

cameyo
#38
A string consist of digits and non-digit characters. The digits contains a series of positive integers. For instance, the string "abc22zit62de0f" contains the integers 22, 62 and 0.

Write a function to calculate the sum of the integers inside a string (es. 22 + 62 + 0 = 84)
#39
newLISP in the real world / Sorting nil and true
June 16, 2020, 02:10:39 AM
How to sort a list containing nil and true symbols?
(setq a '(nil true b a))
(sort a)
;-> (nil true a b)

Thanks
#40
I am reading the book "The Little Schemer" (yes, i know newLISP is different from Scheme...but i'm learning)

Until chapter 8 i had no problem to translate the code in newLISP.

But now i have the following function:
(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)))))))

Calling it in the following way:
((rember-f =) 'tuna '(shrimp salad and tuna salad))
I got an error:
ERR: invalid function : (test? (first l) a)
Instead the correct output should be:
(shrimp salad and salad)
Can you help me to solve this problem?

Thanks

cameyo