[plt-scheme] Mini code review request

From: Greg Hendershott (greghendershott at gmail.com)
Date: Thu Apr 8 15:24:09 EDT 2010

If anyone has time to do a mini code review, I'd appreciate the feedback.

Welcome any comments, but mainly I'm curious about my experiment to
stop being Mr. Imperative. Avoid iterating and mutating. Instead
embrace map and fold. Don't just sip the kool aid, actually swallow
it.

I ended up with a (rest (reverse (flatten ...))) sequence which seems
a bit of a pretzel.

I'm looking at this code with the following questions:
1. Is this clean and simple ... or too "clever"?
b. Is this reasonably idiomatic ... or am I still speaking Scheme as a
second language?
iii. How would you native speakers write it?
100. Should I spit up the kool aid and revert to my old iterative ways? :)

Thanks in advance.

Code:

---------

#lang scheme

(require net/uri-codec)
(require test-engine/scheme-tests)

(define/contract (pairs->params-string lst) ((listof (list/c string?
string?)) . -> . string?)
  ; Convert e.g.
  ; '( ("Param1" "Value1")
  ;    ("Param2" "Value2")
  ;    ("Param3" "Value3") )
  ; to
  ; "Param1=Value1&Param2=Value2&Param3=Value3"
  ;
  ; The point of
  ;   (rest (reverse (flatten (list "key" "=" "val" "&") ... )))
  ; is to get the ampersands only BETWEEN, not start or end.
  (if (empty? lst)
      ""
      (foldl string-append
             ""
             (rest (reverse (flatten (map (lambda (x)
                                            (list
(form-urlencoded-encode (first x)) ; key
                                                  "="

(form-urlencoded-encode (second x)) ; value
                                                  "&"))
                                          lst)))))))

(check-expect (pairs->params-string '()) "")
(check-expect (pairs->params-string '( ("foo" "bar" ) )) "foo=bar" )
(check-expect (pairs->params-string  '( ("Param1" "Value1") ("Param2"
"Value2") ("Param3" "Value3") ))
"Param1=Value1&Param2=Value2&Param3=Value3" )
(test)


Posted on the users mailing list.