Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.haskell > #355

Re: how to use foldl and map to do ring homomorphism on list

From En Hest <hest@wait.what>
Newsgroups comp.lang.haskell
Subject Re: how to use foldl and map to do ring homomorphism on list
Date 2016-02-08 15:09 +0000
Organization A noiseless patient Spider
Message-ID <slrnnbhc2b.pj9.hest@localhost.localdomain> (permalink)
References <2244c1c1-824f-4fa1-8890-36d9baedb8d5@googlegroups.com>

Show all headers | View raw


* (2016-02-07 07:56+0000) Ho Yeung Lee:
> f(a + b) = f(a) + f(b) for all a and b in R
> f(ab) = f(a) f(b) for all a and b in R
>
> 1st definition
>
> map f (foldl (++) [] list) = foldl (++) [] (map f list))
>
> 2nd definition
> map f (foldl (*) list) = foldl (*) 1 (map f list) 

This doesn't look like valid syntax. Terms in parentheses on the left-hand side
should be type constructors, i.e. PascalCase. Further this seems to be partial
redefinition of map, using something beyond (n+1)-patterns, which afaik are
dead and buried.

I'm not familiar with ring homomorphism, but it looks like what you're trying
to do could be generalised with monoids, like so:

  foo :: (Monoid b, Foldable t) => (a -> b) -> t a -> b
  foo f = foldl ((<>) . f) mempty

or, pointfully

  foo f xs = foldl f' mempty xs
   where
    f' acc x = acc <> f x

This can be simplified to

  foo' :: (Monoid b) => (a -> b) -> [a] -> b
  foo' f = mconcat . map f

where mconcat is to blame for restricting us to lists.

See https://wiki.haskell.org/Monoid or
http://learnyouahaskell.com/functors-applicative-functors-and-monoids#monoids
for an introduction to monoids in haskell.

I … suspect I just did your homework? :P

-- 
mumble

Back to comp.lang.haskell | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

how to use foldl and map to do ring homomorphism on list Ho Yeung Lee <davidbenny2000@gmail.com> - 2016-02-06 23:56 -0800
  Re: how to use foldl and map to do ring homomorphism on list En Hest <hest@wait.what> - 2016-02-08 15:09 +0000

csiph-web