Haskell snippet - getPrefixMap

Comments

[this is good]
It seems that the comment box does not like "pre" tags! My alternative is:

infix 1 ><
(><) f g a = (f a, g a)

getPrefixes1 :: [([a], t)] -> [([a], t)]
getPrefixes1 = concatMap ((uncurry zip) . (tail.inits.fst >< slist))
where slist (f,s) = replicate (length f) s

I am not sure if the categorical product (><) is defined in Haskell, but, even if it isn't, it is very simple.

Good luck with the talk,
Joao
Yeah, Vox likes to eat haskell comments, sorry ;-)

Good point about zip! I could just use (repeat s) along with that, will have a play with it.


Of course, repeat! :)

Third version:

getPrefixes = concatMap ((uncurry zip) . (tail.inits.fst >< repeat.snd))

And it is done :)

Ah! OK, I see what you're doing with the (><) operator, basically it's a hack to let you use the "a" parameter in 2 places, to use the currying simplification?
Thanks, I've updated the main post.
Well, products allow you to "glue" functions that do not compose. Moreover, you get a pointfree definition which can be much more useful for calculation purposes!

If you are interested, more details in page 12 of:
http://www.di.uminho.pt/~jno/ps/pdbc02_new.pdf

Joao

[this is good]
You could also use *** instead of &&&: for the function instance of Arrow, we have

f *** g = \(x,y) -> (f x, g y)

i.e., it applies f and g to a pair "in parallel".

So, you can write

getPrefixes = concatMap $ uncurry zip . (tail . inits *** repeat)

even shorter! =)

Post a comment

Already a Vox member? Sign in