News for hackers. Stuff that matters.
Re:G()('al') by csandreasen on Saturday July 26, 2014 @11:58PM (#8091915)
I'm pretty sure this is cheating, but...

  #!/bin/sh
  grep -e "g\(()\)*('al')" $0 | sed 's/()/o/g' | sed "s/('al')/al/"
  exit
  g()('al')
On its own, g()('al') is valid but g()()('al') is a syntax error. Also, bash and sh don't have non-numeric return values, so regardless of the solution you're stuck with printing.
Re:G()('al') by eatnumber1 on Sunday July 27, 2014 @12:02AM (#8091922)
This is a great incomplete solution. Can you submit a pull-request for it?
Re:G()('al') by csandreasen on Sunday July 27, 2014 @12:06AM (#8091928)
Just to clarify, I think it's a non-solution because the g()('al') is never actually executed - grep does all the work. You can add as many parenthesis as you want. The "exit" is there because you'll get a syntax error if you try to execute g()()('al'). g()('al') by itself does nothing (I'm not even sure why it's not flagged as a syntax error).
Re:G()('al') by eatnumber1 on Sunday July 27, 2014 @12:09AM (#8091934)
The three people who are judging this had a long discussion over this case, and basically decided that since it is in the file, it's fine. We frown on it a little because it's basically self-modifying code, but it's better than nothing for bash.
Re:G()('al') by tolmasky on Sunday July 27, 2014 @12:30AM (#8091988)
I actually prefer it vs. frowning on it for the record ;)
Re:G()('al') by pathikrit on Sunday July 27, 2014 @12:29AM (#8091987)
Quite trivial in idiomatic Scala:

  case class Goal(count: Int) {
    def apply() = Goal(count + 1)
    def apply(s: String) = s"g${"o" * count}$s"
  }

  object Goal {
    def g() = Goal(1)
  }

Non-idiomatic shorter answer:

  case class g(p: String = "go") {
    def apply() = g(p + "o")
    def apply(s: String = "o") = p + s
  }
Re:G()('al') by tolmasky on Sunday July 27, 2014 @12:35AM (#8091999)
This looks great! Can you submit a pull request? These things are coming in really fast and its way easier to take that way.
Re:G()('al') by stevenheidel on Sunday July 27, 2014 @12:44AM (#8092022)
Also add

g(s: String) = "gal"

to handle the case of no 'o's.

Re:G()('al') by juol on Sunday July 27, 2014 @12:14AM (#8091947)
C#

    using System;
    using System.Text;

    namespace Goal
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine(G("al"));
                Console.WriteLine(G()("al"));
                Console.WriteLine(G()()()()()("al"));
            }

            static StringBuilder accumulator = new StringBuilder();
            delegate dynamic GoalDelegate(string s = "o");

            static dynamic G(string s = "o")
            {
                switch (s)
                {
                    case "o":
                        accumulator.Append("o");
                        return (GoalDelegate)G;
                    case "al":
                        string result = "G" + accumulator.ToString() + "al";
                        accumulator.Clear();
                        return result;
                    default:
                        return null;
                }
            }
        }

    }
Re:G()('al') by recursive on Sunday July 27, 2014 @12:59AM (#8092041)
Here's my C# solution that uses no global state, and features a cool (I think) variadic lambda.

    using System;
    using System.Linq;

    class Program {
        static void Main() {
            Console.WriteLine(g("al"));
            Console.WriteLine(g()("al"));
            Console.WriteLine(g()()("al"));
            Console.WriteLine(g()()()("al"));
            Console.WriteLine("Press any key to continue.");
            Console.ReadKey();
        }

        delegate dynamic Goal(params string[] args);

        static dynamic g(string suffix = null, string prefix = "g") {
            if (suffix == "al") return prefix + suffix;
            return (Goal)(a => g(a.FirstOrDefault(), prefix + "o"));
        }
    }
It was a fun exercise. I found that creating a github pull request was not such a fun exercise, and took about 10x longer than the actual task at hand. Perhaps I could learn to do it more smoothly with practice.
Re:G()('al') by eatnumber1 on Sunday July 27, 2014 @12:21AM (#8091965)
Cool! Can you submit a pull request?
[deleted]
Re:G()('al') by eatnumber1 on Sunday July 27, 2014 @12:20AM (#8091962)
Can you submit a pull request saying as much in the same form as the Java solution?
Re:G()('al') by zeeboo on Saturday July 26, 2014 @11:27PM (#8091867)
I made this Go one that might be cheating because instead of the ("al") call returning a string, you get a value that implements the String method. Works with fmt.Println :)

http://play.golang.org/p/zrp2sO6ZLU

Re:G()('al') by eatnumber1 on Saturday July 26, 2014 @11:31PM (#8091878)
This is awesome! Can you add a main method with an example of it working and submit a pull request?
Re:G()('al') by personZ on Saturday July 26, 2014 @11:32PM (#8091880)
This is close to identical to what I did, but the use of a global just felt too dirty. Still trying to figure out the use of a closure on it.
Re:G()('al') by eatnumber1 on Saturday July 26, 2014 @11:45PM (#8091896)
Definitely submit a pull-request if you figure out how to do it :)
Re:G()('al') by bobbyi_settv on Saturday July 26, 2014 @11:21PM (#8091847)
The "complete" solutions for Python in the repo don't actually work because they don't reset the state. If you add a final line of:

  print g('al')
for solution1 or:

  print m('rton')
for solution2, you'll find they don't print gal/ mrton.

Here's what I came up with before looking at those solutions that seems to work:

  def g(arg=None):
      if arg == 'al':
          return 'gal'
      def inner(arg=None):
          if arg == 'al':
              return 'g' + (inner.counter * 'o') + 'al'
          inner.counter += 1
          return inner
      inner.counter = 1
      return inner
Re:G()('al') by eatnumber1 on Saturday July 26, 2014 @11:22PM (#8091850)
Resetting the state isn't one of the rules ;)

I'd make a solution which resets the state linked by the table though, so please submit one!

Re:G()('al') by bobbyi_settv on Saturday July 26, 2014 @11:27PM (#8091865)
Rule 7 says:

   g('al') must return "gal".
If you add

   assert g('al') == 'gal'
as the last line of solution 1, the assertion will not pass. I'm not saying there is a rule that mentions "resetting state". I'm saying that their failure to reset state is the bug leading to them not following the rules.
Re:G()('al') by nickdepinet on Saturday July 26, 2014 @11:31PM (#8091873)
Thanks for pointing this out, it's now fixed.

https://github.com/eatnumber1/goal/pull/18

Re:G()('al') by bobbyi_settv on Saturday July 26, 2014 @11:44PM (#8091893)
Nick,

That helps but doesn't completely solve the state problem. Try adding these two lines to the end of the program:

    g()()()()()
    print g('al')

I think 'gal' should be printed here. Rule 7 says that's what your function should return given 'al', and the fact that we've called it in some other way before doesn't change the fact that it is required to return 'gal' given 'al'.

The solution I posted above handles this scenario (and, as a bonus, is thus threadsafe in case multiple people were doing g()()()('al') simultaneously).

Re:G()('al') by morganhankins on Sunday July 27, 2014 @05:20AM (#8092326)

  # goal.rb
  module G
    def [](o=nil)
      o ? "g#{values[0].to_s}al" : ({ :n => values[0].to_s + "o" }.extend G)
    end
  end

  g = {}.extend G
  
  eval DATA.read.gsub("(", "[").gsub(")", "]")
  
__END__

  gal = g('al')
  p gal
  
  goal = g()('al')
  p goal

  goooooooal = g()()()()()()()('al')
  p goooooooal
  
  # ruby goal.rb
  # "gal"
  # "goal"
  # "goooooooal"

# https://gist.github.com/morganhankins/de13ee378907143a7789
Re:G()('al') by GuiA on Saturday July 26, 2014 @11:20PM (#8091843)
The table at the bottom is interesting. I'd love to read articles about why it's impossible in language X. E.g. I'm familiar with C/Python and can implement a solution, and am surprised that it'd be impossible in Ruby - I don't know Ruby, and would love to know why.

More generally, what properties must a programming language satisfy for this problem to be solvable in it?

My current intuition is that if a language does not denote function calls with () enclosing arguments, AND if a language does not support metaprogramming (eg. LISP macros), then the problem is not solvable in it. Still thinking about it.

Re:G()('al') by jcheng on Saturday July 26, 2014 @11:27PM (#8091866)
Ruby and Java both lack first class functions--functions can't return values that can be directly called with () (unless this has changed with Java 8?).
Re:G()('al') by eatnumber1 on Saturday July 26, 2014 @11:21PM (#8091845)
For the impossible bullets, they link to informal arguments as to why it's impossible. The ruby one has code - it's a syntax error.
Re:G()('al') by GuiA on Saturday July 26, 2014 @11:23PM (#8091854)
The bullets link to non-solutions, which are not enough to formally prove that it is impossible to solve this challenge in the language.
Re:G()('al') by eatnumber1 on Saturday July 26, 2014 @11:24PM (#8091858)
Agreed. These are informal. If you want to make a formal proof that it's impossible, I'll make a new category.
Re:G()('al') by nathangrigg on Sunday July 27, 2014 @12:14AM (#8091948)
Python:

    def g(a='', n=0):
        return 'g '+ 'o'*n + a if a else lambda a='': g(a, n+1)
Re:G()('al') by kylec on Saturday July 26, 2014 @11:53PM (#8091903)
Ugh, so many solutions that mutate internal state. I went for a more purely functional approach and kept track of the count with a function parameter (JavaScript):

https://github.com/kylecronin/goal/commit/70c9277ec99f80dca2...

Re:G()('al') by Veritaas on Sunday July 27, 2014 @12:34AM (#8091998)
The ruby non-solution seen here:

https://github.com/eatnumber1/goal/blob/master/non-solutions...

is actually doable if you parse the error.

Re:G()('al') by capicue on Sunday July 27, 2014 @12:37AM (#8092007)
But it requires eval-ing a string literal instead of actually having g()("al") as an expression itself
Re:G()('al') by jsmeaton on Sunday July 27, 2014 @12:21AM (#8091967)
Here's my attempt in python. Started off with the traditional approach but added a functional one too.

https://gist.github.com/jarshwah/0998143317f5ccc093b3

Re:G()('al') by tantalor on Saturday July 26, 2014 @11:17PM (#8091837)
This is a syntax error in perl, so impossible?

  > perl -e "g()()"
  syntax error at -e line 1, near ")("
  Execution of -e aborted due to compilation errors.
Re:G()('al') by eatnumber1 on Saturday July 26, 2014 @11:21PM (#8091848)
Can you submit a pull request saying as much in the same format as the Ruby one?
Re:G()('al') by dima55 on Saturday July 26, 2014 @11:28PM (#8091868)
Nah, you can use a source filter. "man perlfilter"
Re:G()('al') by jnbiche on Saturday July 26, 2014 @11:08PM (#8091819)
Do languages that don't use () as a function call still have to use the same syntax? Or is () just a stand-in for a function call?
Re:G()('al') by eatnumber1 on Saturday July 26, 2014 @11:17PM (#8091835)
Languages that don't use () still have to use the same syntax. See the haskell solution.
Re:G()('al') by krzysz00 on Sunday July 27, 2014 @12:01AM (#8091921)
Not too difficult in Common Lisp (the parser is fully exposed to user code, so I just changed the meaning of "g").
Re:G()('al') by eatnumber1 on Sunday July 27, 2014 @12:11AM (#8091939)
Glad to hear it :)

Can you submit a solution then?

Re:G()('al') by krzysz00 on Sunday July 27, 2014 @12:24AM (#8091974)
I'm pull request #36.
Re:G()('al') by eudox on Saturday July 26, 2014 @11:05PM (#8091813)
This could be an interesting use case for Common Lisp reader macros.
Re:G()('al') by eatnumber1 on Saturday July 26, 2014 @11:22PM (#8091852)
Agreed! Please submit a solution!
Re:G()('al') by whoopdedo on Saturday July 26, 2014 @11:24PM (#8091857)
Easy in Lua. You can even write G()'al'
Re:G()('al') by eatnumber1 on Saturday July 26, 2014 @11:25PM (#8091860)
Great! Can you submit a pull request?
Re:G()('al') by whoopdedo on Sunday July 27, 2014 @12:03AM (#8091923)
Straightforward solution:

    function g(o)
      if o then return "gal" end
      o = "o"
      local function go(al)
        if al then
          return "g"..o..al
        else
          o = o .. "o"
          return go
        end
      end
      return go
    end
    print(g()('al'))
More amusing and hackish solution:

    setmetatable(_G, -- or _ENV, but maintain 5.1 compatibility
      { __index = function(_, g)
        local o = ""
        local function go(al)
          if al then
            return g..o..al
          end
          o = o.."o"
          return go
        end
        return go
      end})
    print(g()('al'))
    print(G()'al')
    print(Wh()()'pdedo')
Re:G()('al') by ufo on Saturday July 26, 2014 @11:56PM (#8091913)
a straightfoward translation of the Javascript version w/ closures should work

    function goal(n)
       return function(al)
           if al then
               return "G" .. string.rep("o", n) .. al
           else
               return goal(n+1)
           end
       end
    end

    G = goal(0)

    print( G()()('al') )
Also note that G()'al' is just syntactic sugar for G()('al') in Lua