From: Jacques Garrigue <garrigue@kurims.kyoto-u.ac.jp>
To: proff@iq.org
Cc: caml-list@inria.fr
Subject: Re: list composition functions
Date: Mon, 16 Oct 2000 15:57:41 +0900 [thread overview]
Message-ID: <20001016155741M.garrigue@kurims.kyoto-u.ac.jp> (raw)
In-Reply-To: Your message of "16 Oct 2000 10:42:03 +1100" <wxg0lxisdw.fsf@foo.iq.org>
From: Julian Assange <proff@iq.org>
> Imagine you have the follow three functions,
>
> let mirror x = [x;x]
> let plus1 x = [x+1]
> let none x = []
>
> I'm trying to define an operator (>>) that will then operate like so
>
> (mirror >> mirror >> plus1) [1]
>
> [2;2;2;2]
>
> let (>>) a b l = List.flatten (List.map b (List.flatten (List.map a l)))
>
> # (>>);;
> - : ('a -> 'b list) -> ('b -> 'c list) -> 'a list -> 'c list = <fun>
>
> While this looks okay, and works fine for two applications, the list
> type keeps on growing with each partial application.
>
> plus1 >> plus1 >> plus1 >> plus1;;
> - : int list list list -> int list = <fun>
> #
>
> Is there anyway I can prevent this, short of making plus1, etc symmetric
> with respect to their argument types?
The problem is that your (a >> b) does not bear any symmetry with the
argument functions.
I can see at least two candidates for this.
1) The bind of the non-determinism monad:
# let (>>) l f = List.flatten (List.map f l);;
val ( >> ) : 'a list -> ('a -> 'b list) -> 'b list = <fun>
# [1] >> mirror >> mirror >> plus1;;
- : int list = [2; 2; 2; 2]
2) Flattening composition
# let (>>) a b x = List.flatten (List.map b (a x));;
val ( >> ) : ('a -> 'b list) -> ('b -> 'c list) -> 'a -> 'c list = <fun>
# (mirror >> mirror >> plus1) 1;;
- : int list = [2; 2; 2; 2]
They both slightly differ from what you were asking for, but I see no
other way to proceed as long as your basic blocks have type ('a -> 'b
list).
Jacques Garrigue
next prev parent reply other threads:[~2000-10-16 7:50 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2000-10-15 23:42 Julian Assange
2000-10-16 6:57 ` Jacques Garrigue [this message]
2000-10-16 7:58 ` Alain Frisch
2000-10-16 12:22 ` Julian Assange
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20001016155741M.garrigue@kurims.kyoto-u.ac.jp \
--to=garrigue@kurims.kyoto-u.ac.jp \
--cc=caml-list@inria.fr \
--cc=proff@iq.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox