gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
countPrimes in Haskell with user input
- User
countPrimes in Haskell with user input
- Assistant
To implement the
countPrimesfunctionality in Haskell with user input, you can use the following code:isPrime :: Int -> Bool isPrime n | n <= 1 = False | otherwise = not $ any (\x -> n `mod` x == 0) [2..(n-1)] countPrimes :: [Int] -> Int countPrimes nums = length $ filter isPrime nums main :: IO () main = do putStrLn "Enter a list of numbers (separated by spaces):" input <- getLine let numbers = map read $ words input :: [Int] let primeCount = countPrimes numbers putStrLn $ "The count of prime numbers in the list is: " ++ show primeCountIn this code, the function
isPrimechecks if a number is prime or not using trial division. ThecountPrimesfunction takes a list of numbers and returns the count of prime numbers in that list. Finally, in themainfunction, it prompts the user to input a list of numbers, processes the input, and calls thecountPrimesfunction to get the count of prime numbers in the list, which is then printed to the console.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.