在Haskell中随着条件循环 [英] While loop in Haskell with a condition
问题描述
只要条件对于函数的输入/输出为真,首先应该遍历一个函数。第二个应该使用第一个输入数字作为输入并将其写入输出,直到您输入空格为止。
我坚持这一点,任何帮助?
module测试其中
pre
while ::(a - > Bool) - > (a - > 10 a) - > a - > IO a
,同时praed funktion x = do
f< - praed(funktion x)
if f == True然后通过< - funktion x
做
funktion y
else return x
power2 :: IO()
power2 = do putStr(请输入一个数字。)
i < - getChar
,同时表示函数
,其中表示x = if x / =''then False else True
funktion = i
解决方案
import Control.Monad
while :: (a - > Bool) - >(a - > IO a) - > a - > IO a
同时表达函数x
| praed x = do
y< - funktion x
,同时表示函数y
|否则=返回x
power2 :: IO()
power2 = do
putStr 请e
i< - getChar
let praed x = x / =''
let fx = do
putChar x
getChar
while praed f'?'
return()
一些注释:
- 使用
if x then True else False
是多余的,它相当于x
。 - 同样
if x == True ...
是多余的,相当于if x ...
。 -
您需要区分
IO
他们的结果。举例来说,如果你做的是
do
i < - getChar
...
然后在...
i
表示动作的结果,一个字符,所以i :: Char
。但是getChar :: IO Char
是操作本身。您可以将其视为执行时返回Char
的配方。您可以将配方传递给函数等,并且只在执行时执行。 $ c>称为
funktion
两次,这可能不是你想要的 - 它会读取一个字符两次,检查第一个并返回第二个。记住,你的 funktion
是一个动作,所以每次你调用动作(例如使用< - funktion ... 在 do
表示法中),则再次运行该操作。所以它应该是像 do
y< - funktion x
f< - praed y
- ...
(我的代码有些不同,它检查参数被传递给它。)
I'm having a little Haskell Situation over here. I'm trying to write two functions with monads. First one is supposed to iterate through a function as long as the condition is true for the input / output of the function. Second one is supposed to use the first one to take a number as input and write it as output until you enter a space.
I'm stuck with this, any help?
module Test where
while :: (a -> Bool) -> (a -> IO a) -> a -> IO a
while praed funktion x = do
f <- praed (funktion x)
if f == True then do
y <- funktion x
while praed funktion y
else return x
power2 :: IO ()
power2 = do putStr (Please enter a number.")
i <- getChar
while praed funktion
where praed x = if x /= ' ' then False else True
funktion = i
import Control.Monad
while :: (a -> Bool) -> (a -> IO a) -> a -> IO a
while praed funktion x
| praed x = do
y <- funktion x
while praed funktion y
| otherwise = return x
power2 :: IO ()
power2 = do
putStr "Please enter a number."
i <- getChar
let praed x = x /= ' '
let f x = do
putChar x
getChar
while praed f '?'
return ()
Some notes:
- Using
if x then True else False
is redundant, it's equivalent to justx
. - Similarly
if x == True ...
is redundant and equivalent toif x ...
. You need to distinguish between
IO
actions and their results. For example, if yo dodo i <- getChar ...
then in ...
i
represents the result of the action, a character, soi :: Char
. ButgetChar :: IO Char
is the action itself. You can view it as a recipe that returnsChar
when performed. You can pass the recipe around to functions etc., and it is only performed when executed somewhere.Your
while
calledfunktion
twice, which probably isn't what you intend - it would read a character twice, check the first one and return the second one. Remember, yourfunktion
is an action, so each time you "invoke" the action (for example by using<- funktion ...
in thedo
notation), the action is run again. So it should rather be something likedo y <- funktion x f <- praed y -- ...
(My code is somewhat different, it checks the argument that is passed to it.)
这篇关于在Haskell中随着条件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!