Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell program will not end if input = 0

Tags:

haskell

When the user inputs number m = 0, the program will never stop counting. Is there any way how to take care of that? so if the user inputs 0 the program will end.

import Control.Monad (replicateM)

transpose :: [[a]]->[[a]]
transpose ([]:_) = []
transpose x = (map head x) : transpose (map tail x)
.
.
.
like image 788
Adam Studenic Avatar asked Jan 23 '26 01:01

Adam Studenic


1 Answers

You missed a case in transpose that is triggered by entering 0:

transpose :: [[a]]->[[a]]
transpose [] = []
transpose ([]:_) = []
transpose x = (map head x) : transpose (map tail x)

And the above is quite dangerous, as 'head' and 'tail' might fail:

transpose [[0],[]]

[[0,* Exception: Prelude.head: empty list

like image 142
Chris Kuklewicz Avatar answered Jan 27 '26 00:01

Chris Kuklewicz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!