Trying to get my head around typed racket and higher order functions

41 views
Skip to first unread message

greadey

unread,
Jun 30, 2019, 4:03:06 AM6/30/19
to Racket Users
Hi all,

I have a function - create-points- which takes two lists of Flonum and yields a list of vector pairs suitable for passing to Racket's plot function.

In un-typed code I can just do;

(define (create-points xs ys)
  (map vector xs ys))

and voila!  It works.  However the only way I have managed to get it to type check is by wrapping the vector function in a lambda;

(: create-points : (Listof Flonum) (Listof Flonum) -> (Listof (Vectorof Flonum))

(define (create-points xs ys)
  (map  (lambda ([x : Flonum] [y : Flonum]) (vector x y)) xs ys)).

I have tried using an inst form, but I cannot seem to get that to work. Question is thought, is wrapping the vector function in a lambda the only way to do this?

Cheers,

greadey

Sorawee Porncharoenwase

unread,
Jun 30, 2019, 4:22:17 AM6/30/19
to greadey, Racket Users

inst is the way to go. How did you use it? This seems to work fine for me.

#lang typed/racket

(: create-points : (Listof Flonum) (Listof Flonum) -> (Listof (Vectorof Flonum)))
(define (create-points xs ys)
  (map (inst vector Flonum) xs ys))

--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/b6192278-03b1-4720-a3c2-72a915e6c959%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Storrs

unread,
Jul 2, 2019, 11:18:39 AM7/2/19
to greadey, Racket Users
I haven't used typed racket, so maybe this is pointless, but does this work for you?

(define/contract (create-points xs ys)
  (-> (listof flonum?) (listof flonum?) (listof (vector/c flonum? flonum?)))
  (map vector xs ys))

(create-points '(1.0 2.0 3.0) '(4.0 5.0 6.0))
; => '(#(1.0 4.0) #(2.0 5.0) #(3.0 6.0))


Reply all
Reply to author
Forward
0 new messages