CL (COBOL-Like) sorts mindlessly

  • Follow


COBOL-Like (SBCL):

(defun myfunc (n) (print "It's COBOL!") (- n))
(sort '(2 0 1 4 3) '<  :key #'myfunc)

"It's COBOL!"
"It's COBOL!"
"It's COBOL!"
"It's COBOL!"
"It's COBOL!"
"It's COBOL!"
"It's COBOL!"
"It's COBOL!"
"It's COBOL!"
"It's COBOL!"
"It's COBOL!"
(4 3 2 1 0)

MatzLisp:

Note that the "decorating" function is called only
once for each item in the list.

[2, 0, 1, 4, 3].sort_by{|n| puts "It's Lisp!"; -n}
It's Lisp!
It's Lisp!
It's Lisp!
It's Lisp!
It's Lisp!
    ==>[4, 3, 2, 1, 0]
0
Reply w_a_x_man (2778) 3/19/2012 1:48:58 AM

On Mon, 19 Mar 2012 01:48:58 +0000, WJ wrote:

> Note that the "decorating" function is called only once for each item in
> the list.
> 
> [2, 0, 1, 4, 3].sort_by{|n| puts "It's Lisp!"; -n} It's Lisp!
> It's Lisp!

It's not! No s-expressions, no homoiconicity, no Lisp. Smells rather like 
Python to me, in fact.

0
Reply dagnabbit (2) 3/19/2012 1:52:51 AM


WJ wrote:

> COBOL-Like (SBCL):
> 
> (defun myfunc (n) (print "It's COBOL!") (- n))
> (sort '(2 0 1 4 3) '<  :key #'myfunc)
> 
> "It's COBOL!"
> "It's COBOL!"
> "It's COBOL!"
> "It's COBOL!"
> "It's COBOL!"
> "It's COBOL!"
> "It's COBOL!"
> "It's COBOL!"
> "It's COBOL!"
> "It's COBOL!"
> "It's COBOL!"
> (4 3 2 1 0)
> 
> MatzLisp:
> 
> Note that the "decorating" function is called only
> once for each item in the list.
> 
> [2, 0, 1, 4, 3].sort_by{|n| puts "It's Lisp!"; -n}
> It's Lisp!
> It's Lisp!
> It's Lisp!
> It's Lisp!
> It's Lisp!
>     ==>[4, 3, 2, 1, 0]

Racket:

(define (mylength str)
  (displayln str)
  (string-length str))

(sort '("fleece" "piece" "ease" "is" "was")
      <  #:key mylength  #:cache-keys? #t)


fleece
piece
ease
is
was
'("is" "was" "ease" "piece" "fleece")
0
Reply w_a_x_man (2778) 4/22/2012 8:32:07 AM

> Note that the "decorating" function is called only
> once for each item in the list.

I see no value in caching keys: if you use simple, fast accessor (like 
CAR or struct field lookup) caching will only make it slower.

In rare cases when you use some expensive function for key you can make 
sure it's called only once explicitly:

(mapcar #'car
    (sort (mapcar (lambda (thing) (cons thing (expensive-key thing)))
                  things)
          #'> :key #'cdr)))

Or you can wrap this into a function like sort-with-cached-keys.

Why do you think that lack of some obscure and unnecessary feature makes 
Lisp a COBOL?
0
Reply alex.mizrahi (227) 4/22/2012 10:46:09 AM

On Sunday, April 22, 2012 12:46:09 PM UTC+2, Alex Mizrahi wrote:
> > Note that the "decorating" function is called only
> > once for each item in the list.
> 
> I see no value in caching keys: if you use simple, fast accessor (like 
> CAR or struct field lookup) caching will only make it slower.
> 
> In rare cases when you use some expensive function for key you can make 
> sure it's called only once explicitly:
> 
> (mapcar #'car
>     (sort (mapcar (lambda (thing) (cons thing (expensive-key thing)))
>                   things)
>           #'> :key #'cdr)))
> 
> Or you can wrap this into a function like sort-with-cached-keys.
> 
> Why do you think that lack of some obscure and unnecessary feature makes 
> Lisp a COBOL?

In reality it should.  CL is a "business oriented" language. :)

Cheers
--
MA
0
Reply marcoxa1 (977) 4/23/2012 7:59:41 AM

4 Replies
16 Views

(page loaded in 0.202 seconds)


Reply: