The following program works in Typed Racket, but if you comment out the
for/list in (encode ...) and uncomment the seemingly equivalent map it
fails to type check.

Why the discrepancy and how do the experienced Typed Racketeers diagnose
and fix these sorts of issues?


Thanks

Dan


P.S. These are taken from Nintey-nine Lisp Problems:
http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html


#lang typed/racket

(require typed/test-engine/racket-tests)

;P09 (**) Pack consecutive duplicates of list elements into sublists.
(: pack : (All (a) (Listof a) -> (Listof (Listof a))))
(define (pack xs)
  (cond [(null? xs) null]
        [(null? (rest xs)) (list xs)]
        [else (let ([packed (pack (rest xs))]
                    [head (first xs)])
                (if (equal? (first xs) (second xs))
                    (cons (cons head (first packed))
                          (rest packed))
                    (cons (list head) packed)))]))

(check-expect (pack '(a a a a b c c a a d e e e e))
              '((a a a a) (b) (c c) (a a) (d) (e e e e)))


;P10 (*) Run-length encoding of a list.
(: encode : (All (a) (Listof a) ->
                 (Listof (List Nonnegative-Integer a))))
(define (encode xs)
  #;(map (λ (ys) (list (length ys) (first ys))) (pack xs))
  (for/list ([ys (pack xs)])
    (list (length ys) (first ys))))

(check-expect (pack '(a a a a b c c a a d e e e e))
              '((a a a a) (b) (c c) (a a) (d) (e e e e)))


(test)

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to