News for hackers. Stuff that matters.
Re:Stack Overflow: Printing 1 to 1000 in C by ajays on Monday March 14, 2011 @01:49PM (#2323546)
How would you do the same in other languages? It would be interesting to see the same question answered in Python, Perl, Java, etc. etc. Just as a fun thought exercise, and not to get into language flamewars.
Re:Stack Overflow: Printing 1 to 1000 in C by BobKabob on Monday March 14, 2011 @03:13PM (#2323938)
Here's a solution in Python, using pretty much the same logic as the top vote-getter on Stack Overflow:

    def f(a):
        print a
        print a+1
        {999: int}.get(a,f)(a+2)

    f(1)
Note, I had to count by twos, because when I counted by ones, I got a "maximum recursion limit exceeded" error.

Basically, the concept is to call f recursively, until you get to 999 (and had printed 999 and 1000), at which time you call some non-recursive function (I call "int").

At 999, the inner-called f function terminates, leading to the unwinding of the stack (each previous f function terminating), and then the program ends.

This statement is the hard one to understand:

        {999: int}.get(a,f)(a+2)
Basically, it's saying "Look up 'a' in this hard-coded dictionary that only has one value in it - for 999. If 'a' isn't found (as it won't be most of the time), set the look-up value to the function called 'f'. Otherwise, set the look-up value to the function called 'int'. In either case, call that looked-up value, passing the parameter of a+2."
Re:Stack Overflow: Printing 1 to 1000 in C by gurraman on Monday March 14, 2011 @04:26PM (#2324258)
An Erlang solution:

    f() -> f(1).
    f(1001) -> ok;
    f(N) -> io:fwrite("~p~n", [N]), f(N + 1).
Re:Stack Overflow: Printing 1 to 1000 in C by xentronium on Monday March 14, 2011 @03:23PM (#2323986)
Will this count for solution? :)

    MAX = 1000

    def execute_if_not_equals(value, other, &block)
      noop = lambda {}
      [block, noop][value / other].call
    end

    def print(i)
      execute_if_not_equals(i, MAX) do
        puts(i + 1)
        print(i + 1)
      end
    end

    print(0)

[edit]: updated to not use != operator which is probably a cheat.
Re:Stack Overflow: Printing 1 to 1000 in C by Tomek_ on Tuesday March 15, 2011 @11:10AM (#2327809)
Javascript, validity depends on how exactly do you define conditions and loops:

  var items = [];
  var rec = function(item) {
      var func = [];
      func[true] = function(item) { items.push(item); [item-1].map(rec); };
      func[false] = function(item) {};
      return func[Boolean(item)](item);
  };
  var item = [1000];
  item.map(rec);
  console.log(items.reverse().join(','));
Won't do in browsers due to recurssion limit but change [1000] to [10] and you'll see it works.
Re:Stack Overflow: Printing 1 to 1000 in C by rararational on Monday March 14, 2011 @03:15PM (#2323951)
Does this fit the criteria for python:

    print str(range(1,1001)).strip('[]').replace(', ','\n')
Edit: added missing [ as pointed out below
Re:Stack Overflow: Printing 1 to 1000 in C by kroo on Monday March 14, 2011 @04:27PM (#2324260)
no! For one thing, it still prints out an opening '[' before 1. Second of all, relying on the structure of the string representation of an array is just silly. Perhaps:

    print "\n".join(map(str, range(1,1001)))
or

    print "\n".join(str(i) for i in range(1,1001))
Re:Stack Overflow: Printing 1 to 1000 in C by rararational on Monday March 14, 2011 @04:42PM (#2324350)
Heh, I forgot about join but that for is still a loop right? :)

Or else you could just:

    for i in range(1,1000):
        print i
Plus relying on the string representation of a list isn't silly it as it doesn't change. I just didn't know if range would meet the criteria or not.
Re:Stack Overflow: Printing 1 to 1000 in C by BobKabob on Monday March 14, 2011 @06:34PM (#2324820)
How about this Python solution:

    z="0123456789"
    print map(lambda x:int("".join(x))+1,zip(sorted(zip(*z)[0]*100),
              sorted(zip(*z)[0]*10)*10,
              zip(*z)[0]*100))
Only downside is that it prints as a list (so it has the format of [1,2,3..])
Re:Stack Overflow: Printing 1 to 1000 in C by BobKabob on Monday March 14, 2011 @10:02PM (#2325487)
Even better Python variation: Prints 1 to 1000 and quits:

    from __future__ import print_function 
    z="0123456789"
    map(print,map(lambda x: int("".join(x))+1,zip(sorted(z*100),
                      sorted(z*10)*10, z*100)))

The way it works is to create 3 strings, one for each digit-place. The first digit, when counting from 000 to 999, is 100 zeros, followed by 100 ones, followed by 100 twos, etc. That is represented by

    sorted(z*100)
The middle digit is 10 zeros, 10 ones, etc... and repeat this 10 times. So this is

    sorted(z*10)*10.
The least significant digit is represented by a string that just counts and starts over. it's 1000 characters long: "0123456789012345..." and represented by

    z*100.
I define an unnamed function (using lambda), that does the following: Join the three digits, make it an int, and add 1. So the result of this is a list of numbers from 1 to 1000. In other words,

    map(lambda x:  int("".join(x))+1,zip(sorted(z*100),
                              sorted(z*10)*10, z*100)))
is about the same as if I just used range(1,1001)

Then I map it to the new print function that is available in Python 3.0, or with the import statement.

A simpler version would use range, but that seems a little too simple:

    from __future__ import print_function 
    map(print,range(1,1000))

Now that I think about it, this last one is the right answer!
Re:Stack Overflow: Printing 1 to 1000 in C by patrickyeon on Monday March 14, 2011 @09:43PM (#2325439)

  ('\n').join(map(str, [1,2,3]))
Re:Stack Overflow: Printing 1 to 1000 in C by jodrellblank on Tuesday March 15, 2011 @02:36AM (#2326318)
http://pleac.sourceforge.net/ - Perl cookbook examples translated to other languages, and links to similar translation challenges.
Re:Stack Overflow: Printing 1 to 1000 in C by acqq on Tuesday March 15, 2011 @03:39AM (#2326472)
For this specific task, I think the shortest Perl, as I've mentioned in http://news.ycombinator.com/item?id=2326372 is just

   $,="\n"; print 1..1000
Re:Stack Overflow: Printing 1 to 1000 in C by die_sekte on Monday March 14, 2011 @07:39PM (#2325035)
Haskell:

    mapM_ (putStrLn . show) [1..1000]
Re:Stack Overflow: Printing 1 to 1000 in C by kamechan on Tuesday March 15, 2011 @05:38AM (#2326682)
actually, can be done more simply as:

mapM_ print [1..1000]

but yeah, ...haskell +1 for that :D

Re:Stack Overflow: Printing 1 to 1000 in C by scrabbles on Monday March 14, 2011 @11:45PM (#2325780)
Is there a better way than this for scheme (racket)?

  (map (lambda (n)
       (displayln n))
     (cdr (build-list 1001 values)))
Re:Stack Overflow: Printing 1 to 1000 in C by jsmcgd on Monday March 14, 2011 @08:10PM (#2325138)
Clojure: (map println (range 1 1001))
Re:Stack Overflow: Printing 1 to 1000 in C by 3pt14159 on Monday March 14, 2011 @02:01PM (#2323607)
1.upto 1000 {|n| puts n} # ruby ftw
Re:Stack Overflow: Printing 1 to 1000 in C by ajays on Monday March 14, 2011 @02:02PM (#2323612)
Isn't "upto" a loop? The idea, IIRC, is to print the numbers without loops and conditionals.
Re:Stack Overflow: Printing 1 to 1000 in C by jerf on Monday March 14, 2011 @03:08PM (#2323915)
It "can be" implemented recursively. If you're going to be pedantic about this (and who doesn't love being pedantic?) you need to be very careful about your definitions, because regardless of what you write you're going to have jump instructions in the finally-executed machine code. If you say that you can't have a loop "anywhere", you're limited to recursion, and then what will you do when someone comes along and points out that recursion and looping are ultimately equivalent? This is one of those cases where examining the boundary too closely makes it dissolve under the scrutiny, and it's better just to smile and nod and play along (and have fun arguing).
Re:Stack Overflow: Printing 1 to 1000 in C by 3pt14159 on Monday March 14, 2011 @02:14PM (#2323671)
1.upto(10).class => Enumerator
Re:Stack Overflow: Printing 1 to 1000 in C by tptacek on Monday March 14, 2011 @02:50PM (#2323848)
That's like doing:

  #define NOT_A_LOOP(x) while(x)
Enumerator is just Ruby object goo over a loop.
Re:Stack Overflow: Printing 1 to 1000 in C by 3pt14159 on Monday March 14, 2011 @08:05PM (#2325118)
More like:

#include someone_else_did_it_so_we_dont_know(x)

Just as those other answers in C relied on GOTOs somewhere.

Re:Stack Overflow: Printing 1 to 1000 in C by jluxenberg on Monday March 14, 2011 @03:35PM (#2324041)
puts ((1..1000).to_a.join("\n"))

Which, of course, is passing the buck to Range#to_a

Re:Stack Overflow: Printing 1 to 1000 in C by silentbicycle on Monday March 14, 2011 @02:53PM (#2323857)
1+!1000

No stinking loops!

Re:Stack Overflow: Printing 1 to 1000 in C by iskander on Monday March 14, 2011 @03:24PM (#2323991)
1 + til 1000 No stinking distinction between monadic/dyadic usage of array operators!