1

I'm new to Haskell so I have lots of questions so I'd like your help.

Make a function "func" that receives a function " f " (String -> String) type and a String "s" that returns the reverse of "s" concat with " f " application in "s"

I managed to make the " f " function like this:

f:: String -> String
f x -> reverse(x) ++ x

console: f "HASKELL" -> "LLEKSAHHASKELL"

But when it comes to the "func" function I don't know what to do or type in the console. This code below worked in GHC, but I don't know why, it doesn't accepts entries.

func:: (String -> String) -> String -> String
func f s = (reverse(s) ++ f "")

console: func "HASKELL" "ROCKS"

<interactive>:49:6: error:
    • Couldn't match expected type ‘String -> String’
                  with actual type ‘[Char]’
    • In the first argument of ‘func’, namely ‘"HASKELL"’
      In the expression: func "HASKELL" "ROCKS"
      In an equation for ‘it’: it = func "HASKELL" "ROCKS"

If there's a better way to do it, please can you tell me ?

Thanks for your help.

2
  • 2
    Why f ""? The text seems to ask for f s instead. Anyway, you can't call func with two strings, the first argument must be a function. Commented Sep 22, 2019 at 16:32
  • Thanks, I knew that something was off. Commented Sep 22, 2019 at 16:45

1 Answer 1

3

Your function is almost correct, except that you did not use s as parameter for the function application with s:

func:: (String -> String) -> String -> String
func f s = reverse s ++ f s

You furthermore use the function the wrong way, since the first parameter is not a string, but a function that maps a string on a string. We can for example use id :: a -> a to return s, or reverse :: [a] -> [a] to reverse the string:

Prelude> func id "haskell"
"lleksahhaskell"
Prelude> func reverse "haskell"
"lleksahlleksah"
Sign up to request clarification or add additional context in comments.

Comments

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.