typed-racket

Insert Node into a Tree - Racket

与世无争的帅哥 提交于 2019-12-25 16:46:49
问题 I am trying to add a new node to the tree. The following are my definitions and function type: (define-struct (Some T) ([value : T])) (define-type (Option T) (U 'None (Some T))) (define-type BST (U 'E Nd)) (define-struct Nd ([root : Integer] [lsub : BST] [rsub : BST])) (: insert : Integer BST -> BST) ;; insert an item into a tree ;; note: do not insert duplicate items (define (insert n x) (match x ('E 'E) ((Nd ro ls rs) (cond ((= (size x) 1) (Nd ro (Nd n 'E 'E) 'E)) (else (Nd ro ls rs))))))

create sqrt function in racket

被刻印的时光 ゝ 提交于 2019-12-13 22:47:43
问题 I tried to create a sqrt+ function, which will get a list of numbers and return a list of numbers. Can anyone tell me what's wrong with the function? #lang pl 03 (: sqrt+ : (Listof Number) -> (Listof Number)) ;; a version of `sqrt' that takes a list of numbers, and return a list ;; with twice the elements, holding the two roots of each of the inputs; ;; throws an error if any input is negative. (define (sqrt+ ns) (cond [(null? ns) 0] [(< (first ns) 0) (error 'ns "`sqrt' requires a nonnegative

Zip function in typed racket with rest arguments

那年仲夏 提交于 2019-12-11 07:26:30
问题 I'm struggling with the syntax for a function that zips together any number of lists. I currently have: (define (zip . [lsts : (Listof Any) *]) (apply map (inst list Any) lsts)) Which causes the following error when evaluated: Error: struct:exn:fail:syntax /Applications/Racket v6.6/collects/racket/private/kw.rkt:929:25: Type Checker: Bad arguments to function in `apply': Domains: (-> a b ... b c) (Listof a) (Listof b) ... b (-> a c) (Pairof a (Listof a)) Arguments: (-> Any * (Listof Any))

How do I write higher-order functions that take polymorphic functions as arguments in Typed Racket?

走远了吗. 提交于 2019-12-09 15:57:04
问题 For example, how can I write a version of map that will work with polymorphic functions in Typed Racket? I use a simple id function defined as: (: id : (All (A) A -> A)) (define (id x) x) When I try to map it over a list i get an error: > (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

for/list annotations in typed/racket

不羁的心 提交于 2019-12-07 06:54:55
问题 I'm trying to add types to some numerical racket code in the hopes of making it faster, but I am stuck dealing with for/list macro expansion in the code below. (: index-member ((Listof Any) (Listof Any) -> (Listof Index))) (define (index-member xs ys) (filter-not negative? (for/list ([(ann i Index) (in-range (ann (length xs) Index))]) (if (member (list-ref xs i) ys) i -1)))) This function returns a list of indexes foreach x which is a member of y. It works in Racket, but I can't seem to get

How do I write higher-order functions that take polymorphic functions as arguments in Typed Racket?

落爺英雄遲暮 提交于 2019-12-04 03:11:34
For example, how can I write a version of map that will work with polymorphic functions in Typed Racket? I use a simple id function defined as: (: id : (All (A) A -> A)) (define (id x) x) When I try to map it over a list i get an error: > (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))) You have to manually