[Haskell-beginners] A simple function V2
Lyndon Maydwell
maydwell at gmail.com
Fri Aug 31 14:47:00 CEST 2012
You can also take advantage of higher order functions to simplify the
code. Something like:
doubleton :: a -> a -> [a]
doubleton x y = [x,y]
pre_intercalate :: [t] -> [t] -> [[t]]
pre_intercalate xs ys = zipWith doubleton xs ys
...
Then you're just one function-call (you can find it on Hoogle when you
determine what the signature needs to be) away from a solution. This
has the advantage of doing away with base cases and recursion. You
should definitely try out this style of whole-structure manipulation
as the Haskell standard libraries make it very convenient.
On Fri, Aug 31, 2012 at 8:16 PM, Gary Klindt <gary.klindt at googlemail.com> wrote:
> my mistake, that's not right.
>
> But you should try to use use something like this:
>
>
> intercalate :: (Eq t) => [t] -> [t] -> [t]
> intercalate _ [] = []
> intercalate [] _ = []
> intercalate (x:xs) (y:ys) = x : y : intercalate xs ys
>
> with the last line, you imply that your lists have at least one element
> (through pattern matching). By the recursive call, you will enter this
> 'non-exhaustive' case.
>
> Also, instead of writing
>
>
> *intercalate (x:xs) (y:ys)*
>>> * | xt == [] = []*
>>> * | yt == [] = []*
>>> * | otherwise = x : y : intercalate xs ys*
> * where xt=(x:xs)*
>>> * yt=(y:ys)*
>
> for defining xt and yt, you could write:
>
> intercalate xt@(x:xs) yt@(y:ys)
>
>
> greets
>
>
>
>
> On 08/31/2012 02:06 PM, Gary Klindt wrote:
>>
>> Hi,
>>
>> you don't proof for empty lists, only for empty tails: (x:[])
>>
>>
>> On 08/31/2012 02:01 PM, Ezequiel Hernan Di Giorgi wrote:
>>>
>>> First i want to thank all the persons who responded me yesterday to help
>>> me. Thanks! I am so happy with with your friendliness.
>>> So i have other beginners question:
>>>
>>> Now i want a improved version of my* intercalate*. Now i want to call a
>>> function with two [t][t] and obtain another one which have only
>>> even elements, even because:
>>>
>>> - [1,2,3,3,4][6] and the ouput [1,6]
>>> - [1,2,3,4][5,6,7] output [1,5,2,6,3,7]
>>>
>>> I tried it:
>>>
>>> *intercalate :: (Eq t) => [t] -> [t] -> [t]*
>>> *intercalate (x:xs) (y:ys)*
>>> * | xt == [] = []*
>>> * | yt == [] = []*
>>> * | otherwise = x : y : intercalate xs ys*
>>> * where xt=(x:xs)*
>>> * yt=(y:ys)*
>>>
>>> but i get nice error
>>>
>>> **Main> intercalate [1][6]*
>>> *[1,6*** Exception: baby.hs:(2,1)-(5,51): Non-exhaustive patterns in
>>> function intercalate*
>>> *
>>> *
>>> **Main> *
>>>
>>> (yes...the file's name is baby.hs)
>>>
>>> Thanks in advance! (: (: (:
>>> (: (:
>>>
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list