Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

re: What is the main advantage of macros over functions?

106 views
Skip to first unread message

WJ

unread,
May 21, 2012, 5:09:40 PM5/21/12
to
Rainer Joswig wrote:

> (defun curry (function arg)
> (lambda (&rest args)
> (apply function arg args)))
>
> (defun call-with-stream (function file)
> (with-open-file (stream file)
> (funcall function stream)))
>
> (defun count-lines-in-stream (stream)
> (loop for line = (read-line stream nil nil)
> while line count line))
>
> Now you want to count lines of some files.
>
> CL-USER 5 > (reduce #'+
> (mapcar
> (curry #'call-with-stream #'count-lines-in-stream)
> (directory "http:lw;**;.lisp")))
> 5559
>
> Not many LAMBDAs here, but combining functions.
>
> It is called Functional Programming and pretty powerful.

Racket:

(define (count-lines-in-file file-name)
(with-input-from-file file-name
(lambda() (sequence-length (in-lines)))))

(for/sum
([file-name (find-files (curry regexp-match "\\.bak"))])
(count-lines-in-file file-name))

WJ

unread,
May 21, 2012, 6:02:55 PM5/21/12
to
Pascal Costanza wrote:

> (loop for path in (directory "http:lw;**;.lisp") sum
> (with-open-file (stream path)
> (loop while (read-line stream nil nil) count t)))


Racket:

(for/sum ([file (find-files (curry regexp-match "\\.bak"))])
(with-input-from-file file (thunk* (sequence-length (in-lines)))))
0 new messages