Sou bastante novo em Haskell e estou tentando criar uma função que gere um array de números finitos levando em consideração alguns limites iniciais e finais. Se eu usar a seguinte função ...
finiteRandoms :: (Eq n, Num n, RandomGen g, Random a, Num a) => n -> g -> ([a], g)
finiteRandoms 0 g = ([], g)
finiteRandoms n g =
let (value, newGen) = randomR (20,100) g
(restOfList, finalGen) = finiteRandoms (n-1) newGen
in (value:restOfList, finalGen)
e executo o seguinte comando...
finiteRandoms 3 (mkStdGen 3)
Eu obtenho um array de Int com o novo gerador
([61,33,82],StdGen {unStdGen = SMGen 9186733390819170434 2092789425003139053})
O que estou tentando fazer é adicionar dois novos parâmetros à função que marca o ponto inicial e final. Na função que compartilhei, ela representa os números 20 e 100. Algo assim
finiteRandoms :: (Eq n, Num n, RandomGen g, Random a, Num a) => n -> n -> n-> g -> ([a], g)
finiteRandoms 0 start end g = ([], g)
finiteRandoms n start end g =
let (value, newGen) = randomR (start,end) g
(restOfList, finalGen) = finiteRandoms (n-1) newGen
in (value:restOfList, finalGen)
Mas quando carrego através do ghci recebo o seguinte erro
randomSample.hs:19:10: error: [GHC-83865]
• Couldn't match expected type: n -> g0 -> ([a0], g0)
with actual type: (a1, b)
• In the pattern: (restOfList, finalGen)
In a pattern binding:
(restOfList, finalGen) = finiteRandoms (n - 1) newGen
In the expression:
let
(value, newGen) = randomR (start, end) g
(restOfList, finalGen) = finiteRandoms (n - 1) newGen
in (value : restOfList, finalGen)
• Relevant bindings include
value :: n (bound at randomSample.hs:18:11)
end :: n (bound at randomSample.hs:17:24)
start :: n (bound at randomSample.hs:17:18)
n :: n (bound at randomSample.hs:17:16)
finiteRandoms :: n -> n -> n -> g -> ([a], g)
(bound at randomSample.hs:16:2)
|
19 | (restOfList, finalGen) = finiteRandoms (n-1) newGen
| ^^^^^^^^^^^^^^^^^^^^^^
randomSample.hs:20:11: error: [GHC-25897]
• Couldn't match expected type ‘a’ with actual type ‘n’
‘n’ is a rigid type variable bound by
the type signature for:
finiteRandoms :: forall n g a.
(Eq n, Num n, RandomGen g, Random a, Num a) =>
n -> n -> n -> g -> ([a], g)
at randomSample.hs:15:2-92
‘a’ is a rigid type variable bound by
the type signature for:
finiteRandoms :: forall n g a.
(Eq n, Num n, RandomGen g, Random a, Num a) =>
n -> n -> n -> g -> ([a], g)
at randomSample.hs:15:2-92
• In the first argument of ‘(:)’, namely ‘value’
In the expression: value : restOfList
In the expression: (value : restOfList, finalGen)
• Relevant bindings include
value :: n (bound at randomSample.hs:18:11)
end :: n (bound at randomSample.hs:17:24)
start :: n (bound at randomSample.hs:17:18)
n :: n (bound at randomSample.hs:17:16)
finiteRandoms :: n -> n -> n -> g -> ([a], g)
(bound at randomSample.hs:16:2)
|
20 | in (value:restOfList, finalGen)
| ^^^^^
Failed, no modules loaded.
Como posso parametrizar esses dois limites (início/fim)? Obrigado.
Você esqueceu de adicionar o
start
eend
na chamada recursivastart
eend
também precisa ser do tipog
Mas provavelmente é melhor trabalhar com uma função auxiliar aqui:
Geramos então, por exemplo, cinco números entre vinte e cem com: