Andreas' practical language comparison

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas Koch

    Andreas' practical language comparison

    Hi all,

    i started a little "practical language comparison" - practical
    in the sense that i compare how actual distributions and versions
    and their libraries (not abstract language specifications) solve small
    test cases like the 8 queens problem.

    Currently there are contributions for 17 different languages, and
    none for Phyton (and Lisp. And Forth. And many others ).
    If someone is interested in contributing a few implementations ,
    please have a look at:



    and mail me your code snippets (or critics) or post them here.

    thanks a lot,
    --
    Andreas
    He screamed: THIS IS SIG!
  • Peter Hansen

    #2
    Re: Andreas' practical language comparison

    Andreas Koch wrote:
    [color=blue]
    > i started a little "practical language comparison" - practical
    > in the sense that i compare how actual distributions and versions
    > and their libraries (not abstract language specifications) solve small
    > test cases like the 8 queens problem.
    >
    > http://www.kochandreas.com/home/language/lang.htm
    >
    > and mail me your code snippets (or critics) or post them here.[/color]

    The Java implementation of Bubble Sort doesn't follow the
    specification for the algorithm. It fails to use a "swapped"
    flag to determine when to terminate the loop.

    -Peter

    Comment

    • Peter Hansen

      #3
      Re: Andreas' practical language comparison

      Andreas Koch wrote:
      [color=blue]
      > i started a little "practical language comparison" - practical
      > in the sense that i compare how actual distributions and versions
      > and their libraries (not abstract language specifications) solve small
      > test cases like the 8 queens problem.
      >
      > Currently there are contributions for 17 different languages, and
      > none for Phyton (and Lisp. And Forth. And many others ).
      > If someone is interested in contributing a few implementations ,
      > please have a look at:
      >
      > http://www.kochandreas.com/home/language/lang.htm
      >
      > and mail me your code snippets (or critics) or post them here.[/color]

      You might want to put a little more thought into the way you
      present the information there. As it stands, it appears likely
      to heavily influence people to produce the shortest possible
      programs, rather than the most readable or even most typical for
      a given language.

      Also, even if you leave the line counts in, different languages
      (and people) typically count lines differently. For example,
      some line-counting programs for C count semicolons and commas
      rather than newlines, as these are closer to representative of
      the number of *statements*, and that's what matters most of the
      time when you are thinking "lines of code".

      I sent bubble sort, based on the algorithm, but I'm not going
      to bother with more if this is simply a fewest-lines competition,
      since the first person to post some Perl will win (although K
      appears to be ready to give tight competition in that area,
      if the current list is any indication).

      -Peter

      Comment

      • Andreas Koch

        #4
        Re: Andreas' practical language comparison

        Peter Hansen wrote:

        [color=blue]
        > The Java implementation of Bubble Sort doesn't follow the
        > specification for the algorithm. It fails to use a "swapped"
        > flag to determine when to terminate the loop.[/color]

        Thanks Peter - for a quick solution i corrected the problem
        myself (and corrected sort order, too), and added your
        python examples.


        --
        Andreas
        He screamed: THIS IS SIG!

        Comment

        • Andreas Koch

          #5
          Re: Andreas' practical language comparison

          Peter Hansen wrote:

          [color=blue]
          > You might want to put a little more thought into the way you
          > present the information there. As it stands, it appears likely
          > to heavily influence people to produce the shortest possible
          > programs, rather than the most readable or even most typical for
          > a given language.[/color]

          A valid point. We currently have a discussion about this top
          on c.l.misc, where i made my first proposals for my site. Feel
          free to join in.

          [color=blue]
          > I sent bubble sort, based on the algorithm, but I'm not going
          > to bother with more if this is simply a fewest-lines competition,
          > since the first person to post some Perl will win (although K
          > appears to be ready to give tight competition in that area,
          > if the current list is any indication).[/color]

          It is on no way a fewest-lines competiton, especially considering
          that my favourite language (Delphi) had the most lines in nearly
          every test case :-)

          I removed the line counts from the overview matrix until we find
          a better solution.


          --
          Andreas
          He screamed: THIS IS SIG!

          Comment

          • Georgy

            #6
            Re: Andreas' practical language comparison


            "Andreas Koch" <nospam@kochand reas.com> wrote in message news:c6glig$phm $07$4@news.t-online.com...
            | Hi all,
            |
            | i started a little "practical language comparison" - practical
            | in the sense that i compare how actual distributions and versions
            | and their libraries (not abstract language specifications) solve small
            | test cases like the 8 queens problem.
            |
            | Currently there are contributions for 17 different languages, and
            | none for Phyton (and Lisp. And Forth. And many others ).
            | If someone is interested in contributing a few implementations ,
            | please have a look at:
            |
            | http://www.kochandreas.com/home/language/lang.htm
            |
            | and mail me your code snippets (or critics) or post them here.
            |
            | thanks a lot,
            | --
            | Andreas
            | He screamed: THIS IS SIG!


            Comment

            • Georgy

              #7
              Re: Andreas' practical language comparison

              Quick'n'dirty translation of Algol-68 solution:


              # N queens task * translation from Algol-68 (C) 2004 Georgy
              # Algol-68; http://www.kochandreas.com/home/lang...UEENS_A68G.HTM
              # All: http://www.kochandreas.com/home/language/matrix.htm

              def solve( n, all_solutions=F alse ): # solve n-queens problem

              class Done(Exception) : pass

              n1 = n-1 # some optimization :-)

              column = [True]*n # available columns
              diaga = [True]*(n+n1) # available tr-bl diags (c+r)
              diagb = [True]*(n+n1) # available tl-br diags (c-r) + n-1
              position = [0] * n

              def print_row( q ): # print a row
              stars = ['*']*n
              stars[q] = 'Q'
              for s in stars: print s,
              print

              def try_row(r):
              """try_row( r) -- the first r-1 queens have been placed in the top rows,
              # column/diaga/diagb record which columns and diagonals are still available.
              """
              for c in range(n):
              if r >= n:
              print_row( position[c] )
              if c == n1:
              if all_solutions:
              print '-'*(n+n1)
              return
              else:
              raise Done
              elif column[c] and diaga[c+r] and diagb[c-r + n-1]:
              column[c] = diaga[c+r] = diagb[c-r + n1] = False
              position[r] = c
              try_row( r+1 )
              column[c] = diaga[c+r] = diagb[c-r + n1] = True

              try:
              try_row(0)
              except Done:
              pass

              solve( 8 ) # find one solution; for all solutions: solve( 8, True )


              "Andreas Koch" <nospam@kochand reas.com> wrote in message news:c6glig$phm $07$4@news.t-online.com...
              | Hi all,
              |
              | i started a little "practical language comparison" - practical
              | in the sense that i compare how actual distributions and versions
              | and their libraries (not abstract language specifications) solve small
              | test cases like the 8 queens problem.
              |
              | Currently there are contributions for 17 different languages, and
              | none for Phyton (and Lisp. And Forth. And many others ).
              | If someone is interested in contributing a few implementations ,
              | please have a look at:
              |
              | http://www.kochandreas.com/home/language/lang.htm
              |
              | and mail me your code snippets (or critics) or post them here.
              |
              | thanks a lot,
              | --
              | Andreas
              | He screamed: THIS IS SIG!


              Comment

              • GerritM

                #8
                Re: Andreas' practical language comparison

                "Andreas Koch" <nospam@kochand reas.com> schreef in bericht
                news:c6gu81$pk6 $05$2@news.t-online.com...
                <..snip...>[color=blue][color=green]
                > > I sent bubble sort, based on the algorithm, but I'm not going
                > > to bother with more if this is simply a fewest-lines competition,
                > > since the first person to post some Perl will win (although K
                > > appears to be ready to give tight competition in that area,
                > > if the current list is any indication).[/color]
                >
                > It is on no way a fewest-lines competiton, especially considering
                > that my favourite language (Delphi) had the most lines in nearly
                > every test case :-)
                >
                > I removed the line counts from the overview matrix until we find
                > a better solution.
                >[/color]
                I really would like to see the linecount. I do belive that it is one of the
                indicators of the power of the language and its batteries. Somehow it would
                be nice to have a figure for "readabilit y", but I don't have a clue how to
                generate such a figure in an automatic way. Maybe you need some panel of
                experts that gives a readability figure?

                kind regards, Gerrit
                --




                Comment

                • Peter Hansen

                  #9
                  Re: Andreas' practical language comparison

                  GerritM wrote:
                  [color=blue]
                  > I really would like to see the linecount. I do belive that it is one of the
                  > indicators of the power of the language and its batteries. Somehow it would
                  > be nice to have a figure for "readabilit y", but I don't have a clue how to
                  > generate such a figure in an automatic way. Maybe you need some panel of
                  > experts that gives a readability figure?[/color]

                  I'd reply to this, but as there is already a discussion on
                  comp.lang.misc (as Andreas said) it would probably be
                  silly to continue one in parallel here...

                  -Peter

                  Comment

                  • Andrea Griffini

                    #10
                    Re: Andreas' practical language comparison

                    On Sun, 25 Apr 2004 22:46:48 +0200, "GerritM" <gmuller@worldo nline.nl>
                    wrote:
                    [color=blue]
                    >I really would like to see the linecount. I do belive that it is one of the
                    >indicators of the power of the language and its batteries.[/color]

                    Then at least you should be talking of the same problem and
                    of the same solution to the problem. For example the Delphi
                    solution for the 8 queens problem is completely different
                    from the ALGOL one (and I must say IMO the Delphi solution
                    is quite terrible from an algorithm point of view), comparing
                    a linecount for that that with a linecount for a totally
                    different solution is just nonsense.

                    However this create problems for very different approaches;
                    implementing a certain algorithm in prolog and comparing it
                    with the same algorithm in C has any meaning ? If say one
                    solution uses generators (may be heavily and with backtracking,
                    for example in Icon) how can you implement the same solution
                    on a language that has no generator concept at all ?
                    Even if you manage to do that, what's the point in finding
                    that you needed a lot of lines of code to do it ?
                    [color=blue]
                    >Somehow it would be nice to have a figure for "readabilit y", but I
                    >don't have a clue how to generate such a figure in an automatic way.
                    >Maybe you need some panel of experts that gives a readability figure?[/color]

                    I'm very new to python, but anyway this is my solution to 8
                    queen's problem:

                    ------------------------ PYTHON --------------------------
                    import sets

                    def solve(base, # starting position, as a list of cols
                    free_cols, # list of columns not yet taken
                    free_diag1, # list of diagonals not yet taken
                    free_diag2): # list of counter diagonals not yet taken
                    if free_cols:
                    row = len(base)
                    for i,col in enumerate(free_ cols):
                    d1 = row + col
                    d2 = row - col
                    if d1 in free_diag1 and d2 in free_diag2:
                    solve(base+[col],
                    free_cols[0:i]+free_cols[i+1:],
                    free_diag1.diff erence([d1]),
                    free_diag2.diff erence([d2]))
                    else:
                    print base

                    n = 8
                    solve([],
                    range(1,n+1),
                    sets.Set(range( 1+1,n+n+1)),
                    sets.Set(range( 1-n,n-1+1)))
                    -----------------------------------------------------------

                    and this is a solution to the same problem I wrote in C
                    some time ago while having fun in trying to use as few
                    characters as possible...

                    ----------------------- C -------------------------------
                    char n[39],q=7;void s(){char*x=n+7, *e,*d;while((*x +*(
                    e=x+9+q)+*(d=x+ 31-q)?0:(*x=*e=*d= '1'+q,q--?s(),1:puts
                    (n),q++,*x=*e=* d=0))|x--!=n);}main(){s( );return 0;}
                    ---------------------------------------------------------

                    Does this mean that Python is less expressive ? that
                    C is less clear ? Or just means one program has been
                    written trying to be expressive and the other trying
                    to be compact ?

                    If I run that Delphi solution for the 8 queens problem
                    should I conclude that Python is faster than Delphi ?


                    Andrea

                    Comment

                    • Dennis Lee Bieber

                      #11
                      Re: Andreas' practical language comparison

                      On Sun, 25 Apr 2004 23:12:40 GMT, Andrea Griffini <agriff@tin.i t>
                      declaimed the following in comp.lang.pytho n:

                      [color=blue]
                      > for example in Icon) how can you implement the same solution
                      > on a language that has no generator concept at all ?[/color]

                      Heh... I think even FORTRAN-IV had notation that would allow you
                      to create the equivalent of a "generator" ... At least, some had an
                      extension that allowed for calling into the midpoint of a subprogram
                      body.

                      --[color=blue]
                      > =============== =============== =============== =============== == <
                      > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
                      > wulfraed@dm.net | Bestiaria Support Staff <
                      > =============== =============== =============== =============== == <
                      > Home Page: <http://www.dm.net/~wulfraed/> <
                      > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

                      Comment

                      • Dan Bishop

                        #12
                        Re: Andreas' practical language comparison

                        Andreas Koch <nospam@kochand reas.com> wrote in message news:<c6glig$ph m$07$4@news.t-online.com>...[color=blue]
                        > Hi all,
                        >
                        > i started a little "practical language comparison" - practical
                        > in the sense that i compare how actual distributions and versions
                        > and their libraries (not abstract language specifications) solve small
                        > test cases like the 8 queens problem.
                        >
                        > Currently there are contributions for 17 different languages, and
                        > none for Phyton (and Lisp. And Forth. And many others ).
                        > If someone is interested in contributing a few implementations ,
                        > please have a look at:
                        >
                        > http://www.kochandreas.com/home/language/lang.htm
                        >
                        > and mail me your code snippets (or critics) or post them here.[/color]

                        # ************ Sort 2 ************

                        import sys

                        lines = file(sys.argv[1]).readlines()
                        lines.sort()
                        file('sorted.tx t', 'w').writelines (lines)

                        # ************ Type "file" ************

                        import sys

                        for line in file(sys.argv[1]):
                        print line

                        Comment

                        • Elaine Jackson

                          #13
                          Re: Andreas' practical language comparison

                          At my website (SeanMcIlroy.ne xuswebs.net) I keep a compressed file of all the
                          modules I've written so far in the process of teaching myself python. One of
                          them has to do with prime numbers, primitive roots, etc, which I see forms part
                          of your language comparison. There are also python implementations of some
                          standard graph algorithms (Kruskal, Dijkstra) and assorted other mathematical
                          tidbits, as well as some toy games (tic-tac-toe, nim, mastermind). Help yourself
                          to whatever you want.


                          "Andreas Koch" <nospam@kochand reas.com> wrote in message
                          news:c6glig$phm $07$4@news.t-online.com...
                          | Hi all,
                          |
                          | i started a little "practical language comparison" - practical
                          | in the sense that i compare how actual distributions and versions
                          | and their libraries (not abstract language specifications) solve small
                          | test cases like the 8 queens problem.
                          |
                          | Currently there are contributions for 17 different languages, and
                          | none for Phyton (and Lisp. And Forth. And many others ).
                          | If someone is interested in contributing a few implementations ,
                          | please have a look at:
                          |
                          | http://www.kochandreas.com/home/language/lang.htm
                          |
                          | and mail me your code snippets (or critics) or post them here.
                          |
                          | thanks a lot,
                          | --
                          | Andreas
                          | He screamed: THIS IS SIG!


                          Comment

                          • Andreas Koch

                            #14
                            Re: Andreas' practical language comparison



                            Andrea Griffini wrote:

                            Hi Andrea!
                            [color=blue][color=green]
                            >>I really would like to see the linecount. I do belive that it is one of the
                            >>indicators of the power of the language and its batteries.[/color]
                            >
                            > Then at least you should be talking of the same problem and
                            > of the same solution to the problem. For example the Delphi
                            > solution for the 8 queens problem is completely different
                            > from the ALGOL one (and I must say IMO the Delphi solution
                            > is quite terrible from an algorithm point of view)[/color]

                            I didn't really get the ALGOL one. My first Delphi version
                            was using bitmaps and marking endangered fields, but the
                            code was pretty horrible because i found no elegant way
                            to fill the diagonals but using 2 loops (~ 20 lines of
                            not very intuitive additional code)

                            I find the current version to be quite nicely readable,
                            while the bitmap solution would probably be faster.
                            [color=blue]
                            > comparing
                            > a linecount for that that with a linecount for a totally
                            > different solution is just nonsense.[/color]

                            Depends. The solution often reflects how certain problems
                            are handled in a language.
                            [color=blue]
                            > However this create problems for very different approaches;
                            > implementing a certain algorithm in prolog and comparing it
                            > with the same algorithm in C has any meaning ? If say one
                            > solution uses generators (may be heavily and with backtracking,
                            > for example in Icon) how can you implement the same solution
                            > on a language that has no generator concept at all ?
                            > Even if you manage to do that, what's the point in finding
                            > that you needed a lot of lines of code to do it ?[/color]

                            I have some tests that require to solve an problem, and some
                            that require to use an certain algorith. I think you can
                            encounter both situations in real life. Of course, saying
                            "A needs 20 lines so its better than B which needs 22 lines"
                            is idiotic.
                            [color=blue]
                            > I'm very new to python, but anyway this is my solution to 8
                            > queen's problem:[/color]

                            Thanks, i allready had lots of submissions by mail this
                            night. Lots more feedback than expected :-D
                            [color=blue]
                            > Does this mean that Python is less expressive ? that
                            > C is less clear ? Or just means one program has been
                            > written trying to be expressive and the other trying
                            > to be compact ?
                            >
                            > If I run that Delphi solution for the 8 queens problem
                            > should I conclude that Python is faster than Delphi ?[/color]

                            Good questions. Any ideas for solutions?

                            --
                            Andreas
                            He screamed: THIS IS SIG!

                            Comment

                            • Andreas Koch

                              #15
                              Re: Andreas' practical language comparison

                              Dan Bishop wrote:


                              thanks, but all cases but "spread sheet" got
                              solved in this nights mail rush :-)

                              --
                              Andreas
                              He screamed: THIS IS SIG!

                              Comment

                              Working...