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

#1
Hi,



I realized that the code for the fibonacci functions in the codepatterns

section is wrong: http://www.newlisp.org/CodePatterns.html#toc-5">http://www.newlisp.org/CodePatterns.html#toc-5



The code is:


; classic recursion
; slow and resource hungry
(define (fib n)
    (if (< n 2) 1
        (+  (fib (- n 1))
            (fib (- n 2)))))

> (fib 12)
233


Should be:


; classic recursion
; slow and resource hungry
(define (fib n)
    (if (< n 2) n
        (+  (fib (- n 1))
            (fib (- n 2)))))

> (fib 12)
144


The iterative version should be fixed also, something like

that should be correct:


; iteration
; fast and also returns the whole list
(define (fibo n , f)
  (set 'f '(1 0))
  (case n
    (0 (1 f))
    (1 f)
    (true
      (dotimes (i (- n 1))
        (push (+ (f 0) (f 1)) f)))))

> (fibo 12)
(144 89 55 34 21 13 8 5 3 2 1 1 0)
#2
newLISP newS / Re: newLISP stable release 10.6.2
January 20, 2015, 01:27:06 PM
Thanks a lot for this new release Lutz!
#3
Quote from: "Lutz"When these conflicts occur in newLISP, they are normally decided by the occurrance of real world usage patterns and their practicality rather then trying to be consistent.


Perfect, I see the point. Thanks for your detailed explanation!
#4
I'm surprised that implicit indexing doesn't raise an error

on an empty string (when called with 0 or -1), while it

does on an empty list:



> ("" 0)

""



> ("" -1)

""



With another index, we get an error as expected:

> ("" 1)



ERR: invalid string index



If we compare to implicit indexing on an empty list, we always

get an error:



