You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Racket中cons函数参数报错与结果不符的技术咨询

Fixing cons Order & Type Errors in Racket

Let's break down what's going on here—this is such a common gotcha when you're getting comfortable with Racket's list semantics, especially with typed checking enabled!

First, let's recap how cons works, because that's the core of your issue:

  • cons takes two arguments: a single element (the car of the new cons cell) and a list (or the empty list '(), which terminates a list) as the cdr.
  • In Typed Racket, its type signature is (All (a) (-> a (Listof a) (Listof a)))—plainly put, the element you pass first has to match the type of elements in the list you pass second.

Why your first call gave reversed results

When you ran (cons (Listof Number) (Listof (Listof Number))) (I'm assuming you passed actual values like (cons '(1 2) '((3 4) (5 6)))), you got a reversed order compared to your expectation because cons always prepends the first argument to the front of the second argument's list.

For example:

(cons '(1 2) '((3 4) (5 6))) ; Returns '((1 2) (3 4) (5 6))

If you were expecting '((3 4) (5 6) (1 2)) instead, cons can't do that directly—it's meant for adding to the head of a list. To add to the end, you need to use append with list to wrap your single element into a list:

(append '((3 4) (5 6)) (list '(1 2))) ; Returns '((3 4) (5 6) (1 2))

Why swapping parameters caused a type error

When you flipped the arguments to (cons (Listof (Listof Number)) (Listof Number)), Typed Racket threw an error because you violated the type constraint of cons.

Let's use concrete values to see why:

(cons '((3 4) (5 6)) '(1 2))

Here, the first argument is a (Listof (Listof Number)), so cons expects the second argument to be a (Listof (Listof (Listof Number))) (a list of lists of lists) or '(). But you passed a (Listof Number)—a list of plain numbers—so the types don't line up. The checker can't reconcile a list that should contain lists of lists with one that contains plain numbers.

The fix

If your goal is to build a (Listof (Listof Number)) (a list where each element is a list of numbers):

  1. To prepend a number list to the front of your existing list of number lists: Keep using cons with the number list first, and the list of number lists second. This is the idiomatic use of cons for building lists incrementally.
    #lang typed/racket
    (define my-number-list : (Listof Number) '(1 2 3))
    (define my-list-of-lists : (Listof (Listof Number)) '((4 5) (6 7)))
    (cons my-number-list my-list-of-lists) ; Correct, returns '((1 2 3) (4 5) (6 7))
    
  2. To append a number list to the end: Use append as shown earlier—wrap your single number list in list to turn it into a list of the same type as your existing list of lists, then append them.

内容的提问来源于stack exchange,提问作者Tomer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:52:33