Just curious why you would use that imperative example with Racket, and not an imperative language like Python, or even Haskell? You parenthetically mention (match) (ha!) but to someone who doesn't know the languages, give the impression that Racket can't do pattern matching.
Anyway, this is the analogous `map` in racket:
(define (my-map f lst)
(match lst
['() '()]
[(cons hd tl) (cons (f hd) (my-map f tl))]))
It's a 1-to-1 correspondence, and is arguably the idiomatic way to define `map` in Racket.I was largely using Racket as a common enough Scheme that I have installed my computer. I wanted an example from another functional language but in an older classical style (I was shooting for the Little Schemer here). I'm quite familiar with Racket's match, somewhere around my blog I even have a "proof" of sorts of using Racket's match to do the famous Haskell quicksort in just as few lines of code.
If you really want an ugly rendition I could try and replicate one of the awful JavaScript versions of "map" that uses a for loop, but I thought that might obscure the comparison of operations too much, and the semantics wouldn't be equivalent (linked lists vs. JS arrays).
Re:How to stop functional programming
by abecedarius on Tuesday February 23, 2016 @11:28AM
(#11159999)
That wasn't quite a fair comparison.
map f xs =
if empty xs
then []
else f (head xs) : map f (tail xs)
(I like pattern matching too.)The point isn't to be a "fair" comparison, but rather to compare the number of explicit operations involved. I'd note that your Haskell example there is largely equivalent to the Scheme one.
Re:How to stop functional programming
by abecedarius on Tuesday February 23, 2016 @08:49PM
(#11163919)
Yes, it's equivalent on purpose. Yet in Haskell it's only a couple tokens longer than the pattern-matching original which you called "far more terse". To fairly test a claim, one should change only the variables the claim is about.
This is a mild example too, it gets far, far more useful when you're dealing with type/struct fields and the like.