Reputation: 37
So i have a function that takes in three variables. Variable y is called by another function that will turn it into a list.
I want to apply map to this list of variables. So that i can recurse through the function using all the elements of the list one at a time.
since this function takes in three variables, and one is a list that i want to recall the function and apply each element of the list until the list is empty. I am not sure how to do this and return the remaining two variables back each time.
search :: State -> Int -> Int -> Int
search state n1 n2 = case n1 of
0 -> 1 * n2
num -> average( map (search s) n1-1 n2)
where s = turnStateToList s
i havent included the turn state to list but i dont think it is required to answer the question. basically it will produce a list of new states. think of it like a tree.
the errors i get are
* map is applied to too many functions
* y has been given arguments however yy takes in 0 arguments
i understand how it is getting these errors however im not sure what else to do to fix it. Ive tried changing up the brackets and that just gives a different argument error.
Upvotes: 0
Views: 385
Reputation: 10645
As was mentioned in the comments to your question, in Haskell you can define variables recursively, so s = turnStateToList s
doesn't mean that you make a new s
that gets the value of turnStateToList
of the old s. Instead, it defines s
recursively in terms of itself, which leads to the infinite expression s = turnStateToList (turnStateToList (turnStateToList ...
which is probably not what you want.
To map
over a list while keeping some arguments fixed you can introduce an anonymous function, in this case I think you want \s'' -> search s'' (n1 - 1) n2
(the parentheses around n1 - 1
are necessary!).
Oh, and I think you mixed up state
and s
by accident.
So, with minimal changes to your style, I think you want this code:
search :: State -> Int -> Int -> Int
search s n1 n2 = case n1 of
0 -> 1 * n2
num -> average( map (\s'' -> search s'' (n1 - 1) n2) s')
where s' = turnStateToList s
I think it is more idiomatic to avoid the case statement and to avoid binding turnStateToList s
to a variable, then you get this code:
search :: State -> Int -> Int -> Int
search _ 0 n2 = n2
search s n1 n2 = average $ map (\s' -> search s' (n1 - 1) n2) (turnStateToList s)
Upvotes: 1