简体   繁体   English

Haskell-有一个函数调用一个函数

[英]Haskell - have a function call a function

we are currently sitting on a task from university, which we don't fully understand (please no solution but only ideas or suggestions). 目前,我们正处于大学的一项任务中,我们对此尚不完全了解(请提供解决方案,仅提供想法或建议)。

What is given is a type: 给出的是一种类型:

type MyType = String -> String

Now we are trying to be able to have a function, which takes 2 Strings and a function (the type) and then gives a function (type) 现在,我们试图拥有一个函数,该函数接受2个字符串和一个函数(类型),然后给出一个函数(类型)

myCode :: String -> String -> MyType -> MyType

and we already implemented a function, which can be used as MyType one: 并且我们已经实现了一个可以用作MyType的函数:

emptyString :: MyType
emptyString :: (\a -> "")

The task is to be able to store several 2x Strings. 任务是能够存储几个2x字符串。 This is our current idea: 这是我们目前的想法:

myCode :: String -> String -> MyType ->MyType
myCode a b c = (\x -> b)

in this case we have an input String, which is "Hello" and another one which is "World" and then as c we put in the "emptyString". 在这种情况下,我们有一个输入String,它是“ Hello”,另一个是“ World”,然后在c中输入“ emptyString”。 This works for one String, because when we type the following in the console: 这适用于一个String,因为当我们在控制台中键入以下内容时:

a = (myCode "Hello" "World" emptyString) ""

we get "World" on input "a". 我们在输入“ a”上得到“世界”。 Now the hard part: We should be able to store several of these (searching them is another task, not needed right now). 现在最困难的部分是:我们应该能够存储其中的一些(搜索它们是另一项任务,现在不需要)。 We thought we might be able to use "a" now when declaring another variable: 我们认为声明其他变量时现在可以使用“ a”:

b = (myCode "1" "2" a) "Hello" "World" emptyString "")

This would call in "b" the function saved as "a" and within this the "emptyString". 这将在“ b”中调用保存为“ a”的函数,并在其中调用“ emptyString”。 As you may have guessed - it doesn't work! 您可能已经猜到了-它不起作用! And we are really at a loss on how to carry on from now. 从现在开始,我们真的对如何进行下去感到茫然。

When you reached this part, it means you took the time to understand our complicated explanation of our task - thanks a lot. 当您达到这一部分时,这意味着您花了一些时间来理解我们对任务的复杂解释-非常感谢。

Thanks for suggestions and help in advance! 感谢您的建议和帮助!

From the question linked by amalloy in the comments, it looks like you are trying to build a phonebook based on a continuation passing style like paradigm. 从合金中注释链接的问题来看,您似乎正在尝试基于像范式这样的连续传递样式来构建电话簿。

Basically, what is supposed to happen for your type 基本上,您的类型应该发生什么

myCode :: String -> String -> MyType -> MyType

is that you will generate a piece of data dat = myCode ab pb , which is of type MyType . 是您将生成一条数据类型为MyType的数据dat = myCode ab pb So, you can query dat with an s :: String and it will output another String . 因此,您可以使用s :: String查询dat ,它将输出另一个String In the operation of dat s , if you expand it to the definition, dat s的操作中,如果将其扩展为定义,

dat s = myCode a b pb s

you have access to three strings, a , b , and whatever pb s returns. 您可以访问三个字符串ab和任何pb s返回。 You will build up functionality recursively, either by doing something with a b and s , or pushing it down the road to pb , letting the continuation handle it. 您将递归构建的功能,无论是做什么用a bs ,或者推下山的路到pb ,让继续处理它。

Hope this helps without giving too much away. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM