好的,我正在尝试添加两个元素来成为一个列表,在第4分钟和第53分钟加入来自科琳·刘易斯教授的视频,从官方球拍网站中我看到了最佳实践是使用“con”。
当我这么做的时候
(cons (Listof Number) (Listof (Listof Number)))它工作得很好,但它给了我一个镜子,我应该得到什么。因此,我试着:
(cons (Listof (Listof Number)) (Listof Number) )这导致:
类型检查器:多态函数“`cons”不能应用于参数: 论点1: 预期:a 给予:(清单(实表)) 论点2: 预期:(a清单) 给予:(实表) 结果类型:(a表) 预期结果:(表(实表)) in:(cons (从混合列表中得到-min&max)(第一个lol)
这很奇怪,因为官方网站说:
cons函数实际上接受任意两个值,而不仅仅是第二个参数的列表。
以下是我的实际代码:
(: min&max-lists (-> (Listof (Listof Any)) (Listof (Listof Number))))
(: tail-min&max-lists (-> (Listof (Listof Any)) (Listof (Listof Number)) (Listof (Listof Number))))
(: get-min&max-from-mixed-list (-> (Listof Any) (Listof Number)))
(define (min&max-lists lol)
(tail-min&max-lists lol '()))
(define (tail-min&max-lists lol acc)
(if (null? lol)
acc
(tail-min&max-lists (rest lol) (cons acc (get-min&max-from-mixed-list (first lol))))))
(define (get-min&max-from-mixed-list mixedList)
(if (null? (sublist-numbers mixedList))
'()
(min&maxRec (sublist-numbers mixedList) (first (sublist-numbers mixedList)) (first (sublist-numbers mixedList)))))
(test (min&max-lists '((any "Benny" 10 OP 8) (any "Benny" OP (2 3)))) => '((8 10) ()))下面是我的代码所使用的各种函数的代码。这些功能很好,因此不应该有理由检查它们:
#lang pl
(require rackunit)
(require racket/trace)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Question 1
(: min&maxRec (-> (Listof Number) Number Number (Listof Number)))
(: min&max (-> Number Number Number Number Number (Listof Number) ))
(define (min&max number1 number2 number3 number4 number5) ; gets all numbers and sends to the min&max recursive part
(min&maxRec (list number2 number3 number4 number5) number1 number1)) ; made all numbers to become a list and has max and min numbers
(define (min&maxRec myList max min)
;; logic: 1) if finished list give back result 2) else check if head of list max or min and recall the function with a shorter list and the new max or min
(if (null? myList)
;return the min and max from myList when finished looking at all numbers
(list min max)
(if (> (first myList) max); is the head max?
(min&maxRec (rest myList) (first myList) min)
(if (< (first myList) min) ; is the head min?
(min&maxRec (rest myList) max (first myList) )
(min&maxRec (rest myList) max min)))))
(test (min&max 2 3 2 7 1) => '(1 7))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Question 2 a
(: sublist-numbers (-> (Listof Any) (Listof Number)))
(: tail-sublist-numbers (-> (Listof Any) (Listof Number) (Listof Number)))
;;This func calls a diff func since the tail-recursion needs an accumelator.
(define (sublist-numbers myList)
(tail-sublist-numbers myList `()))
;;this uses tail-recursion (all calculations are dont before the recursion and are sent with an accumelator)
(define (tail-sublist-numbers myList acc)
(if (null? myList)
acc ; if finished all vars in list
(if (number? (first myList))
(tail-sublist-numbers (rest myList) (cons (first myList) acc)) ; if the head of list is a number add it to acc, and continue working on the rest of the list
(tail-sublist-numbers (rest myList) acc)))) ; else throw this var and work on rest of list发布于 2018-04-07 18:26:10
注意,当您用cons链接到#lang racket时,它与其他语言中的cons不一样,比如#lang pl。相同的名称在不同的语言中可以是相同的,但它们往往是不同的。例如:cons和#!r6rs中的朋友从mcons映射到#lang racket中的朋友
我不知道#lang pl是什么,但似乎是以某种方式输入的。我想当你说你做的时候:
(cons (Listof Number) (Listof (Listof Number)))你的意思是:
(cons a b)其中a是(Listof Number)类型,b是(Listof (Listof Number))类型。我注意到第二个类型是对第一个类型的任何类型的列表。在你的试验中,这些类型的人并没有这种关系,而是相反。很可能您已经切换了参数,acc的正确代码是:
(cons (get-min&max-from-mixed-list (first lol)) acc)我假设cons的类型规范可能要求第二个列表与第一个参数的类型相同。对于非类型化的语言,如Scheme和#lang racket,您可以在这两个参数中使用任何类型。
#lang racket
(cons 3 #f) ; ==> (3 . #f)https://stackoverflow.com/questions/49710398
复制相似问题