Reputation: 127
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.
Upvotes: 1
Views: 5488
Reputation: 476719
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"
Upvotes: 3