Function 在Haskell函数中使用堆栈
我想在Haskell函数中使用堆栈,但我不知道如何使用它。我的函数应该是这样工作的:Function 在Haskell函数中使用堆栈,function,haskell,stack,Function,Haskell,Stack,我想在Haskell函数中使用堆栈,但我不知道如何使用它。我的函数应该是这样工作的: 拿绳子 将此输入字符串的某些元素放入输出字符串,并将其他元素放入堆栈 也将元素弹出到该输出字符串 递归执行2和3,直到堆栈为空 堆栈为空时打印输出字符串 我不知道何时何地创建该堆栈。我自己也不知道,因为我对Haskell编程非常陌生。因为我没有创建任何代码,所以也不能显示任何代码。你能告诉我这个函数在算法上是什么样的吗?我应该在哪里定义堆栈和输出字符串?谢谢。这里有一件很舒服的事情,标准Haskell列表是一个
我不知道何时何地创建该堆栈。我自己也不知道,因为我对Haskell编程非常陌生。因为我没有创建任何代码,所以也不能显示任何代码。你能告诉我这个函数在算法上是什么样的吗?我应该在哪里定义堆栈和输出字符串?谢谢。这里有一件很舒服的事情,标准Haskell列表是一个很好的堆栈(当然,要记住堆栈是一种更受限制的列表)。您的函数可能如下所示:
--takes one string and uses a stack to convert it to another string
doSomethingWithStack :: String -> [String] -> String
doSomethingWithStack str stack =
let str' = --here you embody your points 2 and 3
stack' = --stack top is (head stack), push is (x : stack), pop is (tail stack)
--... any change you'd want to make to any value turns into a new variable
in case stack'' of --check the final variables
[] -> str'' --if stack is empty, end
_ -> doSomethingWithStack str'' stack'' --if not, repeat
--now, to make it pretty
fancyWrapper :: String -> String
fancyWrapper str = doSomethingWithStack str [] -- empty list is an empty stack
--because you should strive to separate pure and impure functions
--, I suggest that you do the print elsewhere, say
main = do
str <- getLine
print $ fancyWrapper str
——获取一个字符串并使用堆栈将其转换为另一个字符串
doSomethingWithStack::String->[String]->String
doSomethingWithStack str stack=
让str'=--在这里你体现了你的第2点和第3点
堆栈'=--堆栈顶部为(头堆栈),推送为(x:堆栈),弹出为(尾堆栈)
--... 您想要对任何值所做的任何更改都会变成一个新变量
如果堆栈“”为--,请检查最终变量
[]->str'--如果堆栈为空,则结束
_->doSomethingWithStack str“”stack“”--如果不是,请重复
--现在,让它变得漂亮
fancyWrapper::字符串->字符串
fancyWrapper str=doSomethingWithStack str[]--空列表是一个空堆栈
--因为你应该努力区分纯函数和非纯函数
--,我建议你到别处去印,比如说
main=do
str这里有一件舒服的事情,标准Haskell列表是一个很好的堆栈(当然,要记住堆栈是一种更受限制的列表)。您的函数可能如下所示:
--takes one string and uses a stack to convert it to another string
doSomethingWithStack :: String -> [String] -> String
doSomethingWithStack str stack =
let str' = --here you embody your points 2 and 3
stack' = --stack top is (head stack), push is (x : stack), pop is (tail stack)
--... any change you'd want to make to any value turns into a new variable
in case stack'' of --check the final variables
[] -> str'' --if stack is empty, end
_ -> doSomethingWithStack str'' stack'' --if not, repeat
--now, to make it pretty
fancyWrapper :: String -> String
fancyWrapper str = doSomethingWithStack str [] -- empty list is an empty stack
--because you should strive to separate pure and impure functions
--, I suggest that you do the print elsewhere, say
main = do
str <- getLine
print $ fancyWrapper str
——获取一个字符串并使用堆栈将其转换为另一个字符串
doSomethingWithStack::String->[String]->String
doSomethingWithStack str stack=
让str'=--在这里你体现了你的第2点和第3点
堆栈'=--堆栈顶部为(头堆栈),推送为(x:堆栈),弹出为(尾堆栈)
--... 您想要对任何值所做的任何更改都会变成一个新变量
如果堆栈“”为--,请检查最终变量
[]->str'--如果堆栈为空,则结束
_->doSomethingWithStack str“”stack“”--如果不是,请重复
--现在,让它变得漂亮
fancyWrapper::字符串->字符串
fancyWrapper str=doSomethingWithStack str[]--空列表是一个空堆栈
--因为你应该努力区分纯函数和非纯函数
--,我建议你到别处去印,比如说
main=do
谢谢你的回答。函数一开始应该只获取一个字符串。这是否符合您提供的结构?当您说“获取字符串”时,您的意思是从命令行?如果是这样,在main
的do
块中添加getLine
。我的意思是使用hugs:function“stackoverflow”可以这样调用函数,但堆栈从何而来?它开始是空的吗?是的,它开始是空的,它必须在函数内部的某个地方创建,我不知道在哪里。谢谢你的回答。函数一开始应该只获取一个字符串。这是否符合您提供的结构?当您说“获取字符串”时,您的意思是从命令行?如果是这样,在main
的do
块中添加getLine
。我的意思是使用hugs:function“stackoverflow”可以这样调用函数,但堆栈从何而来?它开始是空的吗?是的,它开始是空的,它必须在函数内部的某个地方创建,我不知道在哪里。