[Haskell-cafe] How is laziness defined?
TJ
tjay.dreaming at gmail.com
Mon Feb 5 00:37:36 EST 2007
On 2/5/07, ajb at spamcop.net <ajb at spamcop.net> wrote:
> Quoting TJ <tjay.dreaming at gmail.com>:
>
> > I would think that with 100% laziness, nothing would happen until the
> > Haskell program needed to output data to, e.g. the console. Quite
> > obviously that's not it. So how is laziness defined in Haskell?
>
> It means that the program behaves as if "things" are evaluated if and
> only if they are needed. "Needed" in the Haskell sense, means "needed
> to do I/O".
So it's just IO which makes things run huh? OK that's basically what I
said there. Cool.
> > This is one of the things that just boggles my mind everytime I try to
> > wrap it around this thing called Haskell ;)
>
> The cool part is that for the most part, it doesn't matter. It just
> works. If you ever come across a case where it doesn't just work (e.g.
> if you need a tilde pattern), you'll be ready for it.
I despise using what I don't understand. It's a big problem but one
which is more insurmountable than understanding Haskell, I think...
On 2/5/07, Andrew Wagner <wagner.andrew at gmail.com> wrote:
> I found it useful to work through an example where lazy evaluation was
> important, and wrote it up in a tutorial. It may or may not help you,
> no guarantees, but here it is:
> http://www.haskell.org/haskellwiki/Haskell/Lazy_Evaluation
With the code from your tutorial,
magic :: Int -> Int -> [Int]
magic 0 _ = []
magic m n = m : (magic n (m+n))
getIt :: [Int] -> Int -> Int
getIt [] _ = 0
getIt (x:xs) 1 = x
getIt (x:xs) n = getIt xs (n-1)
and the example expression,
getIt (magic 1 1) 3
It only gets run (starts pattern matching and all) if I do a print on
it, or run it from GHCi (which will do the"print" for me), right?
Thanks,
TJ
More information about the Haskell-Cafe
mailing list