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

#1
newLISP in the real world / regex-all
June 13, 2025, 07:49:32 AM
Does anyone know of any more efficient methods than the following to calculate all the matches of a regex?
Thanks.
(define (regex-all regexp str all)
"Find all occurrences of a regex in a string"
  (let ( (out '()) (idx 0) (res nil) )
    (setq res (regex regexp str 64 idx))
    (while res
      (push res out -1)
      (if all
          (setq idx (+ (res 1) 1))        ; contiguos pattern (overlap)
          (setq idx (+ (res 1) (res 2)))) ; no contiguos pattern
      (setq res (regex regexp str 64 idx)))
    out))

(setq a "AAAaBAAAABcADccAAAB")
(regex "[A]{3}" a)
;-> ("AAA" 0 3)
(regex-all "[A]{3}" a)
;-> (("AAA" 0 3) ("AAA" 5 3) ("AAA" 15 3))
(regex-all "[A]{3}" a true)
;-> (("AAA" 0 3) ("AAA" 5 3) ("AAA" 6 3) ("AAA" 15 3))
#2
Problem Solving with newLISP
https://github.com/cameyo42/newLISP-Code
More than 3000 topics on newLISP:
- Algorithms
- Project Euler
- Rosetta Code
- Programming interview questions
- Library for recreational mathematics (344 functions)
- a lot of programming problems
#3
;---------------------------------
; Minimalistic 2048 (4x4)
; Use "W" "A" "S" "D" key to move
; (load "g2048.lsp")
;---------------------------------
(define (print-grid)
  (for (i 0 3)
    (for (j 0 3)
      (print (format "%4d " (grid i j))))
    (println)) '>)
;
(define (find-zeros)
  (let (pts '())
    (for (i 0 3)
      (for (j 0 3)
        (if (zero? (grid i j)) (push (list i j) pts -1)))) pts))
;
(define (new-game)
  (setq grid (array-list (array 4 4 '(0))))
  (setq zeros (randomize (find-zeros)))
  (setf (grid (zeros 0)) 2)
  (setf (grid (zeros 1)) 2)
  (input))
;
(define (input)
  (print-grid)
  (case (setq key (read-key))
    (87 (up))    (119 (up))
    (65 (left))  (97  (left))
    (85 (down))  (115 (down))
    (68 (right)) (100 (right))
    (48 (exit)) ; "0" --> quit the game
    (true (begin (println "Wrong key.") (setq key-error true)))
  )
  (cond ((ref 2048 grid) (println "Bravo! You win.") (print-grid))
        ((= key-error true) (setq key-error nil) (input))
        (true
          (setq zeros (randomize (find-zeros)))
          (when zeros ; put 2 or 4 in a free cell
            (if (zero? (rand 2))
                (setf (grid (zeros 0)) 2)
                (setf (grid (zeros 0)) 4)))
          (input))))
;
(define (shift-right row)
  (let ((non-zero (filter (fn (x) (!= x 0)) row))
        (zeroes (filter (fn (x) (= x 0)) row)))
    (extend zeroes non-zero)))
;
(define (shift-left row)
  (let ((non-zero (filter (fn (x) (!= x 0)) row))
        (zeroes (filter (fn (x) (= x 0)) row)))
    (extend non-zero zeroes)))
;
(define (matrix-left matrix) (map shift-left matrix))
;
(define (matrix-right matrix) (map shift-right matrix))
;
(define (matrix-down matrix)
  (let (trans (transpose matrix))
    (transpose (map shift-right trans))))
;
(define (matrix-up matrix)
  (let (trans (transpose matrix))
    (transpose (map shift-left trans))))
;
(define (merge-numbers row)
  (let ( (result '()) (idx 0) (len (length row)) )
    (while (< idx len)
      (if (and (< idx (- len 1)) (= (row idx) (row (+ idx 1))))
          (begin
            (push (* 2 (row idx)) result -1)
            (++ idx 2))
          (begin
            (push (row idx) result -1)
            (++ idx 1))))
    (extend result (dup 0 (- len (length result))))))
;
(define (matrix-merge matrix) (map merge-numbers matrix))
;
(define (right)
  (println "right")
  (setq grid (matrix-right grid))  ; move the numbers to right
  (setq grid (matrix-merge grid))  ; merge the numbers
  (setq grid (matrix-right grid))) ; move the numbers to right
;
(define (left)
  (println "left")
  (setq grid (matrix-left grid))
  (setq grid (matrix-merge grid))
  (setq grid (matrix-left grid)))
;
(define (up)
  (println "up")
  (setq grid (matrix-up grid))
  (setq grid (transpose (matrix-merge (transpose grid))))
  (setq grid (matrix-up grid)))
;
(define (down)
  (println "down")
  (setq grid (matrix-down grid))
  (setq grid (transpose (matrix-merge (transpose grid))))
  (setq grid (matrix-down grid)))
;
(new-game)
#4
newLISP in the real world / A simple(?) problem
June 17, 2024, 10:34:36 AM
Write a function that generates a random integer within a closed interval and with predefined digits.

For example:
Range: (1 200)
Digits: 1 2 3
Possible numbers: 1 2 3 11 21 31 12 22 32 13 23 33
 111 211 311 121 221 321 131 231 331 112 212 312 122 222
 322 132 232 332 113 213 313 123 223 323 133 233 333
Numbers in the range: 1 2 3 11 21 31 12 22 32 13 23 33
 111 112 113 121 122 123 131 132 133
Random Number: Any of the numbers in the range
#5
Whither newLISP? / Strange behavior
April 19, 2024, 12:28:25 PM

(define (test a) (extend '() (sequence 1 a)))
(test 4)
;-> (1 2 3 4)
(test 4)
;-> (1 2 3 4 1 2 3 4)
(test 4)
;-> (1 2 3 4 1 2 3 4 1 2 3 4)
test
;-> (lambda (a) (extend '(1 2 3 4 1 2 3 4) (sequence 1 a)))
#6
newLISP in the real world / rotate bug?
December 19, 2023, 05:23:26 AM
Maybe a bug of "rotate" when negative rotations and absolute rotations multiple of length of list.

(rotate '("1" "A" "B" "2") 8)
;-> ("1" "A" "B" "2")
(rotate '("1" "A" "B" "2") -8)
;-> ("1") ;ERROR
(rotate '("1" "A" "B" "2") 12)
;-> ("1" "A" "B" "2")
(rotate '("1" "A" "B" "2") -12)
;-> ("1") ;ERROR

Workaround:
(rotate lst (- (% r (length lst))))
(rotate '("1" "A" "B" "2") (- (% 12 4)))
;-> ("1" "A" "B" "2")
(rotate '("1" "A" "B" "2") (- (% 8 4)))
;-> ("1" "A" "B" "2")

#7
Given a string, how to reverse lowercase to uppercase and vice versa with a "regex"?
#8
newLISP and the O.S. / Windows clipboard
October 09, 2023, 06:00:10 AM
Do you known a way to manage windows clipboard (cut, copy, paste)?
I have tried to use clipboard.dll with no luck.
Thanks.

cameyo
#9
newLISP in the real world / newLISP Note
June 03, 2023, 09:07:51 AM
newLISP Note
more than 2000 newLISP topics (Project euler, Rosetta code, Programmers Interview, ...)
cameyo
p.s. it's written in Italian (but not the code ;)) and I'm just an amateur :)
#10
Whither newLISP? / Searching on array
February 01, 2023, 07:06:15 AM
How to search a value in array ?

Use array-list and then find, ref, etc ?
#11
So, what can you actually DO with newLISP? / chatGPT
January 21, 2023, 12:24:36 AM
chatGPT is able to write code in newlisp!!!

Some errors, but it is fun.

Try: "write code in newlisp to solve quadratic equation"
#12
I'm looking for old versions of newLISP manual (historic reasons).

Any version is appreciated.

Thank you

cameyo
#13
Anything else we might add? / Auguri
December 25, 2022, 09:51:22 AM
Best wishes to all
(println "Happy " (add 1 (div 2 (div 3 (add 4 5 (mul 6 7 8 9))))))
#14
So, what can you actually DO with newLISP? / newlisp note
November 23, 2022, 08:44:33 AM
Over one thousand of problems solved with newlisp.

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

So much fun with newlisp.

Thanks Lutz
#15
newLISP newS / Lisp book
July 27, 2022, 05:21:11 AM
Lisp Book (Croatian language) by Kazimir Majorinc at:

http://monoskop.org/images/c/cb/Majorinc_Kazimir_Mocan_koliko_je_god_moguce.pdf">//http://monoskop.org/images/c/cb/Majorinc_Kazimir_Mocan_koliko_je_god_moguce.pdf



Traslation in english "As Powerful As Possible" at:

https://github.com/amno1/as-powerful-as-possible">//https://github.com/amno1/as-powerful-as-possible
#16
newLISP in the real world / Function as list
July 04, 2022, 06:53:35 AM
https://gist.github.com/cameyo42/13fb05eb49769e5f64913b624507c14e">//https://gist.github.com/cameyo42/13fb05eb49769e5f64913b624507c14e

Can't post code on forum :-(
#17
newLISP in the real world / string function
June 13, 2022, 10:43:11 PM
Why this?
(setq a 10)
(setq b 20)
(string "'"a" '"b"")
-> "'10 '20"
#18
https://github.com/cameyo42/newLISP-Note/blob/master/17-note-libere-9.lsp#L456">//https://github.com/cameyo42/newLISP-Note/blob/master/17-note-libere-9.lsp#L456

(useful for testing purpose)
#19
newLISP in the real world / setq '
June 01, 2022, 09:32:46 AM
(setq 'a 3)
output: 3
a
output: nil


Which symbol is binded with 3?

How to retrieve it?



p.s. it's only a curiosity
#20
Decrypt this message:

"WQGI XH BJ HKOUHVO MWCAAP EEQG JDRPJMVH XQZ KSWSJJ STHSKGL XXLU TOXF."