3 comments

[ 3.9 ms ] story [ 15.5 ms ] thread
Let's help Chris fill out his for loop list...

Lua:

for i = 1, 2, 3 do

    print (i)
end
Python:

  for i in range(1, 1+10):
      print(i)
(Yes, I'm into Py 3.x.)

Applesoft BASIC:

  10 FOR I = 1 TO 10
  20 PRINT I
  30 NEXT I
Forth:

  : RANGEPRINT { A B -- }
      A B <= IF
          A . CR
          A 1 + B RECURSE
      THEN
  ;

  1 10 RANGEPRINT
Prolog:

  ?- for(N, 1, 10),
         write(N), nl,
     fail.
Haskell:

Ummm ...

EDIT: Okay, no loops in Haskell, but the logic of the above Forth version still works fine (probably better, actually, thanks to TCO).

  rangeprint a b
      | a <= b     =
          do
              print a
              rangeprint (a+1) b
      | otherwise  =
          return ()

  rangeprint 1 10
(I never can figure out how to indent stuff like the above code.)

EDIT #2: Maybe the following version is cooler?

  rangeprint2 a b = dolist $ map print [a..b] where
      dolist [] = return ()
      dolist (x:xs) = do
          x
          dolist xs

  rangeprint2 1 10
EDIT #3: I think I like this version best:

  listprint (x:xs) = do
      print x
      listprint xs
  listprint [] = return ()

  listprint [1..10]
That was fun. Time to go back to the life I presumably have ....
And there are as many ways to spell "else if".