for loop weiredness

Q&A's, tips, howto's
Locked
sunmountain
Posts: 39
Joined: Tue Mar 15, 2011 5:11 am

for loop weiredness

Post 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 ?

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Re: for loop weiredness

Post by HPW »

My guess: You import the DLL-function printf and call it. The 0 is the returned value from the function-call.
Hans-Peter

kosh
Posts: 72
Joined: Sun Sep 13, 2009 5:38 am
Location: Japan
Contact:

Re: for loop weiredness

Post 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 ...

Locked