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

Messages - fdb

#16
Or replacing the map with a function for apply, so only traversing the string two times  
> (apply (fn(x y) (+ (int x) (int y))) (find-all {[1-9]d*} "o123p0010iru5") 2)
138
#17
Nice, I didn't know starts-with and didn't know I could use a regex in find-all, but then we could also simplify your code:[code]
> (apply + (map int (find-all {[1-9][0-9]*} "o123p010iru5")))
138
>
#18
Hi cameo,



my first attempt would be:
(define (parse-str str)
  (apply + (map int (clean empty? (parse str {[^0-9]} 0)))))


if it needs to be faster I would do:

(define (parse-str str)
(let (total 0)
(dolist (s (parse str {[^0-9]} 0))
(unless (empty? s)
(inc total (int s))))
total))
#19
newLISP in the real world / Re: Puzzle
November 24, 2019, 02:08:35 AM
ah yes to easy when n doesn't need to be a number...



(define (f n)
  (if (number? n)
(list n)
(- (n 0))))
#20
newLISP in the real world / Re: Puzzle
November 22, 2019, 02:21:27 PM
The simplest i could think of is:

(define-macro (f n)
  (- (n 1)))


But i do not know if macro's are allowed!
#21
Hi cameyo,



The keybindings are in the package.json file, have you tried reloading: view/command palette/developer:reload window? If you have customised keybindings (with keybindings.json) you may have keybinding conflicts.
#22
Hi,



I've added the ability to evaluate selected expression(s), it will either send the current selection to the REPL or, if nothing is selected, it will select the 'first' expression within the cursor position to the REPL. This 'first expression' starts 1 position left to cursor if this is a opening bracket, otherwise the next opening bracket to the right and then finds the matching closing bracket. (This seems to be the behaviour of the editor.action.selectToBracket  command of the VS Code editor and the easiest way to implement selections).



Only single new lines within the expression are supported, let me know if it works, only tried this at my MacBook
#23
Hi cameyo,



I had added the possibility to execute the active selection in the REPL but had problems getting it to work reliable so I chose to first release without it. My way of working is that I usually test my one liners in the repl first, for multi line functions I enter them in the editor, save them with cmd-S and then load them in the repl with alt-L.



Let me know if everything works ok for you.



Ferry
#24
I've made a GitHub repository with language support for newLISP in Visual Studio code: https://github.com/luxint/vscode-newlisp">//https://github.com/luxint/vscode-newlisp.



If you don't now VS Code have a look at https://code.visualstudio.com">//https://code.visualstudio.com,  a very nice free editor, built on open source,  from Microsoft, runs on Windows, Mac and Linux.



Current state of the extension is it does newLISP syntax highlighting, launching a newLISP REPL and evaluating the active file in the REPL. What I want to add is several color theme's and snippets (to show function syntax and explanations when 'hovering').



This is how it looks with theme Monokai: https://github.com/luxint/vscode-newlisp/blob/master/images/Screenshot%202019-06-01%20at%2022.18.57.png">//https://github.com/luxint/vscode-newlisp/blob/master/images/Screenshot%202019-06-01%20at%2022.18.57.png



Blue: the 'meta' functions (define, define-macro, macro, fn, lambda, lambda-macro)

Red: mutable primitive functions (set, push, ..)

Green: non-mutable primitive functions (list, array, append, ..)

Purple: numbers (integers, floats, bigints, ..)

Yellow: strings

Grey: quoted literals and comments

White: the rest



If you want to collaborate,  need different colors or have ideas how to make this better, let me know!
#25
HI, cameyo, also here you can use a standard function: explode, see below:



> (setq lst (sequence 0 9))
(0 1 2 3 4 5 6 7 8 9)
> (explode lst 2)
((0 1) (2 3) (4 5) (6 7) (8 9))
> (explode lst 3)
((0 1 2) (3 4 5) (6 7 8) (9))

> (setq lst (sequence 1 12))
(1 2 3 4 5 6 7 8 9 10 11 12)
> (explode (explode lst 2)2)
(((1 2) (3 4)) ((5 6) (7 8)) ((9 10) (11 12)))
#26
Not very elegant but maybe faster (for short lists I presume)


(define (nesting lst prev (t 0))
(if (= lst prev)
t
 (nesting (flat lst 1) lst (inc t))))


