[Haskell-cafe] Re: Lazy IO and closing of file handles
Isaac Dupree
isaacdupree at charter.net
Mon Mar 19 16:48:24 EDT 2007
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Pete Kazmier wrote:
> Bryan O'Sullivan <bos at serpentine.com> writes:
>
>> Pete Kazmier wrote:
>>
>>> I understand the intent of this code, but I am having a hard time
>>> understanding the implementation, specifically the combination of
>>> 'fix', 'flip', and 'interate'. I looked up 'fix' and I'm unsure how
>>> one can call 'flip' on a function that takes one argument.
>
>> As to why it's okay to call "flip" on "fix" at all, look at the types
>> involved.
>>
>> fix :: (a -> a) -> a
>> flip :: (a -> b -> c) -> b -> a -> c
>>
>> By substitution:
>>
>> flip fix :: a -> ((a -> b) -> a -> b) -> b
>
> Sadly, I'm still confused. I understand how 'flip' works in the case
> where its argument is a function that takes two arguments. I've
> started to use this in my own code lately. But my brain refuses to
> understand how 'flip' is applied to 'fix', a function that takes one
> argument only, which happens to be a function itself. What is 'flip'
> flipping when the function passed to it only takes one argument?
fix :: (a -> a) -> a
In this case, we know something about 'a': it is a function (b -> c).
Substitute:
fix :: ((b -> c) -> (b -> c)) -> (b -> c)
Take advantage of the right-associativity of (->)
fix :: ((b -> c) -> b -> c) -> b -> c
Now it looks like a function of two arguments, because the return value
(normally ordinary data) can in fact, in this case, take arguments.
Here's another example of that:
data Box a = Box a
get (Box a) = a
- -- get (Box 1) :: Int
- -- get (Box (\a -> a)) :: Int -> Int
- -- (get (Box (\a -> a))) 1 :: Int
--function application is left-associative:
- -- get (Box (\a -> a)) 1 :: Int
- -- flip get 1 (Box (\a -> a)) :: Int
Yes, it sometimes confuses me too.
Isaac
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFF/vcXHgcxvIWYTTURAj5RAKCUMeAF0vosJ6ROAVlBIDHsEq/vzgCfflnR
50BmW6tuAF6mKXBtrlHdQ5Y=
=uv3G
-----END PGP SIGNATURE-----
More information about the Haskell-Cafe
mailing list