Live data from Hacker News

G()('al')

github.com

21–30 of 49 posts

Re: G()('al')

#21
post #18

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

This is awesome! Can you add a main method with an example of it working and submit a pull request?

Re: G()('al')

#22
post #18

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

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')

#23

Earlier quoted context omitted.

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.

Thanks for pointing this out, it's now fixed. https://github.com/eatnumber1/goal/pull/18

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')

#24
post #22
post #18

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

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.

Definitely submit a pull-request if you figure out how to do it :)

Re: G()('al')

#26

Easy in Lua. You can even write G()'al'

Great! Can you submit a pull request?

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

Re: G()('al')

#27
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')

#29

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.

This is a great incomplete solution. Can you submit a pull-request for it?

Re: G()('al')

#30

Easy in Lua. You can even write G()'al'

Great! Can you submit a pull request?

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')
Post reply on HN