Can you write a haskell function that solves the two sum problem, where the inputs are a…
- User
Can you write a haskell function that solves the two sum problem, where the inputs are a vector of numbers and a target number. The function should return the two numbers in the array that some to the target number or return -1 if an answer is not found in the array
Assistant
Here is a Haskell function that solves the two sum problem:
twoSum :: [Int] -> Int -> (Int, Int)
twoSum numbers target = findPair numbers target 0 where findPair [] _ _ = (-1, -1) findPair (x:xs) t i | t - x < 0 = findPair xs t (i+1) | t - x == 0 = (x, head (filter (/=x) xs)) | otherwise = findPair xs t (i+1)It takes a list of numbers and a target number as input. It recursively searches through the list, trying to find two numbers that sum to the target.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.