r/shittyprogramming • u/Alekzcb • Jul 11 '19
Generating Intervals in Haskell
I find myself often wanting lists of consecutive numbers like [3,4,5,6,7] but it's very difficult to make these in haskal since it doesn't have any for-loops. But I've come up with a simple function you can stick in you code that will do this for you -- no more hassle :)
interval :: Num a => Int -> Int -> [a]
interval = ((fromIntegral.length<$>).).flip(.)((iterate(([]:).(<$>)(():))(fail$undefined)!!).succ).drop
interval 3 7 ~> [3,4,5,6,7]
33
Upvotes
7
u/yayroos Jul 11 '19
i regret knowing enough haskell to comprehend this.
Just, why?
Why?
(Sidenote why would anyone do anything in haskell, that most cursed of languages)
5
u/idk1415 Jul 11 '19
Could someone explain what this does?
14
u/milksnatcher37 Jul 11 '19
Do you want to talk about recursive functions, our lord and savior?
23
u/Basmannen Jul 11 '19
S U C C
2
u/kobbled Jul 11 '19
Oof, that physically hurts
2
u/dethnight Jul 11 '19
I read this as "Generating Interviews in Haskell" and it was made me laugh
1
u/[deleted] Jul 11 '19
I was very confused until I saw what sub this was
1
u/TheTsar Jul 11 '19
Damnit. I was under the impression that this was posted in r/Haskell while I was reading it. I kept thinking to myself “wow, there really is always more to functional programming in Haskell. I’m sure that this generates a graph that is more generic than other algorithms — I’m just not sure why. Oh, damn me for not understanding the language enough to make sense of this beautiful snowflake function.”
And then I realized that I was in r/shittyprogramming