haskell - I want to combine my two functions into one? -
haskell - I want to combine my two functions into one? -
i have 2 functions:
firstfunc :: (randomgen g) => g -> int -> float -> [[int]] firstfunc rnd n p = makgrid $ map (\t -> if t <= p 1 else 0) $ take (n*n) (randoms rnd) makgrid rnd = unfoldr nextrow (rnd, n) nextrow (_, 0) = nil nextrow (es, i) = allow (rnd, rest) = splitat n es in (rnd, (rest, i-1)) sndfunc firstfunc = head $ lst <- firstfunc homecoming $ map (\x -> if (x == 0) 2 else x) lst allow newfunc = ((firstfunc .) .) . sndfunc main = gen <- getstdgen allow g = (firstfunc gen 5 0.3) print g allow h = sndfunc g print h print $ newfunc gen 5 0.3
the firstfunc takes 2 values 5
, 0.3
, returns list of lists like: [[0,0,0,1,0],[1,0,0,1,0],[0,0,1,1,0],[0,0,0,0,0],[1,0,0,0,1]]
the sndfunc takes head of above list of lists, [1,0,0,1,0]
above example, , replaces zeroes 2s this: [1,2,2,1,2]
.
is there way combine 2 functions such firstfunc returns like: [[1,2,2,1,2],[0,0,1,1,0],[0,0,0,0,0],[1,0,0,0,1]]
?
error:
getting parse error parse error (possibly wrong indentation or mismatched brackets) on line: let newfunc = ((firstfunc .) .) . sndfunc
2nd edit:
prac.hs:15:32: couldn't match type ‘[b]’ ‘a -> a1 -> b0’ expected type: [[b]] -> -> a1 -> b0 actual type: [[b]] -> [b] relevant bindings include newfunc :: [[b]] -> -> a1 -> int -> float -> [[int]] (bound @ prac.hs:15:1) in sec argument of ‘(.)’, namely ‘sndfunc’ in expression: (((firstfunc .) .) . sndfunc) failed, modules loaded: none.
you can either utilize
print $ sndfunc $ firstfunc gen 5 0.3
or can define new function
let newfunc = ((sndfunc .) .) . firstfunc
and utilize that.
print $ newfunc gen 5 0.3
(the (.) operators needed because firstfunc takes 3 inputs).
haskell
Comments
Post a Comment