Haskell exercise, type detection and protection

first question:
Define a function that combines a list of lists together using a separator value.
the type definition should be like this:

intersperse :: a -> [[a]] -> [a]

The separator should appear between list items, but should not follow the last item.
Your function should behave as follows:


ghci> :load Intersperse
[1 of 1] Compiling Main             ( Intersperse.hs, interpreted )
Ok, modules loaded: Main.
ghci> intersperse ',' []
""
ghci> intersperse ',' ["foo"]
"foo"
ghci> intersperse ',' ["foo","bar","baz","quux"]
"foo,bar,baz,quux"

after a while I will be able to solve it:


intersperse myChar lists
    | lists == []          = ""  
    | otherwise            = attach myChar lists
        where   attach myChar (x:[]) = x 
                attach myChar (x:xs) = x ++ (myChar : []) ++ attach myChar xs 

but, as you can see, this is without a type definition.
if I put a type definition over a function, I get an error. why?

second question:
before I get to this decision, I want to add another guard to the list of guards. this quad should be after the first guard. I want to check if the list variable has only one list, so I just return the lists variable. but I can’t guard like that (again, the error comes to life :-)):


| lists == (x:[]) = lists

and this did not work:


| lists == (_:[]) = lists
why why? :-).

after that I tried to make another guard:


| length lists == 1    = lists

but it also caused an error.

(by the way, I don’t need these guards because I found that the first template after the where keyword is exactly what I want.
This is the template that I mean:
attach myChar (x: []) = x

, , , , . , , , , :-)

: -).

p.s. haskell.

+3
3
  • "" [Char], intersperse [a], a , .

  • , .

    | length lists == 1    = lists
    

    lists [[a]], [a]. , lists - ["foo"], "foo". ["foo"].

+3

, , ( Char).

lists == []          = []

, , Eq - equals. , , .

, , :

intersperse myChar lists = case lists of
    []      -> []
    lists   -> attach myChar lists
    where   attach myChar (x:[]) = x 
            attach myChar (x:xs) = x ++ (myChar : []) ++ attach myChar xs 

:

intersperse _ [] = []
intersperse x xs = attach x xs
    where   attach myChar (x:[]) = x 
            attach myChar (x:xs) = x ++ (myChar : []) ++ attach myChar xs

:

intersperse _ [] = []
intersperse _ (xs:[]) = xs
intersperse x (xs:xss) = (xs ++ x:intersperse x xss)

, , , . . , , , .

+2

.
? , , , case of.


intersperse :: a -> [[a]] -> [a]
intersperse myChar lists
    | lists == []          = []  
    | otherwise            = attach myChar lists
        where   attach myChar (x:[]) = x 
                attach myChar (x:xs) = x ++ (myChar : []) ++ attach myChar xs

( , ):


intersperse :: a -> [[a]] -> [a]
intersperse myChar lists = case lists of
    []      -> []
    lists   -> attach myChar lists
    where   attach myChar (x:[]) = x 
            attach myChar (x:xs) = x ++ (myChar : []) ++ attach myChar xs

( ), , , .

?

.

+1
source

Source: https://habr.com/ru/post/1728306/


All Articles