5

Örneğin, Typed Racket'te polimorfik işlevlerle çalışacak bir map sürümünü nasıl yazabilirim? Bir liste üzerinde eşlemek çalıştığınızda i bir hata alıyorumTyped Racket'te polimorfik işlevleri argüman olarak alan üst düzey işlevleri nasıl yazarım?

(: id : (All (A) A -> A)) 
(define (id x) x) 

:: Manuel olarak bu durumda polimorfizmin örneğini zorunda

> (map id '(1 2 3)) 

Type Checker: Polymorphic function `map' could not be applied to arguments: 
Types: (-> a b ... b c) (Listof a) (Listof b) ... b -> (Listof c) 
    (-> a c) (Pairof a (Listof a)) -> (Pairof c (Listof c)) 
Arguments: (All (A) (-> A A)) (List One Positive-Byte Positive-Byte) 
Expected result: AnyValues 
    in: (map id (quote (1 2 3))) 

cevap

1

: Ben olarak tanımlanan basit id işlevini kullanın

-> (map (inst identity Integer) '(1 2 3)) 
- : (Listof Integer) [more precisely: (Pairof Integer (Listof Integer))] 
'(1 2 3) 

nedeni Yazılan Raket Kılavuz here açıklanmıştır:

Typed Racket’s local type inference algorithm is currently not able to infer types for polymorphic functions that are used on higher-order arguments that are themselves polymorphic.

(daha fazla bilgi ve örnek için dokümanlar sayfasına bakın)

+0

Bu çok üzücü. Teşekkür ederim. –