I saw this in The Little MLer, a book on SML
datatype 'a list = Empty | Cons of 'a * 'a list
fun subst_c (pred)
= fn (n,Empty) => Empty
| (n,Cons(e,t)) => if pred (e)
then Cons (n,subst_c (pred) (n,t))
else Cons (e,subst_c (pred) (n,t))
The data type is just a homemade list, and the function subst_c takes a predicate ('a -> Bool) and determines whether an incoming list's elements pass or fail. What is interesting is the fn ... => ... part which takes in more parameters, namely an 'a and an 'a list. Technically this fn... is an anonymous, nameless function, and it seems bizarre to me that it's nested inside the named function but still taking in parameters as if it were at the top level. Here's a previous version showing all three parameters at the top level
fun subst_c (pred) (n,Empty) = Empty
...
The purpose of the first function was to demonstrate currying. IOW, what the second unnamed function is doing behind the scenes can be broken down to two-stages (currying) of the first with named, then unnamed functions. So my question is, is there anything like this in Haskell where a function inside a function -- named or anonymous -- can take incoming parameters as if it were top level?
LB