3

I have never programmed before and i have just recently (1 week ago) started learning! The first course is functional programming, using Haskell.

I have a school assignment that I'd like to improve by removing one or two steps, but there's one pesky bug in my way.

Basically, I create a list and i get the result with the type [Integer], whereas I'd like to convert this to Integer, if possible? I've set my test function to accept the types Integer -> Integer -> Bool (takes two values, computes them, and returns a bool). The test function puts the values into two functions and compares their results.

I could just change the expected type to [Integer] (?) but that would eliminate the option of manually putting in values.

For my test cases I've chosen a few values and put them into lists. a = [0, 2, (-3)] and b = [0, 2, 4]. What I'd like to do when I call the function is to enter a and b as the values, instead of typing in each testcase every time. Is this possible? Example:

testFunction a b

instead of something like

testFunction Integer Integer.

I hope I made sense :-) Keep in mind I am just learning!

8
  • It's not entirely clear what you want to do. It seems that you want to pull test values out of two lists to pass to a test function. Do you want to execute every combination of elements from a and b, or just pairs of values like testFunction 0 0, testFunction 2 2, testFunction (-3) 4? Commented Sep 8, 2014 at 16:12
  • 2
    What this sounds like is you want a function testFunction :: [Integer] -> [Integer] -> [Bool], which at each step takes the first elements of each list, runs your function testFunction and then appends it to a list of previously calculated versions. See if you can think of a way to phrase that in Haskell. Commented Sep 8, 2014 at 16:12
  • are you allowed to use libraries? QuickCheck would help with testing. Commented Sep 8, 2014 at 16:17
  • Following up on @C.Quilley's suggestion, how could you do this with recursion? I'll start you off: testAll :: [Integer] -> [Integer] -> [Bool]; testAll (x:xs) (y:ys) = <something with "testAll xs ys">; testAll _ _ = [], where the last case catches the cases where either or both of the input lists are empty. Commented Sep 8, 2014 at 16:22
  • 1
    Another hint: Try typing your desired type signature into hoogle and seeing what comes up. I bet within the top 10 suggestions returned you might find something that could help you. Commented Sep 8, 2014 at 16:25

1 Answer 1

1

Without making this too complicated for you, what you seem to be looking for is the ability to pass two lists of Integers to a function, which accepts Integers, to perform an element-wise operation.

For this, you can use zipWith, which has a type signature of:

zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]

This type signature means that zipWith takes a function that accepts two individual elements, and two lists and returns a list built upon the results of the function that you passed.

In sum, you would be executing:

zipWith myFunction [1,2,3,4] [5,6,7,8]

Now, you want to create a function that first uses zipWith on each of your two functions, and then uses zipWith yet again to compare the two resulting lists, to finally return the Booleans. If you wanted to be even more sophisticated, you could use the and function at the end, to return a single Boolean, if all of the Booleans are True.

Now, building that function is left as an exercise to the reader :)

I hope this helps.

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.