问题
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,
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.
Negative signs on either input number should be ignored - that is, (make5 561432 -254) would also return 43225.
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)))))))
I'm getting the error...
application: not a procedure; expected a procedure that can be applied to arguments
Any advice would be appreciated. I'm new to scheme and still trying to grasp everything.
回答1:
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.)
You're also missing a comparison in a couple of conditions; (= (modulo (abs x) 1000) 0)
.
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:
- 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 andy
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)
, which produces the leftmostn
digits ofx
, and(right-digits n x)
, which produces the rightmostn
digits ofx
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))))
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.
来源:https://stackoverflow.com/questions/47218016/scheme-function-drracket