简体   繁体   中英

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)

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

and we already implemented a function, which can be used as MyType one:

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

The task is to be able to store several 2x Strings. 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". This works for one String, because when we type the following in the console:

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

we get "World" on input "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:

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

This would call in "b" the function saved as "a" and within this the "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 . So, you can query dat with an s :: String and it will output another String . In the operation of dat s , if you expand it to the definition,

dat s = myCode a b pb s

you have access to three strings, a , b , and whatever pb s returns. 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.

Hope this helps without giving too much away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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