exercism / exercism/problem-specifications

Exercise idea: Golomb sequence

Offen
#906 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
new exercise idea
Vorherrschende Sprache
Ruby
Sterne
358
Forks
563
Ø Merge
18 Std. 41 Min.
Gemergte PRs (30 T.)
2

Beschreibung

### Golomb sequence
See:[ OEIS page of Golomb Sequence](https://oeis.org/A001462) for more information.

I like the idea of the Golomb sequence as an exercise. There are many possible implementations like the recursive definition:
```Haskell
g :: Int -> Int
g 1 = 1
g n = 1 + g (n - g (g (n-1)))
```
or using the self-descriptive characteristics of the sequence:
```Haskell
golomb :: Int -> Int
golomb n = sucGolomb !! (n-1)

sucGolomb :: [Int]
sucGolomb = 1 : 2 : 2 : f 3
where f x = (replicate (golomb x) x) ++ (f (x+1))
```

but non of them it's really close of being efficient. So this might be good as an example of how and when to use recursive functions.

```Haskell
import Prelude hiding (replicate, length, drop)
import Data.Sequence ((><), index, replicate, fromList, length, drop)

golomb :: Int -> Int
golomb n = fn (fromList [1, 2, 2]) 3 2
where fn gl x t = if n <= length gl
then gl `index` (pred n)
else let n_gl = (><) gl (replicate t x)
in fn n_gl (succ x) (n_gl `index` x)
```
And there are probably a lot of better (faster and cleaner) implementations out there. But this one at least implicates to try an alternative to lists.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.