Mapping 3 argument function to a list in Haskell - haskell

The map function works fine on really easy functions that take no arguments like *7 to every element in a list of Ints.
But say I made a custom function that takes a Char, String and Int and then returns a Char and I wanted to apply this function to a list of Chars i.e. a String to get a String back after applying the other function to each Char? When I try this all I get is error messages?

Put your Char argument to the last position:
foo :: String -> Int -> Char -> Char
foo str num c = c
bar = map (foo str num)
But we really need some more information from your side to help you better. Can you add the code you tried to write to your question?

Related

How can I transform Char to Int in Haskell using "ord" function?

I have been trying to figure out how can I covert a Char to an Int in Haskell using the "ord" function from the Data.Char library. Here is where I got to now:
charToNum :: Char -> Int
charToNum x = (ord(x))
It is a pretty simple program, but I'm not a 100% sure how to work with an "ord" function
When I run compile the program, everything compiles, but when I try to enter a character I get this error:
> charToNum e
<interactive>:85:11: error: Variable not in scope: e :: Char
e is not a character. I mean, it is of course a character of the string you typed into the prompt, like c and N are characters of that string, but as far as the parser is concerned all of these represent just parts of variable names. And, well, as GHCi then tells you that there is no variable named e.
If you want to pass the character ‘e’ as an argument then do just that!
> charToNum 'e'
charToNum 'e'?
BTW, your definition should read
charToNum x = ord x
(Or simply
charToNum = ord
)

How to apply a Char function via map to a [Char]

I'm working on a very specific question however I've stumbled upon a logical error that I just cannot seem to solve.
Basically I have a function that that takes a char and returns it as a string with a space appended. From there I am required to map that function to a string and return a string of strings, each as a char with a space appended.
I have attempted so many different things I don't even recall what gives me the least errors anymore. After staring at this for two sad hours I could really use some input.
addSpace :: Char -> [Char]
addSpace t = [t,' ']
addSpaces :: [Char] -> [[Char]]
addSpaces y = map addSpace[????????]
The main issue I have is, no matter what I put into the 'map addSpace' function I get type errors.
I apologize if this question is way simpler than I think it is. I'm just having a rough time.
Thanks in advance! I have really appreciated the help I have received in the past from the Haskell community on stack overflow.
You should pass in y as the second argument to map.
addSpaces y = map addSpace y
Or, you could remove the argument y, and have the argument passed in implicitly (pointfree style).
addSpaces = map addSpace

Haskell list type conversion

Support I have a list of type [Char], and the values enclosed are values of a different type if I remove their quotations which describe them as characters. e.g. ['2','3','4'] represents a list of integers given we change their type.
I have a similar but more complicated requirement, I need to change a [Char] to [SomeType] where SomeType is some arbitrary type corresponding to the values without the character quotations.
Assuming you have some function foo :: Char -> SomeType, you just need to map this function over your list of Char.
bar :: [Char] -> [SomeType]
bar cs = map foo cs
I hope I get this correctly and there is a way (if the data-constructors are just one-letters too) - you use the auto-deriving for Read:
data X = A | B | Y
deriving (Show, Read)
parse :: String -> [X]
parse = map (read . return)
(the return will just wrap a single character back into a singleton-list making it a String)
example
λ> parse "BAY"
[B,A,Y]

Haskell: Type Error in Explicitly Typed Binding

I'm trying to create a function that will remove all occurrences of a given character in a string, but being the Haskell amateur that I am, I've run into issues that are making it hard to sleep.
Here's my code:
remove:: Char -> String -> Int
remove[] = []
remove (char : list) = char : remove (filter (\elm -> not (char == elm) ) list)
With the type definitions in, I get the following error code:
ERROR "a5.hs":17 - Type error in explicitly typed binding
*** Term : char : list
*** Type : [a]
*** Does not match : Char
Can anyone please help?
The type signature says that remove takes two parameters, a Char and a String, and gives back an Int.
The error message refers to the second equation, which tries to match the first parameter (a Char) to (char : list) (a list).
Perhaps you meant
remove char list = ...
?
Further errors:
The first equation makes the same mistake (trying to match an empty list with the Char parameter).
Perhaps this should be
remove _ [] = ...
(_ means to match anything.)
The type signature says that the result is an Int, but your equations give a String result.
There are more mistakes. But that will get you started.
Edit: In response to your first comment:
The type signature should be
remove :: Char -> String -> String
remove takes a Char and a String and gives back a String.
When you put remove :: Char -> String, that means remove takes a Char and gives back a String.
(\elm -> flip (==) elm)
is the same as (\elm -> (==) elm)
is the same as (\elm birch -> (==) elm birch)
is the same as (\elm birch -> elm == birch)
is also the same as (==).
This expression has the type a -> a -> Bool, but you are passing it as the first parameter to filter, which expects a function with type a -> Bool (i.e. it wants a function that takes one parameter, not a function that takes two).
(This is what the error message is telling you about.)

Char to string function

I have a very simple question: Given a function accepting a char and returning a string
test :: Char -> [String]
how can one convert the char into a string? I'm confused over the two types.
In Haskell String is an alias for [Char]:
type String = [Char]
If you just want a function that converts a single char to a string you could e.g. do
charToString :: Char -> String
charToString c = [c]
If you prefer pointfree style you could also write
charToString :: Char -> String
charToString = (:[])
A String is just a [Char]
But that's just a nice way of saying
'H':'E':'L':'L':'O':[]
So to make it a [String] we could do:
['H':'E':'L':'L':'O':[]]
Another way would be using
return . return
Since return for lists is defined as :[]
Note that you can convert any type implementing the Show type class to a string using show:
(Show a) => a -> String
Because Char implements this, the function is already written for you!

Resources