在球拍中创建 sqrt 函数 [英] create sqrt function in racket
本文介绍了在球拍中创建 sqrt 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试创建一个 sqrt+
函数,它将获取一个数字列表并返回一个数字列表.谁能告诉我这个函数有什么问题?
I tried to create a sqrt+
function, which will get a list of numbers and return a list of numbers. Can anyone tell me what's wrong with the function?
#lang pl 03
(: sqrt+ : (Listof Number) -> (Listof Number))
;; a version of `sqrt' that takes a list of numbers, and return a list
;; with twice the elements, holding the two roots of each of the inputs;
;; throws an error if any input is negative.
(define (sqrt+ ns)
(cond [(null? ns) 0]
[(< (first ns) 0) (error 'ns "`sqrt' requires a nonnegative input ~s")]
[else ((sqrt ns) (* (sqrt ns) -1))]))
Type Checker: type mismatch
expected: (Listof Number)
given: Zero
in: 0
Type Checker: type mismatch
expected: Nonnegative-Real
given: (Pairof Nonnegative-Real (Listof Number))
in: ns
Type Checker: type mismatch
expected: Nonnegative-Real
given: (Pairof Nonnegative-Real (Listof Number))
in: ns
Type Checker: Summary: 3 errors encountered
in:
0
ns
ns
推荐答案
在 else
的情况下,你应该使用:
In the else
case, you should use:
(let ([s (sqrt (first ns))])
(cons s
(cons (* s -1)
(sqrt+ (rest ns)))))`
这篇关于在球拍中创建 sqrt 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文