'how to use map append correctly with lists of list in Racket
i have this code :
(: min&max-lists : (Listof (Listof Any)) -> (Listof Any))
(define (min&max-lists lst)
(apply append
(map (lambda (slst)
(sublist-numbers slst) ;; **
(cons (sublist-numbers slst) '()) ;; *
)
lst)))
if I run this code with the line with two ** everything works fine, but when I run the code above with the line with one * it shows me like that : Type Checker: type mismatch expected: (Listof Any) given: Any in: slst
how can I solve this? I see if i use the slst with another function it shows me that it is typed if Any and they need List of Any but if I use it like with two ** it works fine
Solution 1:[1]
The problem with (cons (sublist-numbers slst) '())
:
(cons x '()) ;; => '(x)
;; which is nothing other than:
(list x)
If x
is already a list - like in this case)
you have instead of '(1 2 3)
one list layer around it: '((1 2 3))
.
If you apply-append such results you get (Listof (Listof Any)) instead of just (Listof Any).
So, if you want to run using the line *
, you have to change the typing to:
(: min&max-lists : (Listof (Listof Any)) -> (Listof (Listof Any)))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Gwang-Jin Kim |