[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Scheme-reports] Pattern matching and list comprehensions



Howdie,

2014-03-21 20:38 GMT+01:00 Denis Trapeznikoff <denin@x>:
>
> > Haskell is a very nice language, but -- compared to Scheme -- it
> > conveys a lot of information in its complex syntax. Correct me if I'm
> > wrong, but I don't think that it would allow you to define the "."
> > operator if it wasn't already in the language.
>
> Actually, there is no problem in that:
>
> import Prelude hiding ((.))
>
> main = (putStrLn . concat) ["Hell", "o!"]
>
> (.) :: (b -> c) -> (a -> b) -> (a -> c)
> f . g = (\x -> f (g x))
>
> Haskell is a functional language after all! :)

Nice! :)

> Interestingly, when I was exploring Scheme for the first time, I have
> also been introducing such operators as
>
> (define ($_ f . x)
>   (lambda y (apply f (append x y))))
>
> (define (_$ f . y)
>   (lambda x (apply f (append x y))))
>
> ((append) is not needed here.)

As Alan shows, it is. The variadic version of apply works by consing
its previous arguments (except the first one) with the last one, so
e.g.
(apply f x y)
is an equivalent of
(apply f (cons x y))

> > It's not only that -- it is also relevant how arguments are applied to
> > the predicate. To be honest, I have no intuition of whether such
> > generalization is good. Can you come up with any real-world usage
> > example? (Like: multiple argument map allows you to easily define the
> > code that computes the dot product)
>
> Sorry, I don't have production examples right before my eyes now, so
> anything I come up with would be somewhat artificial.
> Let's say you have two function graphs in two buffers and want to compute
> their relation at all points where the denominator is not zero:
>
> (define nom-list)
> (define denom-list)
> (define ratio-list (call-with-values (lambda () (filter (lambda (a b) (not
> (zero? b))) nom-list denom-list)) (lambda (l1 l2) (map / l1 l2))))
>
> (I intentionally have not used any more procedures of prelude.scm in this
> example.)

I'll introduce indentation for better readability:

(define nom-list)
(define denom-list)
(define ratio-list
  (call-with-values
    (lambda ()
       (filter (lambda (a b) (not (zero? b)))
                nom-list denom-list))
    (lambda (l1 l2) (map / l1 l2))))

So the body of ratio-list definition is an srfi-11-equivalent of

(let-values (((l1 l2) (filter (lambda (a b) (not (zero? b)))
                                   nom-list denom-list)))
  (map / l1 l2))

With srfi-1, this problem would be better solved using filter-map:

(filter-map (lambda (a b)
                  (and (not (zero? b)) (/ a b)))
                nom-list denom-list)

If you come up with some other examples, I'd be eager to see them
(I'll try to trace some too). No rush, of course. But there's no other
way for trying whether something is useful than just testing it in
real-world programs.

Z poważaniem,
M.

_______________________________________________
Scheme-reports mailing list
Scheme-reports@x
http://lists.scheme-reports.org/cgi-bin/mailman/listinfo/scheme-reports