newLISP Fan Club

Forum => newLISP in the real world => Topic started by: sunmountain on March 14, 2011, 10:14:58 PM

Title: for loop weiredness
Post by: sunmountain on March 14, 2011, 10:14:58 PM
Trivial:

> (for (i 1 10 1) (println i))
1
2
3
4
5
6
7
8
9
10


Now this:

> (import "msvcrt.dll" "printf")
printf<7714C5B9>
> (for (i 1 10 1) (printf "%in" i))
0
0
0
0
0
0
0
0
0
0


Could someone please explain what is going on here ?
Title: Re: for loop weiredness
Post by: HPW on March 14, 2011, 11:55:45 PM
My guess: You import the DLL-function printf and call it. The 0 is the returned value from the function-call.
Title: Re: for loop weiredness
Post by: kosh on March 15, 2011, 08:54:34 PM
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.


(for (i 1 10 1) (println (float? i))) ;-> true ...

The solution is use "int" function, or %f, %g format.


(printf "%in" (int i))
(printf "%gn" i)


# P.S.

If num-step (`for' primive argument) is not specified, the variable type i will be `int' .


(for (i 1 10) (println (integer? i))) ;-> true ...