On Sun, Jun 21, 2015 at 7:13 PM, Bahman Movaqar <b.mova...@gmail.com> wrote:
> On Monday, 22 June 2015 02:34:43 UTC+4:30, Bahman Movaqar  wrote:
>> Assuming
>>
>>     (define my-list ("a" 1 "b" 2.5 "c" #t "d" "hi"))
>>
>> what is the idiomatic way to convert `my-list` into the following?
>>
>>     "[a=1][b=2.5][c=#t][d=hi]"
>>
>> I thought of `FORMAT` but apparently the `{}` (for recursion) are not 
>> available.
>> For example, in CL `(format nil "~{[~a:~a]~}" my-list)` would do the trick.
>
> This is what I've come up with so far; but it doesn't feel idiomatic at all 
> :-)
>
>     (string-join
>        (unfold null-list?
>                (lambda (x) (format "[~a=~a]" (first x) (second x)))
>                (lambda (x) (cdr (cdr x)))
>                my-list)
>        "")

This might be a bit more rackety:

(apply string-append
         (for/list ([(k v) (apply hash my-list)])
           (format "[~a=~a]" k v)))

It relies on the (happy?) accident that `hash` happens to accept a
flat list. This also makes it more expensive than a version that
doesn't use a hash.
Here's a hash-less alternative (though this one does use an unstable library):

(require unstable/sequence)

(apply string-append
         (for/list ([slice (in-slice 2 my-list)])
           (format "[~a=~a]" (car slice) (cadr slice)))))

-- 
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