> (nesting '(a (((b c (d)))) (e) ((f)) g))
5
#27
I think newLISP is a great tool for tinkering with some ideas in your head, AKA 'exploratory computing'. If you like this as well maybe you'll also like the little write up below.



Recently I had to explain 'what is a prime' to my eldest daughter, while explaining 'there are an infinite number of primes' I thought, what about the distance between primes.., maybe there is a certain distribution?



I resisted the easiest way (google it) and went for the best (explore it in newLisp).



Ok , so we need to the primes, how to generate them ? Look in the documentation and I got this:

;; primes.lsp - return all primes in a list, up to n

(define (primes n , p)
  (dotimes (e n)
    (if (= (length (factor e)) 1)
      (push e p -1))) p)

Great a fast factor function! So how long this take for all the primes up to 1,000,000 ?
> > (time primes 1000000)
937.407

Ok within a second so how much primes are there?
> (length (primes 1000000))
78498

So about 80000 primes /second, can we do better? Well an obvious improvement is to step through the odd numbers only and start with 2:
(define (primes-to n , (p-list '(2)))
(for (x 3 n 2)
(unless (rest (factor x))
(push x p-list -1)))
p-list
)

I really like my '(unless (rest (factor(.. bit here, so what about the time?
> (time (primes-to 1000000))
569.302


Aha! as expected almost twice as fast, now one last improvement before we go to the main topic, of course to generate primes has been known since antiquity so how does the  'Sieve of Eratosthenes' look in newLisp? Here is my version:
(define (sieve n)
(set 'arr (array (+ n 1)) 'lst '(2))
(for (x 3 n 2)
(when (not (arr x))
(push x lst -1)
(for (y (* x x) n (* 2 x) (> y n))
(setf (arr y) true))))
lst
)

But is it faster then using the built in factor function?
> (time (sieve 1000000))
259.257

Not bad for antiquity, we'll be using this function from now on.So what we want to do now is calculate the distance between primes. After some tinkering I came up with this:(define (funlist lst func rev)
(if rev
(map func (chop lst) (rest lst))
(map func (rest lst) (chop lst))))

So a generic function which we can use to calculate the differences between consecutive list members:
> (funlist (sieve 1000) -)
(1 2 2 4 2 4 2 4 6 2 6 4 2 4 6 6 2 6 4 2 6 4 6 8 4 2 4 2 4 14 4 6 2 10 2 6 6 4 6
 6 2 10 2 4 2 12 12 4 2 4 6 2 10 6 6 6 2 6 4 2 10 14 4 2 4 14 6 10 2 4 6 8 6 6 4
 6 8 4 8 10 2 10 2 6 4 6 8 4 2 4 12 8 4 8 4 6 12 2 18 6 10 6 6 2 6 10 6 6 2 6 6 4
 2 12 10 2 4 6 6 2 12 4 6 8 10 8 10 8 6 6 4 8 6 4 8 4 14 10 12 2 10 2 4 2 10 14 4
 2 4 14 4 2 4 20 4 8 10 8 4 6 6 14 4 6 6 8 6)

Ok great no we want to count all the differences, we'll use this function:
(define (freq lst)
(let (ulist (unique (sort lst)))
(map list ulist (count ulist lst))))

It is so nice to have all this functions in newLISP like count!

So applying this we get:> (freq (funlist (sieve 1000) -))
((1 1) (2 35) (4 40) (6 44) (8 15) (10 16) (12 7) (14 7) (18 1) (20 1))

Hmm for all the primes below 1000, 6 is the most frequent difference,i didn't expect that, how about all the primes below 1,000,000 ? We probably need to sort the list to get the highest first, so lets make a function to use in the sort:(define (comp x y)
    (>= (last x) (last y)))

And use it:(sort(freq(funlist (sieve 1000000) -)) comp)
((6 13549) (2 8169) (4 8143) (12 8005) (10 7079) (8 5569) (18 4909) (14 4233) (16
  2881)
 (24 2682)
 (20 2401)
 (22 2172)
 (30 1914)
 (28 1234)
 (26 1175)
 (36 767)
 (34 557)
 (32 550)


Definitely looks like number 6, does this take forever? Have a look at https://en.wikipedia.org/wiki/Prime_gap">//https://en.wikipedia.org/wiki/Prime_gap, hope you enjoyed this little  exercise!
#28
Hi Lutz,



The 'macOS Intel executable 10.7.5'  and the 'macOS dynamic library 64-bit UTF8'  are .dms files (Amiga DMS Disk archive)?  which I cannot extract (by the Unarchiver). if I change to extension to .dmg , no luck either.



mvg

Ferry
#29
Whither newLISP? / Re: Retrieving the value of a symbol
February 11, 2019, 12:20:33 PM
Hi, I presume you want to convert a string into a symbol , which can be done like this:

(set (sym "name") 3)

name ->3

If you however want to convert "name" including quotes into a symbol then use this code:

(set (sym {"name"}) 3)


and to retrieve the value from "name":

(eval (sym {"name"})) -> 3
#30
newLISP in the real world / Re: csv to assoc-list
January 25, 2019, 01:11:41 PM
someting like:

(set 'csv '(("a" "b") (1 2) (3 4) (5 6)))

(flat (map (fn(x) (map list (first csv) x)) (rest csv)) 1)

=> (("a" 1) ("b" 2) ("a" 3) ("b" 4) ("a" 5) ("b" 6))