Skip to content

Instantly share code, notes, and snippets.

@mbloms
Last active December 2, 2017 14:03
Show Gist options
  • Save mbloms/b76940a37034dc9ccbc85c078faa32d9 to your computer and use it in GitHub Desktop.
Save mbloms/b76940a37034dc9ccbc85c078faa32d9 to your computer and use it in GitHub Desktop.
En kvällskluring; implementera en funktion som gör följande: [2 3 5 10] -> [150 100 60 30] (varje tal i outputen är produkten av hela inputen, bortsett från talet på samma index) Naiv lösning är trivial, men om lösningen INTE får vara O(n^2) och INTE får använda division? Hur smart kan man göra den?
funkyProduct lst = zipWith
(*)
(scanl (*) 1 lst)
(scanr (*) 1 $ (tail lst))
--Prelude> funkyProduct [2,3,5,10]
--[150,100,60,30]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment