Programmming in Lua, Third Edition上的一道题
lua吧
全部回复
仅看楼主
level 3
chaosink 楼主
Exercise 8.4: Can you find any value for f such that the call pcall(pcall, f) returns false as its first result?
2015年09月09日 16点09分 1
level 11
pcall不懂,,,,function a(x)
2015年09月09日 23点09分 2
if x>5 then return (5) else error "6" end pcall(a,15) 完全不会pall,,,,哎,,,@魔术方块黄
2015年09月09日 23点09分
level 8
the answer is : can't
2015年09月10日 02点09分 3
level 3
chaosink 楼主
这道题有问题?
2015年09月11日 03点09分 4
它用pcall调用pcall,问你第二个参赛用什么类型的数据就可以让第一个pcall返回false
2015年09月11日 04点09分
人家题目就是考你对pcall熟不熟悉,如果你知道pcall的用法你就明白根本没有这种函数f。话说,题目说的是什么你清楚了吗?
2015年09月11日 08点09分
@ks_isaf 哪里说是函数了?英语不好?[滑稽]我就是翻译了一下
2015年09月11日 22点09分
回复
Э����־_Tox
:好吧,没有规定f是函数,但必须是个value。
2015年09月12日 03点09分
level 14
print( pcall(pcall, ...) )
falsebad argument #1 to '?' (value expected)
2015年09月11日 05点09分 5
人家题目要的是找出函数f,不是让你返回false。你下面的回复也不符号题目的要求。我认为世界上没有函数f可以使得pcall(pcall,f)的第一个返回值为false,不然pcall就自己打自己的脸了。
2015年09月11日 08点09分
@ks_isaf 明明说的any value
2015年09月11日 16点09分
@qqirisit ...不是value,是个表达式,f()也不是value,是函数调用。不要以为可以a = ...或a = f()就说...和f()是个value好不好。
2015年09月12日 03点09分
level 14
local function f()
end
print( pcall(pcall, f()) )
这样也可以
false
bad argument #1 to '?' (value expected)
2015年09月11日 06点09分 6
level 3
chaosink 楼主
Hello, Mr Ierusalimschy
Exercise 8.4: Can you find any value for f such that the call pcall(pcall, f) returns false as its first result?
I don't know clearly the purpose of Exercise 8.4.
Does it want readers understand that 'pcall' only catches the error message and return it but raise an error so that such 'f' doesn't exist?
Or want really readers to find a 'value' for f?
I find two 'forms' for f that make the call 'pcall(pcall, f)' returns false as the first result:
1. print(pcall(pcall, ...))
2. function f() return end; print(pcall(pcall, f()))
But I'm not sure whether '...' and 'f()' are values in Lua. That's why I use 'form' instead.
And I have some other questions while doing Exercise 8.4:
Why the following two code snippets get different results?
1. function f() return end; print(pcall(pcall, f()))
2. function f() return nil end; print(pcall(pcall, f()))
Why the following two code snippets get different results?
1. function f() return end; print(f())
2. function f() return nil end; print(f())
Why the following two code snippets get the same result?
1. function f() return end; a = f(); print(a)
2. function f() return nil end; a = f(); print(a)
Are 'no value' and 'nil' different? And how to distinguish them?
How to make 'type()' in 'function f() return end; print(type(f()))' safe?
Thank you for your book!
2015年09月12日 07点09分 7
这是我刚才发给作者的邮件
2015年09月12日 07点09分
2015年09月12日 07点09分
@chaosink 其实你完全可以用中文在这里问我......
2015年09月12日 07点09分
@chaosink ,,,你那个pcall(f())=pcall(false) 一旦出现()就是调用,所以嘛,,,你下面的都是废话,,,而且你可以直接调试看看嘛
2015年09月12日 12点09分
1