Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Reversing a list (Jona Ekenberg)
   2. Re:  Reversing a list (Frederic Cogny)
   3. Re:  Reversing a list (Francesco Ariis)
   4. Re:  Reversing a list (Jona Ekenberg)


----------------------------------------------------------------------

Message: 1
Date: Tue, 4 Jul 2017 12:06:39 +0200
From: Jona Ekenberg <saik...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] Reversing a list
Message-ID:
        <calveeuf_7jon6fu9acdkzw0ate-kvsrmjwtus+sa5ytuhc-...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hello,

I'm currently going through the exercises in chapter 3 of Real World
Haskell. One of the exercises challenges me to create a function which
takes a list and makes a palindrome of it.

I tried to solve it this way:
palindrome :: [t] -> [t]
palindrome xs = xs ++ (reverse xs)
  where
    reverse []     = []
    reverse (x:xs) = (reverse xs) ++ x

But when I try to compile this I get this error:
Kapitel3.hs:14:32-33: error: …
    • Couldn't match type ‘t’ with ‘[t]’
      ‘t’ is a rigid type variable bound by
        the type signature for:
          palindrome :: forall t. [t] -> [t]
        at /Users/jona/programmering/haskell/boken/Kapitel3.hs:13:15
      Expected type: [[t]]
        Actual type: [t]
    • In the first argument of ‘reverse’, namely ‘xs’
      In the second argument of ‘(++)’, namely ‘(reverse xs)’
      In the expression: xs ++ (reverse xs)
    • Relevant bindings include
        xs :: [t]
          (bound at
/Users/jona/programmering/haskell/boken/Kapitel3.hs:14:12)
        palindrome :: [t] -> [t]
          (bound at
/Users/jona/programmering/haskell/boken/Kapitel3.hs:14:1)
Compilation failed.

It looks like I have used a function that want a list of lists, but I don't
understand where.
Also, is there some way to declare the type of my reverse-function in this
case?

Kind regards,
Jona Ekenberg
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170704/7252097f/attachment-0001.html>

------------------------------

Message: 2
Date: Tue, 04 Jul 2017 10:17:43 +0000
From: Frederic Cogny <frederic.co...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Reversing a list
Message-ID:
        <cagsugsse_fep2ofqmne36s9beubmhjcceo9swywbnw7bbk5...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Looks like you're missing the square brackets around your x in the
definition of reverse (the ++ takes two lists, hence the error message)

Try this

palindrome :: [t] -> [t]
palindrome xs = xs ++ (reverse xs)
  where
    reverse []     = []
    reverse (x:xs) = (reverse xs) ++ *[*x*]*


to declare the type you can define it outside or annotate it within you're
code

palindrome :: [t] -> [t]
palindrome xs = xs ++ (reverse xs)
  where
   * reverse :: [t] -> [t]*
    reverse []     = []
    reverse (x:xs) = (reverse xs) ++ *[*x*]*

On Tue, Jul 4, 2017 at 12:07 PM Jona Ekenberg <saik...@gmail.com> wrote:

> Hello,
>
> I'm currently going through the exercises in chapter 3 of Real World
> Haskell. One of the exercises challenges me to create a function which
> takes a list and makes a palindrome of it.
>
> I tried to solve it this way:
> palindrome :: [t] -> [t]
> palindrome xs = xs ++ (reverse xs)
>   where
>     reverse []     = []
>     reverse (x:xs) = (reverse xs) ++ x
>
> But when I try to compile this I get this error:
> Kapitel3.hs:14:32-33: error: …
>     • Couldn't match type ‘t’ with ‘[t]’
>       ‘t’ is a rigid type variable bound by
>         the type signature for:
>           palindrome :: forall t. [t] -> [t]
>         at /Users/jona/programmering/haskell/boken/Kapitel3.hs:13:15
>       Expected type: [[t]]
>         Actual type: [t]
>     • In the first argument of ‘reverse’, namely ‘xs’
>       In the second argument of ‘(++)’, namely ‘(reverse xs)’
>       In the expression: xs ++ (reverse xs)
>     • Relevant bindings include
>         xs :: [t]
>           (bound at
> /Users/jona/programmering/haskell/boken/Kapitel3.hs:14:12)
>         palindrome :: [t] -> [t]
>           (bound at
> /Users/jona/programmering/haskell/boken/Kapitel3.hs:14:1)
> Compilation failed.
>
> It looks like I have used a function that want a list of lists, but I
> don't understand where.
> Also, is there some way to declare the type of my reverse-function in this
> case?
>
> Kind regards,
> Jona Ekenberg
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-- 
Frederic Cogny
+33 7 83 12 61 69
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170704/57d2bc88/attachment-0001.html>