> ('() 0)



ERR: invalid list index



>('() -1)



ERR: invalid list index



Any explanation for this?



(btw, thanks again Lutz, newLISP is a real pleasure to use)
#5
Quote from: "ralph.ronnquist"How about net-select?.


Yes Ralph, you're totally right, it's exactly what I was looking for.

Works perfectly.
#6
Old post but it's worth a reply as I was also looking for a solution.

process and waitpid can be used. An example returning stdout/stderr

combined and the return code of the command:



(define (exec2 cmd)
  (let ((pid) (out_r) (out_w) (output) (data))
    (map set '(out_r out_w) (pipe))
    (setq pid (process cmd 0 out_w))
    (close out_w)
    (while (read out_r data 1024)
           (extend output data))
    (list output (>> ((wait-pid pid) 1) 8))))


Examples of the execution of a successful and a failed command:

 

> (exec2 "/bin/echo pouet")
("pouetn" 0)

> (exec2 "/bin/ls /root")
("/bin/ls: cannot open directory /root: Permission deniedn" 2)
#7
Yes your code works for the ret_sint32 function, which takes an int as an argument and return an int as a result. My problems comes when I want to call a function taking no argument and returning a signed int:



$ cat returnneg.c
int returnneg()
{
        return -1;
}

$ gcc -Wall -fPIC returnneg.c -shared -o returnneg.so

$ newlisp
newLISP v.10.6.0 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h

> (import "./returnneg.so" "returnneg" "int")
returnneg@7F1E4B1D2645
> (returnneg)
4294967295


After having a look at newlisp source code in nl-import.c, I realize that

libffi is not used if str-param-type is empty (nl-import.c at lines

148-149 for Unix platforms). So, I have to cheat a bit and make my call

this way to get the expected result:



> (import "./returnneg.so" "returnneg" "int" "void")
returnneg@7F1E4B1D2645
> (returnneg)
-1
#8
Thanks for the advice about named pipes and peek.



Code has been tested on  Debian/Squeeze and Debian/Jessie, on amd64.

Newlisp is compiled with ffilib.



$ gcc -m64 -fPIC ffitest.c -shared -o ffitest.dylib
$ newlisp
newLISP v.10.6.0 64-bit on Linux IPv4/6 UTF-8 libffi, options: newlisp -h

> (sys-info)
(436 576460752303423488 408 1 0 2048 0 4355 10600 1409)
> (import "./ffitest.dylib" "ret_sint32" "int")
ret_sint32@7F3325FA3C1B
> (ret_sint32 -1)
4294967295
#9
Had to perform some I/O multiplexing on pipes (read multiple pipes in parallel

until they're closed), and realized that newlisp does not provide a function like

"select" in C to monitor file descriptors.



I've looked in the documentation but didn't find any solution. I've ended doing a C

implementation to get the desired result, something like that (select_readable is a

wrapper calling my c implementation, taking a list of file descriptors and returning

the first one that is ready to read):



(import "./mylib.so" "c_select_readable")
...
; fd1 and fd2, two filedescriptors I'd like to read
(setq fds (list fd1 fd2))
(while (and fds (setq fd (select_readable fds)))
  (if (nil? (read-line fd))
    (replace fd fds)
    (println current-line)))


If you have any 100% newlisp solution, I would be interested. The peek function

might have been a candidate, but is not enough, as I need to know in a non

blocking way when a pipe has been closed.



PS: as a side note, while coding my C extension, I realized I wasn't able to get a signed

integer values as a return value, even if I mention "int" as the str-return-type during import:



$ cat returnneg.c
int returnneg()
{
        return -1;
}

$ gcc -Wall -fPIC returnneg.c -shared -o returnneg.so

> (import "./returnneg.so" "returnneg" "int")
returnneg@7F1E4B1D2645
> (returnneg)
4294967295


Any idea of what I'm doing wrong? Thanks in advance.
#10
Your patch works Lutz, and dev version 10.6.1 compiles just fine.

Thanks a lot for your work on newlisp, that's an amazing piece of software.
#11
Hi everyone, I'm a newcomer to newlisp, that's my first post on this forum.



Had an issue compiling newlisp with readline on debian testing (error messages bellow).

CPPFunction and the like are unavailable since readline 6.3. I provide here a quick

diff to get it compiled properly:



--- newlisp-10.6.0/newlisp.c.ori        2014-04-08 16:54:24.000000000 +0200

+++ newlisp-10.6.0/newlisp.c    2014-05-18 10:31:44.003239839 +0200

@@ -904,7 +904,7 @@



 #ifdef READLINE

 rl_readline_name = "newlisp";

-rl_attempted_completion_function = (CPPFunction *)newlisp_completion;

+rl_attempted_completion_function = (rl_completion_func_t *)newlisp_completion;

 #if defined(LINUX) || defined(_BSD)

 /* in Bash .inputrc put 'set blink-matching-paren on' */

 rl_set_paren_blink_timeout(300000); /* 300 ms */

@@ -984,15 +984,11 @@

 return ((char *)NULL);

 }



-#ifdef _BSD

 extern char **completion_matches PARAMS((char *, rl_compentry_func_t *));

-#else

-char ** completion_matches(const char * text, CPFunction commands);

-#endif



 char ** newlisp_completion (char * text, int start, int end)

 {

-return(completion_matches(text, (CPFunction *)command_generator));

+return(completion_matches(text, (rl_compentry_func_t *)command_generator));

 }

 #endif /* READLINE */





Error messages with unpatched newlisp 10.6.0:



make[1]: Entering directory '/home/jef/Downloads/newlisp-10.6.0'

gcc -fPIC -m64 -Wall -Wno-uninitialized -Wno-strict-aliasing -Wno-long-long -c -O2 -g -DREADLINE -DSUPPORT_UTF8 -DNEWLISP64 -DLINUX -DFFI -I/usr/local/lib/libffi-3.0.13/include  newlisp.c

newlisp.c: In function 'main':

newlisp.c:907:37: error: 'CPPFunction' undeclared (first use in this function)

 rl_attempted_completion_function = (CPPFunction *)newlisp_completion;

                                     ^

newlisp.c:907:37: note: each undeclared identifier is reported only once for each function it appears in

newlisp.c:907:50: error: expected expression before ')' token

 rl_attempted_completion_function = (CPPFunction *)newlisp_completion;

                                                  ^

newlisp.c: At top level:

newlisp.c:990:47: error: unknown type name 'CPFunction'

 char ** completion_matches(const char * text, CPFunction commands);

                                               ^

newlisp.c: In function 'newlisp_completion':

newlisp.c:995:1: warning: implicit declaration of function 'completion_matches' [-Wimplicit-function-declaration]

 return(completion_matches(text, (CPFunction *)command_generator));

 ^

newlisp.c:995:34: error: 'CPFunction' undeclared (first use in this function)

 return(completion_matches(text, (CPFunction *)command_generator));

                                  ^

newlisp.c:995:46: error: expected expression before ')' token

 return(completion_matches(text, (CPFunction *)command_generator));

                                              ^

newlisp.c:996:1: warning: control reaches end of non-void function [-Wreturn-type]

 }