
Hi Donald, I think you misunderstood what I was asking. There's not two cases. Maybe I'm not saying it sufficiently well but the function ALWAYS just returns a function that always returns the original argument to ALWAYS no matter what else you give the resulting function. when one is define as follows one = always 1 then
one 4 1 one "abc" 1 one (2,3) 1 one [0,4,8,2] 1 map one ["one","two","three"] [1,1,1]
The mapping example is just an alternative way of
illustrating the functionality. No matter what the
defined function is given it always gives back the
original value give to ALWAYS.
Michael
--- Donald Bruce Stewart
I'm trying to learn Haskell and translating some Lisp functions as exercises.
How would I write a Haskell function named ALWAYS
behaves like this:
one = always 1 bozo = always "clown"
map one [2,3,4,5,6] [1,1,1,1,1]
one 62 1
map bozo [2,3,4,5,6] ["clown","clown" ,"clown", "clown"," clown"]
bozo 62 "clown"
i.e. ALWAYS returns a function with a single
nowgate: that parameter
that is ignored, returning instead the value given to ALWAYS when the function was created.
This is what I've been trying:
always :: (a -> a) -> a -> a always x = (\y -> x)
one = always 1
So there are two cases you want to handle: * return a list, if the argument is a list * return a single value, if the argument is a single value. We can write thes functions separately:
alwayList :: a -> [b] -> [a] alwayList n xs = map (const n) xs
*Main> let one = always 1 *Main> one "foo" [1,1,1]
*Main> let bozo = always "clown" *Main> bozo "haskell"
["clown","clown","clown","clown","clown","clown","clown"]
Now handle the non-list case:
alwaysAtom :: a -> b -> a alwaysAtom a b = a
*Main> let one = alwaysAtom 1 *Main> one 'x' 1
Unifying these two under a common type class is left as an exercise ;)
I guess the type signature for such a class would be something like:
class Const a b a' | b -> a', a' -> a where
Something like that.
-- Don
__________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com