I have got 2 exercise, but I do not understand how can I make them work. I tried to figure it out but I don't understand. Can I get some help?
headTail :: [a] -> (a, [a])
headTail (l: ls) = (headTail l, [ls])
doubleHead :: [a] -> [b] -> (a, b)
doubleHead (l:ls) =
I would use pattern matching in both cases.
(l:ls)
assigns the first element of the list to l
and the rest to ls
. You can return them as a tuple as instructed.
headTail :: [a] -> (a, [a])
headTail (l:ls) = (l, ls)
Here we only care about the first elements of both lists. So, we pattern match them into x
, and y
and don't capture the rest at all (indicated by _
). And return them as a tuple.
doubleHead :: [a] -> [b] -> (a, b)
doubleHead (x:_) (y:_) = (x, y)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.