I'm trying to call take
on the output of flatten
. The problem is that take
requires a list of a
but flatten
returns a list of Any
. Is there a way to convert between them? Or some other approach I'm supposed to take? I wasn't able to find any examples in the Racket Docs.
(: extend (All (a) (-> (Listof a) Integer (Listof a))))
(define (extend l n)
; extend a list to length 'n' by appending it to itself
;
; @l list of any
; @n int
; @return list of any
(take (flatten (make-list n l)) n))
From the interpreter, here are the exact types for each function for reference.
> take
- : (All (a) (-> (Listof a) Integer (Listof a)))
#<procedure:take>
> flatten
- : (-> Any (Listof Any))
#<procedure:flatten>
Here's the error message for reference too.
alg/waterfall.rkt:65:2: Type Checker: Polymorphic function `take' could not be applied to arguments:
Argument 1:
Expected: (Listof a)
Given: (Listof Any)
Argument 2:
Expected: Integer
Given: Integer
Result type: (Listof a)
Expected result: (Listof a)
flatten
is really hard to give a precise type, since it flattens lists of arbitrary depth. Consider usingappend*
here, instead; it does a simpler thing and therefore has a nicer type. – Alexis King