[plt-scheme] Mini code review request

From: dvanhorn at ccs.neu.edu (dvanhorn at ccs.neu.edu)
Date: Thu Apr 8 15:30:39 EDT 2010

I would try writing it without a fold first.

David


----- Original Message -----
From: "Greg Hendershott" <greghendershott at gmail.com>
To: "plt-scheme" <plt-scheme at list.cs.brown.edu>
Sent: Thursday, April 8, 2010 3:24:09 PM GMT -05:00 US/Canada Eastern
Subject: [plt-scheme] Mini code review request

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)
_________________________________________________
  For list-related administrative tasks:
  http://list.cs.brown.edu/mailman/listinfo/plt-scheme


Posted on the users mailing list.