Can someone please explain or give some resources on how function composition works in relation to laziness?
For example how does filter (/='W') . map toUpper $ "justaword"
filter (/='W') . map toUpper $ "justaword"
work in Haskell compared to it's counterpart in erlang which is not lazy?
Every time another character is demanded (or notification of end), the next character - if any - is mapped to uppercase, that is compared to 'W', delivered if unequal.
filter (/= 'W') . map toUpper $ "justaword"
~> filter (/= 'W') (toUpper 'j' : map toUpper "ustaword")
~> filter (/= 'W') ('J' : map toUpper "ustaword")
~> 'J' : filter (/= 'W') (map toUpper "ustaword")
Now the first character is available, so for queries like null
or functions like take 1
, no further work is done. If more characters are demanded by the consumer, they will be produced one by one until the end of the string is reached.
Example:
Prelude Data.Char> take 10 . filter (/= 'W') . map toUpper $ repeat 't'
"TTTTTTTTTT"
repeat
produces an infinite list, but as long as only a finite part is consumed, the computation finishes in finite time. However, take 10 . filter (/= 'W') . map toUpper $ repeat 'w'
take 10 . filter (/= 'W') . map toUpper $ repeat 'w'
take 10 . filter (/= 'W') . map toUpper $ repeat 'w'
would not terminate, since none of the produced characters passes the filter
to reach the take 10
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.