If I want to add a space at the end of a character to return a list, how would I accomplish this with partial application if I am passing no arguments?
Also would the type be?
space :: Char -> [Char]
I'm having trouble adding a space at the end due to a 'parse error' by using the ++ and the : operators.
What I have so far is:
space :: Char -> [Char]
space = ++ ' '
Any help would be much appreciated! Thanks
What you want here is an operator section. For that, you'll need to surround the application with parentheses, i.e.
which is syntactic sugar for
(++)
won't work here because it expects a string as the first argument, compare:Doing what you want is so common in Haskell it's got its own syntax, but being Haskell, it's extraordinarily lightweight. For example, this works:
so you weren't far off a correct solution. (
[Char]
is the same asString
." "
is the string containing the character' '
.) Let's look at using a similar function first to get the hang of it. There's a function in a library calledequalFilePath :: FilePath -> FilePath -> Bool
, which is used to test whether two filenames or folder names represent the same thing. (This solves the problem that on unix,mydir
isn't the same asMyDir
, but on Windows it is.) Perhaps I want to check a list to see if it's got the file I want:but since functions gobble their first argument first, then return a new function to gobble the next, etc, I can write that shorter as
This works because
equalFilePath "MyBestFile.txt"
is itself a function that takes one argument: it's type isFilePath -> Bool
. This is partial application, and it's super-useful. Maybe I don't want to bother writing a seperateisMyBestFile
function, but want to check whether any of my list has it:or just the partially applied version again:
Notice how I need to put brackets round
equalFilePath "MyBestFile.txt"
, because if I wroteany equalFilePath "MyBestFile.txt"
, thenfilter
would try and use justequalFilePath
without the"MyBestFile.txt"
, because functions gobble their first argument first.any :: (a -> Bool) -> [a] -> Bool
Now some functions are infix operators - taking their arguments from before and after, like
==
or<
. In Haskell these are just regular functions, not hard-wired into the compiler (but have precedence and associativity rules specified). What if I was a unix user who never heard ofequalFilePath
and didn't care about the portability problem it solves, then I would probably want to doand it would work, just the same, because == is a regular function. When you do that with an operator function, it's called an operator section.
It can work at the front or the back:
and you can do it with any operator you like:
and a handy operator for lists is
:
.:
takes an element on the left and a list on the right, making a new list of the two after each other, so'Y':"es"
gives you"Yes"
. (Secretly,"Yes"
is actually just shorthand for'Y':'e':'s':[]
because:
is a constructor/elemental-combiner-of-values, but that's not relevant here.) Using:
we can defineand we can get rid of the
c
as usualwhich hopefully make more sense to you now.