While loop in Haskell with a condition -
i'm having little haskell situation on here. i'm trying write 2 functions monads. first 1 supposed iterate through function long condition true input / output of function. second 1 supposed use first 1 take number input , write output until enter space.
i'm stuck this, help?
module test while :: (a -> bool) -> (a -> io a) -> -> io while praed funktion x = f <- praed (funktion x) if f == true y <- funktion x while praed funktion y else return x power2 :: io () power2 = putstr (please enter number.") <- getchar while praed funktion praed x = if x /= ' ' false else true funktion =
import control.monad while :: (a -> bool) -> (a -> io a) -> -> io while praed funktion x | praed x = y <- funktion x while praed funktion y | otherwise = return x power2 :: io () power2 = putstr "please enter number." <- getchar let praed x = x /= ' ' let f x = putchar x getchar while praed f '?' return ()
some notes:
- using
if x true else false
redundant, it's equivalentx
. - similarly
if x == true ...
redundant , equivalentif x ...
. you need distinguish between
io
actions , results. example, if yo dodo <- getchar ...
then in ...
i
represents result of action, character,i :: char
.getchar :: io char
action itself. can view recipe returnschar
when performed. can pass recipe around functions etc., , performed when executed somewhere.your
while
calledfunktion
twice, isn't intend - read character twice, check first 1 , return second one. remember,funktion
action, each time "invoke" action (for example using<- funktion ...
indo
notation), action run again. should rather likedo y <- funktion x f <- praed y -- ...
(my code different, checks argument passed it.)
Comments
Post a Comment