方案功能(DrRacket) [英] Scheme Function (DrRacket)

查看:56
本文介绍了方案功能(DrRacket)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在尝试在方案中编写以下函数,并能够在 DrRacket 上运行它.问题如下,

So, i'm trying to write the following function in scheme, and to be able to run it on DrRacket. The problem is as follows,

ma​​ke5 - 接受两个整数,并返回一个由第一个输入的最右边 3 位数字和第二个输入的最左边 2 位数字构成的 5 位整数.例如,(make5 561432 254) 将返回 43225.

make5 - takes two integers, and returns a 5-digit integer constructed of the rightmost 3 digits of the first input, and the leftmost 2 digits of the second input. For example, (make5 561432 254) would return 43225.

应忽略任一输入数字上的负号 - 即 (make5 561432 -254) 也将返回 43225.

Negative signs on either input number should be ignored - that is, (make5 561432 -254) would also return 43225.

如果第一个数字少于三位或最后三位以零开头,和/或第二个数字少于两位,您的函数应该返回-2.注意:您可能需要定义一些辅助功能.

If the first number has less than three digits or the last three digits start with zeros, and/or the second number has less two digits, your function should return -2. Note: you may want to define some auxiliary functions.

到目前为止,这是我能够编写的函数.

So far this is the function I've been able to write.

(define (make5 x y)
  (cond ((< (length x) 3) -2)
        ((< (length y) 2) -2)
        (((modulo (abs(x)) 1000) 0) -2)
        (((modulo (abs(y)) 1000) 0) -2)
        (else (append (list-tail x 3) (cons (first(y)second(y)))))))

我收到错误...

应用程序:不是程序;期望一个可以应用于参数的过程

任何建议将不胜感激.我是计划的新手,仍在努力掌握一切.

Any advice would be appreciated. I'm new to scheme and still trying to grasp everything.

推荐答案

不要把参数放在括号里 - (abs(x)) 的意思是调用过程 xcode> 并将结果传递给 abs.
(cons (first(y)second(y)) 意思是cons件事:first;调用过程y的结果;second的值;调用过程y的结果.
(您在某些地方正确调用了过程.坚持相同的模式.)

Don't wrap your arguments in parentheses - (abs(x)) means "call the procedure x and pass the result to abs.
(cons (first(y)second(y)) means "cons these four things: the value of first; the result of calling the procedure y; the value of second; and the result of calling the procedure y".
(You've called procedures correctly in some places. Stick to the same pattern.)

您还缺少在几个条件下的比较;(= (modulo (abs x) 1000) 0).

You're also missing a comparison in a couple of conditions; (= (modulo (abs x) 1000) 0).

输入不是列表,而是整数,因此您不能对它们应用 lengthfirst 或任何此类内容.
结果应该是一个整数,而不是一个列表,所以你不能使用 appendcons 来构造它,你应该只使用算术.

The inputs are not lists, they're integers, so you can't apply length, first, or any such things to them.
The result should be an integer, not a list, so you can't construct it using append and cons, you should only use arithmetic.

这些关于整数的事实应该让你开始:

These facts about integers should get you started:

  • 如果一个数字小于 10000,则它少于五位数字.
  • 非负数n的最后四位是(modulo n 10000).
  • 如果 x 为 12,y 为 34,x * 100 + y 为 1234.
  • 要得到整数中最左边的三位数字,您可以反复除以 10,直到得到一个小于 1000 的数字.
  • A number has fewer than five digits if it is smaller than 10000.
  • The last four digits of a non-negative number n is (modulo n 10000).
  • If x is 12 and y is 34, x * 100 + y is 1234.
  • To get the three leftmost digit in an integer, you can divide by 10 repeatedly until you have a number less than 1000.

另请注意,第二个数字的数字只有一个条件,而第一个数字有两个,并且关于定义辅助功能的说明并没有留下来作为没有它们的挑战.

Also note that the second number only has one condition on its digits while the first has two, and that the note about defining auxiliary functions was not left there as a challenge for you to do without them.

例如,如果你有辅助功能

For instance, if you had the auxiliary functions

  • (left-digits n x),产生x最左边的n个数字,以及
  • (right-digits n x),产生x
  • 最右边的n个数字
  • (left-digits n x), which produces the leftmost n digits of x, and
  • (right-digits n x), which produces the rightmost n digits of x

你可以这样写(描述中使用了if"和or"这两个词也可能不是巧合):

you could write (it's also probably not a coincidence that the description uses the words "if" and "or"):

(define (make5 x y)
    (if (or ( ... ))
        -2
        (+ (* 100 (right-digits 3 x)) (left-digits 2 y))))

既然你想忽略数字的符号,在开始的时候处理一下abs很方便,使用let:

Since you want to ignore the sign of the numbers, it's convenient to take care of abs once at the start, using let:

(define (make5 signed-x signed-y)
    (let ((x (abs signed-x))
          (y (abs signed-y)))
        (if (or ( ... ))
            -2
            (+ (* 100 (right-digits 3 x)) (left-digits 2 y)))))

现在剩下的全部"就是填写条件并编写两位数提取函数.

"All" that's left now is filling in the conditions and writing the two digit-extracting functions.

这篇关于方案功能(DrRacket)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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