Seite 1 von 1

Homework 11

Verfasst: 23. Jan 2019 15:32
von Kai Meinhard
Hello all, :)

I have a question regarding test1 of task 1. It says:
test1 = take 20 (distinct [x `div` 3 | x <- [1..]]) == take 20 [0..]

The problem i see here is that the execution of [x `div` 3 | x <- [1..]] never terminates. That means that this test can never terminate.

My suggestion would be to change it to:
test1 = distinct (take 20 [x `div` 3 | x <- [1..]]) == take 6 [0..]

That way the creation of the list terminates and is checked against the correct list.

Is this correct or did I miss anything?

Regards,
Kai

Re: Homework 11

Verfasst: 23. Jan 2019 15:52
von 0b101101101
Hi! I think that would be the case if the code was run with strict evaluation (like Java, Scala, JS, C, etc.). Haskell uses lazy evaluation, so the individual elements of the infinite stream are not actually computed until needed. So just make sure you never use them?
( Also, I have a working solution for distinct, so I guess it should be possible. ^^ )

Re: Homework 11

Verfasst: 23. Jan 2019 15:57
von Kai Meinhard
Thank you for your fast answer!

I forgot about lazy evaluation, my solution was iterating over all elements.

Regards,
Kai