[Haskell-beginners] numerical types, the $ operator
Daniel Fischer
daniel.is.fischer at web.de
Sun Mar 29 19:47:30 EDT 2009
Am Montag 30 März 2009 00:53:15 schrieb Zachary Turner:
> On Sat, Mar 28, 2009 at 4:53 PM, John Dorsey <haskell at colquitt.org> wrote:
> > > > How would an experienced guy write this without parentheses?
> > >
> > > I'm fairly certain it's impossible to write it without using
> > > parentheses. I would probably just write
> > >
> > > x - fromInteger (floor x)
> >
> > Never impossible!
> >
> > flip subtract x . fromInteger $ floor x
> > case floor x of y -> x - fromInteger y
> > let y = floor x in x - fromInteger y
>
> I'm a bit of a beginner myself, but I came up with this:
>
> let (|>) x f = f x
> let mapping f x = (x, f x)
> let mapping2 f (x,y) = (x, f y)
> let frac x = x |> mapping id |> mapping2 floor |> mapping2 fromInteger |>
> uncurry (-)
But John didn't use
(-) x . fromInteger . floor $ x
because it has parentheses, like your version :)
That is easily fixed, though, and since almost everything you ever need has
already been coded by somebody else, let's use a library instead of (|>),
mapping and mapping2:
import Control.Arrow
frac :: RealFrac a => a -> a
frac = fromInteger . floor &&& id >>> uncurry subtract
pointfree and without parentheses.
f &&& g = \x -> (f x, g x)
(for functions, it's more generally applicable to arrows), so your
'mapping f' is 'id &&& f', your 'mapping2 f' would be 'second f', also defined in
Control.Arrow.
You see that these functions are so generally useful that they already are in a
library :)
(>>>) is forward composition (for functions, it's defined in Control.Category for
more general settings), useful and readable. You can't use it to inject the value
into the pipeline, though.
But often that is not necessary and pointfree style is equally readable
(sometimes even more readable).
>
> A little extreme, but I still like that it illustrates the |> operator,
> which is actually really useful, I borrowed the concept from F#. I
> redefined it because I actually have no idea if F# has a similar operator.
> Does it? It's obviously still easier to read the original parenthesized
> version, but sometimes the |> operator really makes things very readable,
> because it emphasizes the fact that you start with a single value, and send
> that value through a series of transformations one after the other, and you
> can read each transformation in the order that it happens, rather than with
> function composition where you have to scan to the end first to see which
> operation gets applied first.
More information about the Beginners
mailing list