Skip to content
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

Open
ichko opened this issue Nov 8, 2020 · 3 comments
Open

Exercises week 3 #6

ichko opened this issue Nov 8, 2020 · 3 comments

Comments

@ichko
Copy link
Owner

ichko commented Nov 8, 2020

Тук може да споделяте решенията си на задачите от седмица 3. Самите задачи са дефинирани тук, а тук може да намерите нашите решения.

Публикувайте решенията като коментари в това issue или като линкове към ваши хранилища/gist.
Заградете кода на решението си по следния начин за да се highlight-не правилно (ако поствате коментар с кода тук):

```hs
-- solution
```
@Ivaylo-Valentinov
Copy link

Ivaylo-Valentinov commented Nov 10, 2020

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))

@ichko
Copy link
Owner Author

ichko commented Nov 16, 2020

@Ivaylo-Valentinov

  • Отново няма нужда да влагаш ф-ии в where, както си направил в getNewAxiom, особено когато имаш повтарящи се имена на променливи - t в контекста на getCorrectRule е от параметрите на ф-ята а не на тази в която е вложена. Малко е объркващо.

  • Всичко друго изглежда точно.

Харесва ми как си имплементирал findRoots, и collatz повече и от официалните решения.
Добра работа! ⭐

@dimitroffangel
Copy link
Contributor

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
Projects
None yet
Development

No branches or pull requests

3 participants