[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: PiL 3 and pcall
- From: "Pierre Chapuis" <catwell@...>
- Date: Thu, 31 Jan 2013 10:14:02 +0100
> On 31/01/2013, at 9:46 PM, "Pierre Chapuis" <catwell@archlinux.us> wrote:
>
>> In PiL 3 chapter 8 there is this exercise:
>>
>> "Can you find any value for f such that the call pcall(pcall,f)
>> returns false as its first result?"
>
>
> Does the absence of a value count?
>
> $ lua
> Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio
>> =pcall(pcall, (function() end)())
> false bad argument #1 to '?' (value expected)
>
> pcall(pcall) also works, but then it's more obvious that I'm cheating and
> that f is not a value.
Nice trick, although I understood the exercise as: "write pcall(pcall,f)
in your code and it must return false as the first result". Another thing
I would not accept is modifying anything (e.g. pcall...) before calling
this.
fyi what I did is:
local f = function()
local second = false
local h = function()
if c then debug.sethook(nil,"r") end
c = not c
error()
end
debug.sethook(h,"r")
end
pcall(pcall,f)
It returns "false, nil"...
Roberto, will solutions to the exercises be published someday? ;)
--
Pierre Chapuis