0

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!

4
  • 1
    What is "store"? What is "2x Strings"? Commented Jan 20, 2017 at 18:40
  • 2
    Is this an obfuscation of the Phonebook exercise? Commented Jan 20, 2017 at 18:42
  • 1
    It looks like the same assignment as the recent question stackoverflow.com/questions/41753366/… Commented Jan 20, 2017 at 18:50
  • a refers to a single string, not a function. It's not a valid argument to myCode in the binding to b. Even if it was, the result of myCode is a value of type MyType, which is applicable to one value, not 4. You appear to have a severe misunderstanding of how functions are defined and applied, and that is making it extremely difficult to decipher what you are trying to do. Commented Jan 20, 2017 at 18:54

1 Answer 1

1

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 a b 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.

Sign up to request clarification or add additional context in comments.

1 Comment

So easy to guess the task - must be a common one :) Thank you very much for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.