-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Exercises week 3 #6
Labels
Comments
lSystem :: [Char] -> [(Char, [Char])] -> [[Char]]
lSystem axiom rules = axiom : (lSystem (getNewAxiom axiom "") rules)
where
getNewAxiom [] res = res
getNewAxiom (h : t) res = getNewAxiom t (res ++ (getCorrectRule h rules))
where
getCorrectRule elem [] = elem : ""
getCorrectRule elem ((key, str) : t)
| elem == key = str
| otherwise = getCorrectRule elem t
derive :: Fractional a => a -> (a -> a) -> a -> a
derive eps f x = (f (x + eps) - f x) / eps
df :: (Double -> Double) -> Double -> Double
df = derive 1e-10
findRoots f iterations x0
| iterations == 0 = x0
| otherwise = findRoots f (iterations - 1) (x0 - ((f x0) / (df f $ x0)))
quicksort [] = []
quicksort (x:xs) =
let smallerSorted = quicksort [a | a <- xs, a <= x]
biggerSorted = quicksort [a | a <- xs, a > x]
in smallerSorted ++ [x] ++ biggerSorted
collatz n
| n == 1 = [1]
| even n = n : (collatz (div n 2))
| otherwise = n : (collatz (n * 3 + 1)) |
Харесва ми как си имплементирал |
replaceChar charToReplace (currentRule : restOfRules)
| charToReplace == fst currentRule = snd currentRule
| otherwise = replaceChar charToReplace restOfRules
replaceString [] _ result = result
replaceString (currentChar : restOfString) rules result =
replaceString restOfString rules (result ++ replaceChar currentChar rules)
axiom = "A"
rules = [('A', "AB"), ('B', "A")]
lSystem _ _ result 0 = result
lSystem axiom rules result n =
let newAxiom = replaceString axiom rules []
in lSystem newAxiom rules (result ++ [newAxiom]) $ n - 1
-- not so fast way...
fooArray =
[
last $ lSystem axiom rules [axiom] x | x <- [0..]
]
derivative eps f x = (f (x + eps) - f x) / eps
epsilon = 1e-10
df = derivative epsilon
findRoots f 0 x0 = f x0
findRoots f iterations x0
| f x0 < epsilon = x0
| otherwise = findRoots f (iterations - 1) (x0 - f x0 / df f x0)
collatz n =
reverse $ collatzHelper n [n]
where
collatzHelper n result
| n == 1 = result
| even n =
let newN = (n `div` 2)
in collatzHelper newN $ newN : result
| otherwise =
let newN = 3*n + 1
in collatzHelper newN $ newN : result
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Тук може да споделяте решенията си на задачите от седмица 3. Самите задачи са дефинирани тук, а тук може да намерите нашите решения.
Публикувайте решенията като коментари в това
issue
или като линкове към ваши хранилища/gist.Заградете кода на решението си по следния начин за да се highlight-не правилно (ако поствате коментар с кода тук):
The text was updated successfully, but these errors were encountered: