Haskell “Apply”? [duplicate]

十年热恋 提交于 2019-12-04 16:45:25

问题


Possible Duplicate:
Why is such a function definition not allowed in haskell?

I'm a newcomer to the world of Haskell, migrating over from Lisp. I'm trying to adjust to Haskell's fundamentally different worldview, and one of the many things that I find new and exciting is the type system. Being a Lisper, I thought I would try to implement in Haskell a function which is very important in the Lisp world: apply. For those who don't know, apply takes a function and a list of arguments, and invokes the function on those arguments. In Scheme, (apply + '(1 2 3)) is the same as invoking (+ 1 2 3), and returns 6.

My Haskell code looks something like this:

apply x [] = x
apply f (x:xs) = apply (f x) xs

But Haskell complains:

ERROR line 2 - Type error in function binding
*** Term           : apply
*** Type           : (b -> a) -> [b] -> a
*** Does not match : a -> [b] -> a
*** Because        : unification would give infinite type

And I think that I understand why. Apply's type needs to be different depending on the length of the list it is given. Given a list of, say, 3 items, apply's type would need to be: (a -> a -> a -> b) -> [a] -> b, but given a list of 6 items, apply's type would need to be: (a -> a -> a -> a -> a -> a -> b) -> [a] -> b.

I tried this horrendous work-around:

data FnOrDat a b = Dat b | Fn (a -> FnOrDat a b)

apply :: (FnOrDat a b) -> [a] -> (FnOrDat a b)
apply x [] = x
apply (Fn f) (x:xs) = apply (f x) xs
apply (Dat _) _ = error "Cannot apply something which is not a function!"

add a = Fn (\b -> Dat (a + b))

main = putStrLn $ show $ x where Dat x = apply (Fn add) [5,1]

This works, but it hardly counts as an apply function, because I can't pass apply a normal function, I have to use one that has been written specifically to use my (awkward) FnOrDat abstraction. If I wanted to write a function that added four numbers, I would need to write

add4 a = Fn (\b -> Fn (\c -> Fn (\d -> Dat (a + b + c + d))))

Ew.

So - am I missing something, or is asking for a general-purpose apply basically like asking for a function that can manipulate an arbitrary-length tuple? Does apply even make sense in the statically-typed worldview of Haskell?


回答1:


apply isn't very useful in Haskell, since you can't give a type to the function. As you see in your FnOrDat, you're essentially embedding a Lisp language into Haskell as an EDSL to force something through.

asking for a general-purpose apply basically like asking for a function that can manipulate an arbitrary-length tuple?

Exactly. You can come up with type class instances for certain useful combinations of types, but there's just not really a need or use for a general variadic apply.


As a side note, you should consider upgrading to GHC and the Haskell Platform, instead of the obsolete Hugs system, since you're missing out on most of libraries, tools and language features developed in the last 10 years.




回答2:


Notwithstanding Don's explanation, foldl1 (+) would actually add all elements of a list. Hence, one could say that the fold family of functions comes quite close to the apply as the OP describes it.




回答3:


... a function which is very important in the Lisp world: apply. For those who don't know, apply takes a function and a list of arguments, and invokes the function on those arguments. In Scheme, (apply + '(1 2 3)) is the same as invoking (+ 1 2 3), and returns 6. ...

This is quite simple:

foldr  (+) 0 [1,2,3]
foldr1 (+)   [1,2,3]

results in 6.

To apply a function to each element of a list:

map f list

e.g.

map (2*) [1,2,3]

results in [2,4,6]

Is this what you are looking for?



来源:https://stackoverflow.com/questions/10767407/haskell-apply

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!