<p>What????</p>
<p>First of all, you should just use <code>length</code> instead of <code>genericLegth</code>.</p>
<pre><code>λ &gt; let xs = [1..6] in sum xs / length (drop xs)

&lt;interactive&gt;:7:42: error:
    • Couldn't match expected type ‘Int’ with actual type ‘[a1]’
    • In the first argument of ‘drop’, namely ‘xs’
      In the first argument of ‘length’, namely ‘(drop xs)’
      In the second argument of ‘(/)’, namely ‘length (drop xs)’
</code></pre>
<p>The problem is that <code>drop</code> takes two input arguments, not 1 as you have given it in all three examples (the last of which is just wrong).</p>
<pre><code>λ &gt; :t drop
drop :: Int -&gt; [a] -&gt; [a]
</code></pre>
<p>Fixing that problem, you get another:</p>
<pre><code>λ &gt; let xs = [1..6] in sum xs / length (drop 2 xs)

&lt;interactive&gt;:9:20: error:
    • No instance for (Fractional Int) arising from a use of ‘/’
    • In the expression: sum xs / length (drop 2 xs)
      In the expression: let xs = [1 .. 6] in sum xs / length (drop 2 xs)
      In an equation for ‘it’:
          it = let xs = ... in sum xs / length (drop 2 xs)
</code></pre>
<p>Now, you have a problem with the <code>/</code> operator not being usable with <code>Int</code>.</p>
<p>The solution is:</p>
<pre><code>let xs = [1..6] in fromIntegral (sum xs) / fromIntegral (length $ drop 2 xs)
5.25
</code></pre>
<p>The reason this does not work:</p>
<pre><code>let xs = [1..6] in sum xs / length $ drop 2 xs
</code></pre>
<p>is because it gets desugared to:</p>
<pre><code>let xs = [1..6] in (sum xs / length) (drop 2 xs)
</code></pre>
<p>which does not make sense.</p>

CC BY-SA 4.0
2022-03-08T05:03:05.493Z
Thank you very much, you have explained it very clearly to me, have a good day, hope to get more help from you in the future.
tienty muctieu
CC BY-SA 4.0
2022-03-08T06:09:12.133Z
one more problem, hope you can explain it to me, for example drop has its type defined as follows: drop :: Int -> [a] -> [a] if I want to redefine my own drop function: drop' :: Num -> [a] -> [a] ----> it doesn't work even though Num includes Int in it. sorry for the stupid question, i'm a newbie and learning it from scratch
tienty muctieu
CC BY-SA 4.0
2022-03-09T06:39:07.177Z
`Num` is not a type, its a type class. I suspect what you want is:
```
drop' :: Num b => b -> [a] -> [a]
```
The part to the left of the `=>` is a type constraint which says that `b` has to be a type that has a `Num` type class instance.
Erik de Castro Lopo
CC BY-SA 4.0
2022-03-12T12:48:11.350Z
thank you very much, you clarified my problem, i got it. Have a nice day
tienty muctieu
CC BY-SA 4.0
2022-03-07T20:09:17.790Z
2022-03-07T20:09:17.790Z
Erik de Castro Lopo
<p>i'm newbie and trying to learn haskell, but i'm having a problem in the Data.List chapter of learnyouahaskell as follows:</p>
<pre><code>let xs = [1..6] in sum xs / genericLength xs    -- &gt; it works
</code></pre>
<p>Now I try to add the drop function to calculate the average for some elements t want:</p>
<pre><code>let xs = [1..6] in sum xs / genericLength (drop xs) -- &gt; it works
let xs = [1..6] in sum xs / genericLength $ drop xs -- &gt; it doesn't work
let xs = [1..6] in sum xs / genericLength . drop xs -- &gt; also not working
</code></pre>
<p>i want to know what is the problem?</p>

<haskell><learning>
haskell quite hard
# haskell quite hard

(Asked by: username_0 on 2022-03-07)

i'm newbie and trying to learn haskell, but i'm having a problem in the Data.List chapter of learnyouahaskell as follows:
let xs = [1..6] in sum xs / genericLength xs    -- > it works

Now I try to add the drop function to calculate the average for some elements t want:
let xs = [1..6] in sum xs / genericLength (drop xs) -- > it works
let xs = [1..6] in sum xs / genericLength $ drop xs -- > it doesn't work
let xs = [1..6] in sum xs / genericLength . drop xs -- > also not working

i want to know what is the problem?

**Tags:** haskell, learning

---

## 1 answers

### Answer by username_1 on 2022-03-07. Score: 1 | Accepted answer

What????
First of all, you should just use length instead of genericLegth.
λ > let xs = [1..6] in sum xs / length (drop xs)

:7:42: error:
    • Couldn't match expected type ‘Int’ with actual type ‘[a1]’
    • In the first argument of ‘drop’, namely ‘xs’
      In the first argument of ‘length’, namely ‘(drop xs)’
      In the second argument of ‘(/)’, namely ‘length (drop xs)’

The problem is that drop takes two input arguments, not 1 as you have given it in all three examples (the last of which is just wrong).
λ > :t drop
drop :: Int -> [a] -> [a]

Fixing that problem, you get another:
λ > let xs = [1..6] in sum xs / length (drop 2 xs)

:9:20: error:
    • No instance for (Fractional Int) arising from a use of ‘/’
    • In the expression: sum xs / length (drop 2 xs)
      In the expression: let xs = [1 .. 6] in sum xs / length (drop 2 xs)
      In an equation for ‘it’:
          it = let xs = ... in sum xs / length (drop 2 xs)

Now, you have a problem with the / operator not being usable with Int.
The solution is:
let xs = [1..6] in fromIntegral (sum xs) / fromIntegral (length $ drop 2 xs)
5.25

The reason this does not work:
let xs = [1..6] in sum xs / length $ drop 2 xs

is because it gets desugared to:
let xs = [1..6] in (sum xs / length) (drop 2 xs)

which does not make sense.

> **Comments:**
> **username_0**: Thank you very much, you have explained it very clearly to me, have a good day, hope to get more help from you in the future.  
> **username_0**: one more problem, hope you can explain it to me, for example drop has its type defined as follows: drop :: Int -> [a] -> [a] if I want to redefine my own drop function: drop' :: Num -> [a] -> [a] ----> it doesn't work even though Num includes Int in it. sorry for the stupid question, i'm a newbie and learning it from scratch  
> **username_1**: `Num` is not a type, its a type class. I suspect what you want is: ``` drop' :: Num b => b -> [a] -> [a] ``` The part to the left of the `=>` is a type constraint which says that `b` has to be a type that has a `Num` type class instance.  
> **username_0**: thank you very much, you clarified my problem, i got it. Have a nice day  

---

