Page 1 of 1
for loop weiredness
Posted: Tue Mar 15, 2011 5:14 am
by sunmountain
Trivial:
Code: Select all
> (for (i 1 10 1) (println i))
1
2
3
4
5
6
7
8
9
10
Now this:
Code: Select all
> (import "msvcrt.dll" "printf")
printf<7714C5B9>
> (for (i 1 10 1) (printf "%i\n" i))
0
0
0
0
0
0
0
0
0
0
Could someone please explain what is going on here ?
Re: for loop weiredness
Posted: Tue Mar 15, 2011 6:55 am
by HPW
My guess: You import the DLL-function printf and call it. The 0 is the returned value from the function-call.
Re: for loop weiredness
Posted: Wed Mar 16, 2011 3:54 am
by kosh
The variable type of i is maybe `double' (newlisp's float type).
printf %i (dll or libc.so) requires a `int' argument.
Therefore, the output will be broken because of type mismatches.
Code: Select all
(for (i 1 10 1) (println (float? i))) ;-> true ...
The solution is use "int" function, or %f, %g format.
Code: Select all
(printf "%i\n" (int i))
(printf "%g\n" i)
# P.S.
If num-step (`for' primive argument) is not specified, the variable type i will be `int' .
Code: Select all
(for (i 1 10) (println (integer? i))) ;-> true ...