Transformation [Integer] & # 8594; Integer
I have never programmed before and I recently (1 week ago) started learning! The first course is functional programming using Haskell.
I have a school assignment that I would like to improve by removing one or two steps, but there was one nasty mistake on my way.
Basically, I am creating a list and I am getting the result with a type [Integer]
, then how would I like to convert it to Integer
if possible? I set up my test function to accept types Integer -> Integer -> Bool
(takes two values, evaluates them and returns bool). The test function puts values into two functions and compares their results.
I could just change the expected type to [Integer]
(?), But that would remove the ability to manually enter values.
In my test cases, I selected multiple values and put them in lists. a = [0, 2, (-3)]
and b = [0, 2, 4]
. What I would like to do when I call the function is to enter a
and b
as values, rather than entering each test file every time. Is it possible? Example:
testFunction a b
instead of something like
testFunction Integer Integer.
Hope I make sense :-) Keep in mind, I'm just learning!
source to share
Without making it too difficult for you, what you seem to be looking for is the ability to pass two lists of integers to a function that takes integers to perform a rudimentary operation.
For this you can use zipWith
which has a type signature:
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
This type signature means that zipWith takes a function that takes two separate elements and two lists and returns a list based on the results of the passed function.
In total, you do:
zipWith myFunction [1,2,3,4] [5,6,7,8]
Now you want to create a function that first uses zipWith
for each of your two functions and then uses again zipWith
to compare the two resulting lists, to finally return booleans. If you want to be even more complex, you can use the function and
at the end to return one boolean if all boolean strings True
.
Now that this function is left as an exercise for the reader :)
Hope this helps.
source to share