Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using composition to get ceiling of a division

I'm learning haskell, and I'm trying to rewrite a function using composition only

Here's the function I'm trying to refactor:

ceilingDiv a b = ceiling (a / b)

So far I managed to make it work using curry and uncurry but it feels dirty:

ceilingDiv = curry $ ceiling . uncurry (/)

Is there any way to do this more cleanly? I was thinking ceiling . div, but it doesn't work because (/) returns a function and ceiling takes a Double as its argument.

like image 602
Isthos Avatar asked Dec 08 '22 09:12

Isthos


1 Answers

There's a fun site called https://pointfree.io - it provides a solution to your problem like this: ceilingDiv = (ceiling .) . (/). It looks ugly because point-free composition with multiple arguments is a pain. Sometimes it's achieved by using . sections like here, sometimes by using an Applicative instance of functions and the <*> operator.

I think your solution with curry-uncurry is nice. It deals with passing multiple arguments by wrapping them into one tuple.

like image 118
MorJ Avatar answered Feb 14 '23 07:02

MorJ