卢卡斯质疑如何使答案输出为2、1、3、4、7、11..... [英] Lucas question how to make answer output as 2, 1, 3, 4, 7, 11.....

查看:160
本文介绍了卢卡斯质疑如何使答案输出为2、1、3、4、7、11.....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

w_lucas = Block
 [
     VarDecl "a" (Val (VInt 2)),
     VarDecl "b" (Val (VInt 1)),
     VarDecl "c" (Val (VInt 0)),
	 VarDecl "i" (Val (VInt 2)),
 
     If ( Equals (Var "arg") (Val (VInt 0)))
	 ( Block [ Assign "result" (Val (VInt 2))])
	 
	  ( Assign "result" (Val (VInt 2)) ),

     While ( LessOrEq (Var "i")(Var "arg"))
     ( Block
	  [ Assign "c" (Plus (Var "a") (Var "b")),
         Assign "a" (Var "b"),
         Assign "b" (Var "c"),
         Assign "i" (Plus (Var "i") (Val (VInt (1))))    
       ]
     ),

     Assign "result" (Var "b")
 ]

lucas :: Int -> Int
lucas n = let m = [ ("result", VInt (-1)), ("arg", VInt n)]
               in (asInt.fromJust) (lookup "result" (exec w_lucas m))



这是我的代码,输出是1,1,3,4,7,11 ......

我尝试过的事情:

更改VInt号的Val,但不会更改输出



this is my code, the output is 1,1,3,4,7,11......

What I have tried:

change the Val of VInt number but it does not change the output

推荐答案

报价:

i已编译代码并仅运行输出lucas 0应该返回2,但始终返回1

i have the code compiled and run just the output lucas 0 should return 2 but it always return 1



编译并不意味着您的代码是正确的! :laugh:
将开发过程视为编写电子邮件:成功编译意味着您以正确的语言(例如英语,而不是德语)编写了电子邮件,而不是电子邮件中包含您要发送的消息.

因此,现在您进入开发的第二阶段(实际上是第四或第五阶段,但是稍后将进入较早的阶段):测试和调试.

首先查看它的功能以及与您想要的功能有何不同.这很重要,因为它可以为您提供信息,说明执行该操作的原因.例如,如果打算让用户输入一个数字并将其加倍并打印答案,那么输入/输出是这样的:



Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it''s the fourth or fifth, but you''ll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it''s doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:

Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16

然后,很显然,问题出在加倍它的位上-它不是自身相加或乘以2,而是自身相乘而后返回输入的平方.
因此,您可以看一下代码,很明显它就在这里:

Then it''s fairly obvious that the problem is with the bit which doubles it - it''s not adding itself to itself, or multiplying it by 2, it''s multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it''s obvious that it''s somewhere here:

int Double(int value)
   {
   return value * value;
   }


一旦知道什么地方可能出问题了,就开始使用调试器找出原因.在第一行放置一个断点,然后运行您的应用程序.在执行代码之前,请考虑一下代码中的每一行应该执行的操作,并将其与使用"Step over"按钮依次执行每一行时的实际操作进行比较.它符合您的期望吗?如果是这样,请转到下一行.
如果没有,为什么不呢?有什么不同?

这是一项技能,值得开发,因为它可以在现实世界以及开发中为您提供帮助.就像所有技能一样,它只能通过使用来提高!


Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line, and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it''s one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!


我不知道Lucas,但好像您忘记将代码设置为0时就停止了代码
I don''t know Lucas, but it looks like you forgot to tell your code to stop once you set the result for 0
If ( Equals (Var "arg") (Val (VInt 0)))
    ( Block [ Assign "result" (Val (VInt 2)),
              stop here])


引用:

我已编译代码并仅运行输出lucas 0应该返回2,但始终返回1

i have the code compiled and run just the output lucas 0 should return 2 but it always return 1


那就是调试器派上用场的时候了.
调试器-维基百科,免费百科全书 [


That is when a debugger comes handy.
Debugger - Wikipedia, the free encyclopedia[^]


这篇关于卢卡斯质疑如何使答案输出为2、1、3、4、7、11.....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