------------------------------

Message: 3
Date: Tue, 4 Jul 2017 12:20:36 +0200
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Reversing a list
Message-ID: <20170704102036.pbcapqdfwjmpj...@x60s.casa>
Content-Type: text/plain; charset=utf-8

On Tue, Jul 04, 2017 at 12:06:39PM +0200, Jona Ekenberg wrote:
> Hello,
> 
> I'm currently going through the exercises in chapter 3 of Real World
> Haskell. One of the exercises challenges me to create a function which
> takes a list and makes a palindrome of it.
> 
> I tried to solve it this way:
> palindrome :: [t] -> [t]
> palindrome xs = xs ++ (reverse xs)
>   where
>     reverse []     = []
>     reverse (x:xs) = (reverse xs) ++ x

Hello Jona,

    let's analyse the error.
It points to this bit:

    palindrome xs = xs ++ (reverse xs)

And it says: I expected [[t]], but you gave me [t]. Whenever I encounter
such an error I try to write explicit type signatures so to make diagnosing
easier. In your example

    palindrome :: [t] -> [t]
    palindrome xs = xs ++ (reverse xs)
      where
          reverse :: [t] -> [t] -- explicit type signature
          reverse []     = []
          reverse (x:xs) = (reverse xs) ++ x

If we :reload ghci complains again, the offending bit being

    reverse (x:xs) = (reverse xs) ++ x
                                     ^
Expected type is [t1] but we passed t. Not it is clear! The type of `++` is:

    λ> :t (++)
    (++) :: [a] -> [a] -> [a]

and `x` is a single element. When we replace `x` with `[x]` everything works.

Does that help?
-F

P.S.: Real World Haskell is an excellent book but sometimes can be a tad
difficult to follow. If you want to integrate with another source, CIS194 [1]
is an excellent choice: free, thorough, full of home-works and interactive.

[1] http://www.cis.upenn.edu/~cis194/fall16/



------------------------------

Message: 4
Date: Tue, 4 Jul 2017 12:26:53 +0200
From: Jona Ekenberg <saik...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Reversing a list
Message-ID:
        <CALvEEUfZzW4e==zvewpbyt_rf23vxz9+hsj93dulv6vnizw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Thank you both for your answers, I somehow thought ++ acted as both append
and concat, since I've mostly used it on strings where I haven't had to
think about it.

And thank you for the tips regarding adding explicit types and the reading
material. So far I feel that I'm able to follow along quite well, but it's
nice to have a second source!

Grateful regards,
Jona

Den 4 juli 2017 12:21 em skrev "Francesco Ariis" <fa...@ariis.it>:

On Tue, Jul 04, 2017 at 12:06:39PM +0200, Jona Ekenberg wrote:
> Hello,
>
> I'm currently going through the exercises in chapter 3 of Real World
> Haskell. One of the exercises challenges me to create a function which
> takes a list and makes a palindrome of it.
>
> I tried to solve it this way:
> palindrome :: [t] -> [t]
> palindrome xs = xs ++ (reverse xs)
>   where
>     reverse []     = []
>     reverse (x:xs) = (reverse xs) ++ x

Hello Jona,

    let's analyse the error.
It points to this bit:

    palindrome xs = xs ++ (reverse xs)

And it says: I expected [[t]], but you gave me [t]. Whenever I encounter
such an error I try to write explicit type signatures so to make diagnosing
easier. In your example

    palindrome :: [t] -> [t]
    palindrome xs = xs ++ (reverse xs)
      where
          reverse :: [t] -> [t] -- explicit type signature
          reverse []     = []
          reverse (x:xs) = (reverse xs) ++ x

If we :reload ghci complains again, the offending bit being

    reverse (x:xs) = (reverse xs) ++ x
                                     ^
Expected type is [t1] but we passed t. Not it is clear! The type of `++` is:

    λ> :t (++)
    (++) :: [a] -> [a] -> [a]

and `x` is a single element. When we replace `x` with `[x]` everything
works.

Does that help?
-F

P.S.: Real World Haskell is an excellent book but sometimes can be a tad
difficult to follow. If you want to integrate with another source, CIS194
[1]
is an excellent choice: free, thorough, full of home-works and interactive.

[1] http://www.cis.upenn.edu/~cis194/fall16/

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20170704/bcb9d475/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 109, Issue 3
*****************************************

Reply via email to