Homework 11

Moderator: Konzepte der Programmiersprachen

Kai Meinhard
Neuling
Neuling
Beiträge: 5
Registriert: 28. Okt 2016 14:34

Homework 11

Beitrag von Kai Meinhard » 23. Jan 2019 15:32

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

0b101101101
Endlosschleifenbastler
Endlosschleifenbastler
Beiträge: 179
Registriert: 15. Apr 2015 18:24

Re: Homework 11

Beitrag von 0b101101101 » 23. Jan 2019 15:52

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. ^^ )

Kai Meinhard
Neuling
Neuling
Beiträge: 5
Registriert: 28. Okt 2016 14:34

Re: Homework 11

Beitrag von Kai Meinhard » 23. Jan 2019 15:57

Thank you for your fast answer!

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

Regards,
Kai

Antworten

Zurück zu „Konzepte der Programmiersprachen“