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 falseredundant, it's equivalentx. - similarly
if x == true ...redundant , equivalentif x .... you need distinguish between
ioactions , results. example, if yo dodo <- getchar ...then in ...
irepresents result of action, character,i :: char.getchar :: io charaction itself. can view recipe returnscharwhen performed. can pass recipe around functions etc., , performed when executed somewhere.your
whilecalledfunktiontwice, isn't intend - read character twice, check first 1 , return second one. remember,funktionaction, each time "invoke" action (for example using<- funktion ...indonotation), action run again. should rather likedo y <- funktion x f <- praed y -- ...(my code different, checks argument passed it.)
Comments
Post a Comment