cons 函数不接受我的输入

cons function does not accept my input

好吧,我正在尝试添加两个元素成为一个列表,from the video of Prof. Colleen Lewis at minute 4 and 53 secs, and from the official racket site我看到最好的做法是使用"cons"。

当我这样做的时候:

(cons (Listof Number) (Listof (Listof Number)))

它工作得非常好,但它让我了解了我应该得到的东西。因此我尝试了:

(cons (Listof (Listof Number)) (Listof Number) )

这导致:

Type Checker: Polymorphic function `cons' could not be applied to arguments:

Argument 1:

Expected: a

Given: (Listof (Listof Real))

Argument 2:

Expected: (Listof a)

Given: (Listof Real)

Result type: (Listof a)

Expected result: (Listof (Listof Real))

in: (cons acc (get-min&max-from-mixed-list (first lol)))

这很奇怪,因为 official site 说:

The cons function actually accepts any two values, not just a list for the second argument.

这是我的实际代码:

(: 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

请注意,当您在 #lang racket 中 link 到 cons 时,它与其他语言中的 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)) 类型。我注意到第二个的类型是 Listof 无论第一个是什么类型。在您的试验中,类型没有这种关系,而是相反。您可能已经切换了参数并且 acc 的正确代码是:

(cons (get-min&max-from-mixed-list (first lol)) acc)

我假设 cons 的类型规范可能要求第二个列表是与第一个参数类型相同的列表。对于像 Scheme 和 #lang racket 这样的无类型语言,你可以在两个参数中使用任何类型。

#lang racket

(cons 3 #f) ; ==> (3 . #f)