No subject


Wed Apr 10 17:18:51 CEST 2013


    snd $ fn' (zip [x] [a]) = fn [a]

I can see ways of doing this by altering fn, or by repeating some of fn in
the definition of fn', or (because in this case I know that if fn xs = x,
fn is returning the first x in xs and not any others), by doing something
nasty like:

  fn' xs = xs !! fromMaybe 0 (findIndex (\(_,a) -> a == fn (snd $ unzip
xs)) xs )

But it seems to me like there should be prettier solutions to this (that *do
not* involve changing the definition of fn). After all, this doesn't seem
like a rare pattern.

Anyone know if in fact there's a better way to go about it?

--047d7bdc8c3e0cdc8c04dcc40090
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">I have a function that, simplifying a little, looks like t=
his:<div><br></div><div>=A0 fn :: [a] -&gt; a</div><div>=A0 fn =3D (head .)=
 . takeWhile $ (\_ -&gt; True)</div><div><br></div><div>From this, I want a=
 function with the signature fn&#39; :: [(x,a)] -&gt; (x,a) such that:</div=
>

<div><br></div><div>=A0 =A0 snd $ fn&#39; (zip [x] [a]) =3D fn [a]</div><di=
v><br></div><div style>I can see ways of doing this by altering fn, or by r=
epeating some of fn in the definition of fn&#39;, or (because in this case =
I know that if fn xs =3D x, fn is returning the first x in xs and not any o=
thers), by doing something nasty like:</div>
<div style><br></div><div style>=A0 fn&#39; xs =3D xs !! fromMaybe 0 (findI=
ndex (\(_,a) -&gt; a =3D=3D fn (snd $ unzip xs)) xs )<br></div><div style><=
br></div><div style>But it seems to me like there should be prettier soluti=
ons to this (that <i>do not</i>=A0involve changing the definition of fn). A=
fter all, this doesn&#39;t seem like a rare pattern.</div>
<div style><br></div><div style>Anyone know if in fact there&#39;s a better=
 way to go about it?</div><div><br></div></div>

--047d7bdc8c3e0cdc8c04dcc40090--



More information about the Beginners mailing list