Solving the #perl weekly challenge 6 in #racketlang

Seeings as I was playing with P6 (perl6), I thought I’d have a crack at the Perl Weekly Challenge #6. Here’s a recap of the first challenge:

Create a script which takes a list of numbers from command line and print the same in the compact form. For example, if you pass “1,2,3,4,9,10,14,15,16” then it should print the compact form like “1-4,9,10,14-16”.

I tried doing it in P6, but ran into a number of problems. In one attempt, I tried laying down delimiting markers. But P6 didn’t really seem to want to split my array the way I wanted. I also had problems creating arrays of arrays. Another odd problem was that P6 seemed to think that some of my arrays were read-only when I used them in curly braces. I found that to be totally unexpected. What also surprised me is that there does not seem to be a way to intersperse a value between elements in an array. It must exist, surely?

I am new to P6, of course, but the challenge gave me more difficulty than I expected. I eventually gave up on the P6 implementation, and switched to Racket:

#lang racket

(require srfi/13)

(define inputs '(1 2 3 4 9 10 14 15 16 18))

(define marked
  (let loop ((curr '()) (acc '()) (inputs inputs) (prev +inf.f))
    (if (empty? inputs)
        (cons curr acc)
        (let* ((el (car inputs))
               (rem (cdr inputs)))
          (if (empty? curr)
              (loop (cons el el) acc rem el)
              (if (= el (+ 1 prev))
                  (loop (cons (car curr) el) acc rem el)
                  (loop (cons el el) (cons curr acc) rem el)))))))                       

(define seqs
  (map (lambda (x)
         (let* ((el1 (car x))
                (el2 (cdr x))
                (diff (- el2 el1)))
           (case diff
             ((0) (number->string el1))
             ((1) (format "~a,~a" el1 el2))
             (else (format "~a-~a" el1 el2)))))
       marked))

(string-concatenate (add-between (reverse seqs) ","))                                           

I am not expert on Scheme, but at least I found a solution. I wonder what other solutions are like.

It is entirely possible that a P6 implementation would look a lot better than a Scheme implementation. I tend to dislike recursive constructs, but I think that in the case of the challenge presented, recursion works well. If I were to attempt a P6 implementation again, I think I would try to “transpile” the Scheme version.

I am not saying that Scheme is better, but forcing me to write in a recursive style seemed to make me uncover a correct solution. From what I can gather, P6 does not support TCO (Tail Call Optimisation), so it’s likely to be a memory hog on large arrays.

About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s