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 - Fritz

#1
newLISP and the O.S. / NewLISP and CentOS
July 20, 2010, 10:57:34 AM
I got a virtual server with CentOS installed: now I need to run my newLISP projects on it.



Is it possible to install newLISP somehow via yum or any other automatic manager?



Or I should download sources and compile newLISP manually?
#2
newLISP in the real world / How to reduce a list?
July 03, 2010, 07:52:20 AM
I have a list like this:


'((1 Oil 2)
  (1 Oil 5)
  (1 Oil 7)
  (2 Gas 4)
  (2 Gas 12))


I want to reduce it and get list like this:


'((1 Oil 2 5 7)
  (2 Gas 4 12))


I can make it via "ref" or "find" or "filter" operators, but list is pretty big (3'000 records), so speed is important for me.



It takes about 3-4 seconds to reduce list on my PC, but I want 0.5 seconds or even less. May be, there is a special operator for list reducing in newLISP?
#3
I`m used to join "find-all" and "replace" operators via $it/$0 symbol:


(find-all "12" "12345" (replace "1" (copy $0) "N"))

But now, after upgrading to 10.2.8, newLISP says:



"ERR: symbol is protected : $0"



Is here a way to make this construction to work, or I`ll have to change my code somehow to avoid $0/$it symbols?
#4
For some reason find-all operator stops on empty strings:


> (find-all "(?<=,)[^,]*(?=,)" ",1,10,,100,1000,")
("1" "10" "")


I did expect here:


("1" "10" "" "100" "1000")

I think, somewhere inside find-all operator is a check: (if (= found-string prev-string) stop-search)



Is it a feature to make programmers use "parse" operator for empty string searching?
#5
I have suddenly noticed, that newLISP directory operator returns his data in UTF8 codepage, while native Windows XP codepage (in Russia) is windows-1251 (and newlisp-edit.lsp module has windows-1251 encoding too).



That is not an important problem, becouse I always can use construction like


(map decode-utf8-to-cp1251 (directory "."))

Fortunately, russian alphabet is 33 letters only, so decoding is easy. I just want to ask about future politics: will newLISP in the future have an operator like *nix iconv?
#6
Whither newLISP? / Prime numbers
March 23, 2010, 02:46:24 PM
Found today a story about a boy, who was said by a teacher to print all prime numbers from 2 to 100. He had 2 hours to make a program on Basic. Boy had a working code in ten minutes:


10 CLS
20 PRINT "2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97"
30 END


I have tried to translate this program to newLISP:


(define (prime? x) (= (first (factor x)) x))
(println (filter prime? (sequence 2 100)))


My code is as short as Basic one, but it is still 20% slower than a sample code from newLISP manual:


(define (primes n , p)
  (dotimes (e n)
    (if (= (length (factor e)) 1)
      (push e p -1))) p)
#7
I have a variable, which contains some text with special symbols:


> {some symbols /""}
"some symbols \/"""


I want to save contents of this variable to the text file, with all that backslashes before quotes et cetera. Now I use function


(define (gnirts-lave x)
  (1 -1 (string (list x))))


But, may be, there exists more direct way to get "texted" value of a variable?
#8
I have tested today some scripts operating with some millions files, and I have noticed two strange things about (time) function.



1. Big intervals.


> (time (sleep 3600000))
1,844674407e+016


1,8e+016ms = 595 000 years, hehe.



2. Small intervals.


> (time (read-file "100_13.txt") 70)
0
> (time (read-file "100_13.txt") 80)
0
> (time (read-file "100_13.txt") 90)
15,625
> (time (read-file "100_13.txt") 90)
0
> (time (read-file "100_13.txt") 90)
15,625


15,625 = 1/64 second. So, I think, 15,625 ms — is a minimal quant, that can be measured with (time) command. But today I have seen results like 0,010 ms and so on... Only idea I have, there is some difference between Windows and Linux: «15,625 limit» exists in Windows only.



Anyway, I use (time) very often, so it would be really useful for me to know, how (time) function works on small intervals.
#9
newLISP and the O.S. / (+ newLISP Windows RS232)
March 07, 2010, 10:47:46 AM
I need to communicate with standart rs-232 serial port on PC.



Is it possible to write on newLISP something like (read-port "COM1" 20)?
#10
I`m going to create a small program, which will synchronize contents of two folders via TCP-IP.



Now I have an idea to start two newLISP servers and communicate via (net-eval) command. Something like



(net-eval "192.168.1.94" 4711 '(write-file "test.txt" (get-file "http://192.168.1.7/test.txt">http://192.168.1.7/test.txt")))



But this method seems to be unsafe: any villain can make my server to evaluate something evil.



May be there is some other way to send remote commands?
#11
I`m trying to find some easy way to cut lists to the limited size.


(0 100 '("Some" "long" "list")) returns me first 100 dwellers of the list.



(By the way, expression (-100 100 '("Some" "long" "list")) says: "ERR: list index out of bounds" if the list is not long enough. But same expression works ok with strings).



Then I have tried to create a function, that will push an item to the list and cut it to the size I need.


(define (push-cut what where how-long)
  (push what where)
  (if (> (length where) how-long)
    (set 'where (0 how-long where)))
  0)


And... it won`t work. I have found some method in "Code Patterns": make a context and transfer variables like db:db.



But I`m pretty sure, there should be some other way to expain to the function, which variable I want to be pushed.
#12
newLISP in the real world / define-macro and curry
November 12, 2009, 04:55:08 AM
I`m trying to make function curry-all, and my macro works, but... looks too ugly to be correct.


(define-macro (curry-all f)
  (letex (f1 f lst (map string (args)))
    (fn (z) (eval-string (append "(" (name 'f1) " " (join 'lst " ") " " (name 'z) ")")))))

((curry-all + 1 2 3 4) 5)

> 15


I think, there should be some easier way to ask LISP to expand args, right?
#13
newLISP in the real world / (amb 'list)
October 31, 2009, 01:52:05 PM
Trying to select a random value from the list.


(set 'mylist '((0 0) '(0 1) '(0 2)))
(amb mylist)


Result: ((0 0) (0 1) (0 2))



Different combinations of expand, eval, map and apply wont work in my hands. Only working method I have found is the monstercode with eval-string:


(eval-string (append "(amb '" (join (map string mylist) " '") ")"))

But there should be some simple solution, right?
#14
newLISP Graphics & Sound / Gui-Server: "sunked" letters
October 29, 2009, 06:09:42 AM
I have problems with "find" menu in GUI-server (letters are hidden somewhere below the fields, on screenshot I have a word "zulo" typed in the find-field):



http://img7.imageshost.ru/imgs/091029/52f65993ca/d437a.png">



System: Ubuntu 8. On Windows everything is ok.
#15
I have a russian Windows with native Win-1251 and DOS-CP866 encoding. For some strange reason "upper-case" operator want not work:


(println (upper-case "абвгдеёжзийклмнопрстуфхцчшщъыьэюя"))

Result:


"ࡢ㤥¸槨骫쭮ﰱ㴵縹콾"

Expected:


"АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ"

Screenshots:

http://img7.imageshost.ru/imgs/091025/a6a681a191/e2e93.png">http://img7.imageshost.ru/imgs/091025/a ... /e2e93.png">http://img7.imageshost.ru/imgs/091025/a6a681a191/e2e93.png

http://img7.imageshost.ru/imgs/091025/b357b8e2bc/44605.jpg">http://img7.imageshost.ru/imgs/091025/b ... /44605.jpg">http://img7.imageshost.ru/imgs/091025/b357b8e2bc/44605.jpg



Version: newLISP v.10.1.5 on Win32 IPv4 UTF-8



Btw, in Ubuntu "upper-case" works allright:

http://img7.imageshost.ru/imgs/091025/35fb59f3ce/31ca8.png">http://img7.imageshost.ru/imgs/091025/3 ... /31ca8.png">http://img7.imageshost.ru/imgs/091025/35fb59f3ce/31ca8.png
#16
newLISP in the real world / Find-all + Replace = Crash
October 14, 2009, 07:16:27 AM
I meet some problems during attepts to create (html-parse) function. Here are two functions, which do the same: both function suppose to extract data from "td" tags.


(define (sacar-td linea)
  (set 'alveolos (find-all "(<td)(.*?)(</td>)" linea $0 1))
  (map (fn (x) (replace "</?td(.*?)>" x "" 1)) alveolos))

(define (crash-td linea)
    (find-all "(<td)(.*?)(</td>)" linea (replace "</?td(.*?)>" $0 "" 1) 1))

(set 'testrow "<tr><td class='kin'>Alpha</td><td>Gamma</td></tr>")


Longer one, "(sacar-td testrow)" works ok. Shorter one, "(crash-td testrow)", crashes the shell:



> (sacar-td testrow)

("Alpha" "Gamma")

> (crash-td testrow)

*** glibc detected *** /usr/bin/newlisp: double free or corruption (fasttop): 0x080cc808 ***

======= Backtrace: =========

/lib/tls/i686/cmov/libc.so.6[0xb7e31a85]

/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7e354f0]

...



It is the first strange thing.



Second problem -- my repexps works ok only if I replace all "n" to " " before searching.



Update: accidentaly found solution for the second problem, it is "(?s)" key. Now my "parse-html" function works:


; Usage (parse-html (get-url "http://www.newlisp.org/downloads/newlisp_manual.html"))
(define (parse-html texto)
  (map sacar-table (find-all "(?s)(<table)(.*?)(</table>)" texto $0 1)))

(define (sacar-td linea)
  (set 'alveolos (find-all "(<t[dh])(.*?)(</t[dh]>)" linea $0 1))
  (map (fn (x) (replace "</?t[dh](.*?)>" x "" 1)) alveolos))

(define (sacar-table linea)
  (map sacar-td (find-all "(?s)(<tr)(.*?)(</tr>)" linea $0 1)))
#17
newLISP in the real world / Mapping with two arguments
October 13, 2009, 06:51:40 AM
Is it possible to give some argument to a map function? The method below want not work:



(define (add-tail tl str)
  (append str tl))

(map (add-tail "-Schwanz") '("Hund" "Katze" "Pinguin"))


Now I do this so:



(map add-tail (dup "-Schwanz" 3 true) '("Hund" "Katze" "Pinguin"))


But I have a feeling there should be a simpler way.
#18
I'm trying to read the string byte-per-byte (for encoding from 8-bit codepage to UTF-8). But (pop the-string) returns some random number of bytes, so does (the-string 0) etc:

http://img7.imageshost.ru/imgs/091008/3b8db7732c/11005.png">http://img7.imageshost.ru/imgs/091008/3 ... /11005.png">http://img7.imageshost.ru/imgs/091008/3b8db7732c/11005.png



(set-locale "C") did not help too. Only working way I have found is to write temporary file and then use read-char function.



; Usage: (cyr-win-utf "text in windows-1251 encoding")
; Decodes text from windows-1251 to utf-8
(define (cyr-win-utf t-linea)
  ; Loading encoding table
  (set 'en-win-1251 '((255 "я") (254 "ю") (253 "э") (252 "ь") (251 "ы")
  (250 "ъ") (249 "щ") (248 "ш") (247 "ч") (246 "ц") (245 "х") (244 "ф")
  (243 "у") (242 "т") (241 "с") (240 "р") (239 "п") (238 "о") (237 "н")
  (236 "м") (235 "л") (234 "к") (233 "й") (232 "и") (231 "з") (230 "ж")
  (184 "ё") (229 "е") (228 "д") (227 "г") (226 "в") (225 "б") (224 "а")
  (223 "Я") (222 "Ю") (221 "Э") (220 "Ь") (219 "Ы") (218 "Ъ") (217 "Щ")
  (216 "Ш") (215 "Ч") (214 "Ц") (213 "Х") (212 "Ф") (211 "У") (210 "Т")
  (209 "С") (208 "Р") (207 "П") (206 "О") (205 "Н") (204 "М") (203 "Л")
  (202 "К") (201 "Й") (200 "И") (199 "З") (198 "Ж") (168 "Ё") (197 "Е")
  (196 "Д") (195 "Г") (194 "В") (193 "Б") (192 "А")))
  ; saving string to a temp file
  (set 't-file-name (append "/tmp/" (crypto:md5 (string (random)))))
  (write-file t-file-name t-linea)
  ; loading characters to the t-out
  (set 't-out "")
  (set 't-file (open t-file-name "read"))
  (while (set 't-char (read-char t-file))
    (push (or (lookup t-char en-win-1251) (char t-char)) t-out -1))
  (close t-file)
  t-out)


May be, there is a shorter way, without file-writing? I need this function in both Linux and Windows, and Windows temp directory has another name.
#19
newLISP in the real world / Timing question
October 04, 2009, 09:18:14 AM
I have a simple function to short my code, this function just adds args to the big string (that string "cuerpo" is a body of html file).


(define (meter)
  (dolist (x (args))
    (push (string x) cuerpo -1)))


But I have noticed, that using this function instead of


(push "<td>" cuerpo -1)

slows script from 50 ms to about 11'000 ms (I have a table with 2k rows). Then I have tried to simple "meter" function:


(define (meter-2 arg-1)
  (push arg-1 cuerpo -1))


But it is as slow as first one. May be, I should try a macro or something?
#20
newLISP in the real world / Searching in nested list
October 02, 2009, 03:47:40 AM
I have converted xml-document to a big list. It looks like:


<group>
  <group>
    <element>
  </group>
  <element>
</group>


List:


(("group" ("group" ("element" (("code" "1")))) ("element" (("code" "2")))))

How can I find an element inside the list?


(assoc '("code" "2") xml-list)

returns nil.