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

#1
This is a mock-up to simplify my question.

I got these scheme:



[*]hin.lsp

(context 'hin)

(define (hin-show)
(println "from hin.lsp"))


  • [*]wrappers.lsp

    (load "hin.lsp")

    (define (wrap-one)
    (context hin)
    (hin-show) ; this wont work
    (context MAIN))


  • [*]main.lsp

    (load "wrappers.lsp")

    (wrap-one)
  • [/list]


    File hin.lsp contains 244 symbols (functions and constants) autogenerated. It's a DSL for HTML5.



    File wrappers.lsp contains functions to wrap snippets of reusable code like: header, navbar, sidebar, you-get-the-idea.



    File main.lsp calls functions from wrappers.lsp.



    Now, the context switch shown in function wrap-one wont produce the desired result. I think it's because the double pass newlisp parser does. So on the first pass (hin-show) gets transformed into (MAIN:hin-show).



    The idea of hin.lsp is to be able to wrote code like this:

    (html lang= "en"
    (head
    (title "Some title"))
    (body
    (p class= "shiny" "Lore ipsum...")))


    If I have to prefix every call with "hin:" or even with an alias like "h:" hin's while purpose is defeated, which is to be as close to HTML syntax as possible.



    My question is: what's the best way to wrap functions calls from hin inside a function defined in another context without prefixing hin calls?



    I'm trying to build a macro to parse those calls by my own and prefix when appropriate, it will be used like this:

    (define (wrap-one)
    (wrap-in-context
    (...))) ; calls to hin functions


    But I'm having problems recurring inside the macro. And now I'm thinking all this extra parsing could make the whole idea to work too slow. Anyway, I'm digressing. Back to the point: any ideas about how to avoid prepending all calls with context name?
    #2
    Hi all,



    I wanted to share a tiny script which allows me to query google/finance/converter from the CLI.



    It's not much of a scrapper really, but I couldn't come up with a better word to describe it. It does scrap! Really! :P



    Check it here:

    https://github.com/conan-lugmen/newlisp-webscraps">//https://github.com/conan-lugmen/newlisp-webscraps



    I have two questions regarding my code:



    1. Why unify requires uppercase tokens to be able to create symbols with bind? Or maybe my question is this one: Am I correctly using bind/unify? Is that the intended use?



    2. I put a check to consider a get-url not giving back a proper answer. However on some runs I've noticed I get nil results like if my check was passed by. What am I doing wrong there?
    #3
    I thought dragonfly was unmaintained, but since I've saw some recent post about it in here I decided to give it a try today. So I downloaded it, unzipped it into my ~/public_html, cd-ed into ~/p.../example-site, and run ./newlispServer, and I've got this error:



    ERR: list expected: (parse - query QUERY_STRING)
    called from user defined
    function load - once
    called from user defined
    function load - files - in -dir


    I tried tracing the bug in the definition of load-once in lib/utils.lsp by putting a (throw-error "mark") in different places (to see when was exploding), but it always explode in my throw.
    #4
    Since we're in this time of the year, I'll take advantage of it and make some wishes for newlisp.



    First I'll wish there's a new area for wishes like this. Could it be a new forum area or a bug tracker, so we can keep track of what has been proposed and avoid discussing things that have already been discussed. Also to know the status of things under implementation.



    That was a meta-wish. Now what I would like to have implemented in newlisp:



    [*]tab completion working not only on newlisp's primitives but also on loaded modules and new defined symbols inside REPL.

  • [*]in debug mode I wish ENTER to issue s+ENTER so one can press just a single key to advance in step mode. A better option would be to give it auto-configuration, that is: to have a default binding of ENTER to s, but also change default on every new command, so the next ENTER keypress without command would issue last issued command (step, next or cont).


  • [*]error messages with line numbers referring to script file. I know this has been http://newlispfanclub.alh.net/forum/viewtopic.php?f=5&t=67">already discussed, but I think it's a necessary addition. Sometimes you have very similar lines of code, or equal, on different parts and the message is not enough to find the offending line. Recently I had to fill my code with printlns to hunt a bug. Regarding the bloating Lutz mention on linked post, I'd say we could have different modes. So in debugging mode newlisp would be bloated, but not in production mode. Not sure though if two different binaries are necessary or if it could suffice to have a command line switch.


  • [*]parenthesis highlight inside REPL would be very nice


  • [*] remove the need to put [cmd] tags or press ENTER to issue an auto [cmd]. If [cmd] tags are necessary for something else, then don't remove them, but make multi-line code work from the beginning. I knew about the press ENTER trick recently and I think is somewhat hidden and feels unnecessary.


  • [*]a centralized repository endorsed by newlisp wich allows collaborative building of code snippets.
  • [/list]
    #5
    I'm reading now the chapter Working with numbers from Introduction to newlisp book, and in the third code example under the subtitle Invisible conversion and rounding we see:

    (set 'lst '(2 6 9 12))
    ;-> (2 6 9 12)
    (inc (lst 0))
    ;-> 3
    lst
    ;-> (3 6 9 12)
    (map inc lst)
    ;-> (4 7 10 13)
    (map (curry inc 3) lst)
    ;-> (6 9 12 15) ; === CHECK OUT THIS LINE ===
    lst
    ;-> (3 6 9 12)

    But on v10.3.3 I get the map+curry operation to work on the list like this instead:

    newLISP v.10.3.3 64-bit on Linux IPv4/6 UTF-8, execute 'newlisp -h' for more info.
    > (set 'lst '(2 6 9 12))
    (2 6 9 12)
    > (inc (lst 0))
    3
    > lst
    (3 6 9 12)
    > (map inc lst)
    (4 7 10 13)
    > (map (curry inc 3) lst)
    (6 12 21 33) ; === CHECK OUT THIS LINE ===
    > lst
    (3 6 9 12)

    It's taking the number by pairs and adding them instead of increasing them individually by 3 (except the first one).



    Now I recall reading about a function that takes elements by pairs, can't recall the name right now, but recalling that it exists confuses me on this behaviour. Is it expected? Is map+curry+inc supposed to take the list elements in pairs and operate them with one another? I feel it shouldn't, but I may be wrong.



    Doing...

    newLISP v.10.3.3 64-bit on Linux IPv4/6 UTF-8, execute 'newlisp -h' for more info.
    > (set 'lst '(2 6 9 12))
    (2 6 9 12)
    > (map (curry inc 3) lst)
    (5 11 20 32)
    > lst
    (2 6 9 12)
    > (map (lambda (x) (+ x 3)) lst)
    (5 9 12 15)
    > (map (lambda (x) (inc x 3)) lst)
    (5 9 12 15)
    > (map (curry + 3) lst)
    (5 9 12 15)

    ...works as expected. So now I see the curry+inc is the issue.



    What's going on? Should I update ITN book and denote the pitfall as expected behavior or should this behavior be modified?
    #6
    newLISP in the real world / getting error from open
    December 22, 2011, 04:15:32 AM
    I'm using http://i3wm.org/">i3 with https://sites.google.com/site/gotmor/dzen">dzen bar and I'm using newlisp to build me a pretty customized bar.



    I have this code to check for cpuload:

    (define (read-proc-stat)
        (setq procStat (open "/proc/stat" "read"))

        (do-while (regex {cpud?} (read-line procStat))
            (parse-stat-line (current-line)))
        (close procStat))  

    Script runs ok for a while, with a (sleep 1000) between calls, and then spits this:

    ERR: value expected in function read-line : procStat
    called from user defined function read-proc-stat
    called from user defined function draw-cpu-load

    I added a (println procStat) and found script stopped when procStat would take value 1024, so it sounded like I was hitting an OS limit there. So I wrote a new script to test that:

    (define (read-proc-stat)
        (setq proc-stat (open "/proc/stat" "read"))
        (println "open handle: " proc-stat)                                                                                                                                  

        (do-while (regex {cpud?} (read-line proc-stat))
            (println (current-line)))
        (close proc-stat))

    (while true
        (read-proc-stat)
        (sleep 1000))

    With this script the opening handle remains constant (at value 4 in my test).



    The difference with the "real" script is that I have a status script that loads a cpuload module which contains read-proc-stat function definition.



    I wonder why same function gets a constant handle when I call it one way and an incremented one (in steps of 4) when I call it the other way.



    I modified read-proc-stat like this:

    (define (read-proc-stat:read-proc-stat)
        (if (nil? read-proc-stat:procStat)
            (setq read-proc-stat:procStat (open "/proc/stat" "read")))

        (do-while (regex {cpud?} (read-line read-proc-stat:procStat))
            (parse-stat-line (current-line)))
        (seek read-proc-stat:procStat 0))


    Which fixed the issue. But now I wonder: since I'm not closing the file ever, does newlisp close opened file handles when script finishes? What if it finishes with ctrl-c or what if it explodes on some other part? Does it do a proper cleaning?
    #7
    I'm reading the chapter on contexts now on the Introduction to Newlisp book, and just copy pasted the example on FOOP, which looks like this:



    ; definitions

    (define (Time:Time (t (date-value)) (zone 0))
    (list Time t zone))

    (define (Time:show t)
    (date (t 1) (t 2)))

    (define (Time:days-between t1 t2)
    "Return difference in days between two times."
    (div (abs (- (t1 1) (t2 1))) (* 24 60 60)))

    (define (Time:get-hours t)
    "Return hours."
    (int (date (t 1) (t 2) {%H})))

    (define (Time:get-day t)
    "Return day of week."
    (date (t 1) (t 2) {%A}))

    (define (Time:leap-year? t)
    (let ((year (int (date (t 1) (t 2) {%Y}))))
    (and (= 0 (% year 4))
    (or (!= 0 (% year 100)) (= 0 (% year 400))))))

    ; use

    (set 'time-now (Time))
    (set 'my-birthday (Time (date-value 2008 5 26)))
    (set 'christmas-day (Time (date-value 2008 12 25)))

    ; call functions with full context prefix
    (println (Time:show christmas-day))

    ; or call them with colon prefix mode
    (println (:show christmas-day))


    Last call gives me this error:



    $ newlisp foop.lsp
    Wed Dec 24 21:00:00 2008

    ERR: invalid function in function date : (MAIN:t 1)
    called from user defined function Time:show


    I tried switching to the context, just to check cause there will be no fun if you have to do that, but I wanted to check it anyway:



    > (context Time)
    Time
    Time> (set 'christmas-day (Time (date-value 2008 12 25)))
    (Time 1230163200 0)
    Time> (:show christmas-day)

    ERR: invalid function in function date : (MAIN:t 1)
    called from user defined function Time:show


    What I understood about colon prefix mode was that it decided which function to use based on the class found in position zero. So I though maybe this version was messing with the contexts and tried also this definition for 'show' function:



    (define (Time:show Time:t)
        (date (Time:t 1) (Time:t 2)))


    Which gives almost the same error:



    $ newlisp foop.lsp
    Wed Dec 24 21:00:00 2008

    ERR: invalid function in function date : (t 1)
    called from user defined function Time:show


    'MAIN' disappeared, but we still got the same error. So it's like 'show', when called with the colon prefix mode, it's not receiving a list inside 't'.



    I don't know if this is something that has changed since that part of the book was writing or if I'm missing something. Any thoughts are welcome.
    #8
    I found a reference to variable $it set by setf in Introduction to newlisp book, in the chapter on strings,  http://en.wikibooks.org/w/index.php?title=Introduction_to_newLISP/Strings&stable=0#Modifying_strings">//http://en.wikibooks.org/w/index.php?title=Introduction_to_newLISP/Strings&stable=0#Modifying_strings



    which doesn't exist in the manual nor works in v10.3.3.



    I was about to remove the mention, but then decided to ask first in here just in case there's another way to achieve the same results. I mean to operate on the character selected by setf and use that result as the input to the same setf.



    The manual only talks about $<int> and $idx
    #9
    newLISP in the real world / no PCRE_* constants
    November 24, 2011, 06:32:03 AM
    I found this topic:



    http://newlispfanclub.alh.net/forum/viewtopic.php?f=9&t=3763&hilit=PCRE_UTF8">//http://newlispfanclub.alh.net/forum/viewtopic.php?f=9&t=3763&hilit=PCRE_UTF8



    But, I tested both v10.3.3 and v.10.3.6 by doing:



    ./configure && make -f makefile_linuxLP64_utf8



    and I got:



    $ ./newlisp -e '(replace { } " x x x " {-} 0x8000)'
    "-x x x "
    $ ./newlisp -e '(replace { } " x x x " {-} REPLACE_ONCE)'

    ERR: value expected in function replace : REPLACE_ONCE


    Also, shouldn't be REPLACE_ONCE and PRECOMPILED constants names better be prefixed with PCRE_ like the rest?



    I hope it's ok to report this in this section, I didn't found any "bug reports" section.
    #10
    I found that ref wants a list to search for when trying to do this:


    (set 'randomMatrix (array 3 5 '(nil)))
    (dotimes (x 3)
        (dotimes (y 5)
            (setf (randomMatrix x y) (rand 10))))

    (ref '0 randomMatrix)


    gives message:

    ERR: list expected in function ref : ((8 3 7 7 9) (1 3 7 2 5) (4 6 3 5 9) (9 6 7 1 6) (0 2 1 8 1))



    So I changed last line to:


    (ref '0 (array-list randomMatrix))

    What I was wondering is why there's no way to tell an array from a list visually from the REPL.

    I get this:



    > randomMatrix

    ((8 3 7 7 9) (1 3 7 2 5) (4 6 3 5 9) (9 6 7 1 6) (0 2 1 8 1))

    > (array-list randomMatrix)

    ((8 3 7 7 9) (1 3 7 2 5) (4 6 3 5 9) (9 6 7 1 6) (0 2 1 8 1))



    It took me some time to think they might not be the same thing internally and to discover it by asking in the REPL:



    > (array? randomMatrix)
    true
    > (list? randomMatrix)
    nil




    On a side note I was expecting random numbers, but every run spits the same stuff. I realize there must be some initialization going on somewhere (didn't look for it yet). But I think most people expects random stuff to be random and when the need to make a repeatable pseudo-random stuff arise, only then use some pre-seeded function.