KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
So I'm trying to teach myself Haskell. I am currently on the 11th chapter of Learn You a Haskell for Great Good and am doing the 99 Haskell Problems as well as the Project Euler Problems . Things are going alright, but I find myself constantly doing something whenever I need to keep track of "variables". I just create another function that accepts those "variables" as parameters and recursively feed it different values depending on the situation. To illustrate with an example, here's my solution to Problem 7 of Project Euler, Find the 10001st prime : answer :: Integer answer = nthPrime 10001 nthPrime :: Integer -> Integer nthPrime n | n < 1 = -1 | otherwise = nthPrime' n 1 2 [] nthPrime' :: Integer -> Integer -> Integer -> [Integer] -> Integer nthPrime' n currentIndex possiblePrime previousPrimes | isFactorOfAnyInThisList possiblePrime previousPrimes = nthPrime' n currentIndex theNextPossiblePrime previousPrimes | otherwise = if currentIndex == n then possiblePrime else nthPrime' n currentIndexPlusOne theNextPossiblePrime previousPrimesPlusCurrentPrime where currentIndexPlusOne = currentIndex + 1 theNextPossiblePrime = nextPossiblePrime possiblePrime previousPrimesPlusCurrentPrime = possiblePrime : previousPrimes I think you get the idea. Let's also just ignore the fact that this solution can be made to be more efficient, I'm aware of this. So my question is kind of a two-part question. First, am I going about Haskell all wrong? Am I stuck in the imperative programming mindset and not embracing Haskell as I should? And if so, as I feel I am
Tags (comma-separated)
Save Edits
Cancel