[Haskell-beginners] numerical types, the $ operator
Zachary Turner
divisortheory at gmail.com
Sun Mar 29 18:53:15 EDT 2009
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 (-)
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090329/62abc697/attachment.htm
More information about the Beginners
mailing list