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.  Explanation of composite with foldl (Lawrence Bottorff)
   2. Re:  Explanation of composite with foldl (Francesco Ariis)
   3.  foldr with ($) as the passed function (Lawrence Bottorff)
   4. Re:  foldr with ($) as the passed function (Ut Primum)


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

Message: 1
Date: Wed, 13 Jan 2021 15:09:35 -0600
From: Lawrence Bottorff <borg...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] Explanation of composite with foldl
Message-ID:
        <CAFAhFSVOCWSuiJEH9HLMqWtNLWnY30HiimyvDjs=47zheqt...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I see this on the Haskell 99 questions
<https://wiki.haskell.org/99_questions/Solutions/2> which will return the
second-from-last element of a list

lastbut1 :: Foldable f => f a -> a
lastbut1 = fst . foldl (\(a,b) x -> (b,x)) (err1,err2)
  where
    err1 = error "lastbut1: Empty list"
    err2 = error "lastbut1: Singleton"

I understand how the code works, but not the significance of the type
declaration. That looks like a type class. It works without it, I believe.
Why have we used Foldable type class?

Likewise with this

lastbut1safe :: Foldable f => f a -> Maybe a
lastbut1safe = fst . foldl (\(a,b) x -> (b,Just x)) (Nothing,Nothing)

What's happening with the type definition?

LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20210113/524197ce/attachment-0001.html>

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

Message: 2
Date: Wed, 13 Jan 2021 22:33:51 +0100
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Explanation of composite with foldl
Message-ID: <20210113213351.GA30794@extensa>
Content-Type: text/plain; charset=iso-8859-1

Il 13 gennaio 2021 alle 15:09 Lawrence Bottorff ha scritto:
> I see this on the Haskell 99 questions
> <https://wiki.haskell.org/99_questions/Solutions/2> which will return the
> second-from-last element of a list
> 
> lastbut1 :: Foldable f => f a -> a
> lastbut1 = fst . foldl (\(a,b) x -> (b,x)) (err1,err2)
>   where
>     err1 = error "lastbut1: Empty list"
>     err2 = error "lastbut1: Singleton"
> 
> I understand how the code works, but not the significance of the type
> declaration. That looks like a type class. It works without it, I believe.
> Why have we used Foldable type class?

A `Foldable` constraint will make the function work on a plethora of
types [1] apart from lists (e.g. Arrays, etc.).

The assignment is clearly monomorphic («Find the last but one element of
a list.»), I would have preferred a `:: [a] -> a` signature.

[1] 
https://hackage.haskell.org/package/base-4.14.1.0/docs/Data-Foldable.html#t:Foldable


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

Message: 3
Date: Thu, 14 Jan 2021 00:24:26 -0600
From: Lawrence Bottorff <borg...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] foldr with ($) as the passed function
Message-ID:
        <CAFAhFSUmcY-LDDO1WQEi-q52g0VK2sW_XOOCtnyYhx=bajf...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

With 99 questions <https://wiki.haskell.org/99_questions/Solutions/3> Problem
3 wants a function elementAt that will take a list and an index and return
the element for that index. One very odd version in the solutions is

elementAt xs n = head $ foldr ($) xs $ replicate (n - 1) tail

So the function "passed" is ($) and the accumulator "seed" is the incoming
list xs and the list to be worked on is (replicate (n-1) tail) which . . .
and I can't fathom what's happening -- other than perhaps (replicate (n-1)
(tail xs))

elementAt [1,2] 2 would be

foldr ($) [1,2] (replicate 1 (tail [1,2])
foldr ($) [1,2] ([2])

. . . now I'm lost. Can someone walk me through this?

LB
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20210114/8a4621cf/attachment-0001.html>

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

Message: 4
Date: Thu, 14 Jan 2021 08:16:04 +0100
From: Ut Primum <utpri...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] foldr with ($) as the passed function
Message-ID:
        <CANjDmKKLO71s+a+=tvxkk_rvhuv1momzj-bkkpbxvrqee2d...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi,

I think your interpretation of (replicate (n-1) tail) is wrong. First note
that tail is not applied to anything, is just the function tail.
So replicate (n-1) tail is [tail, tail, tail, .... , tail]. For example
replicate 3 tail = [tail, tail, tail]
Note that if you try to compute this in Haskell it won't be able to show
that result, because there is no "show" defined for something that has the
type of a list of functions.

Having said this, your example can now be written
elementAt [1,2] 2 =
= head (foldr ($) [1,2] (replicate 1 tail))
= head (foldr ($) [1,2] [tail])
= head (tail $ (foldr ($) [1,2] []))
= head (tail $ [1,2])
= head (tail ([1,2])) = head [2] = 2

For a more general example you can try

elementAt [1,2,3,4] 3 =
= head (foldr ($) [1,2,3,4] (replicate 2 tail))
= head (foldr ($) [1,2,3,4] [tail,tail])
= head (tail $ (foldr ($) [1,2,3,4] [tail]))
= head (tail $ tail $ [1,2,3,4])
= head (tail (tail [1,2,3,4)))
= head (tail [2,3,4]) = head [3,4] = 3

Cheers,
Ut

<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
Mail
priva di virus. www.avg.com
<http://www.avg.com/email-signature?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=webmail>
<#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>

Il giorno gio 14 gen 2021 alle ore 07:25 Lawrence Bottorff <
borg...@gmail.com> ha scritto:

> With 99 questions <https://wiki.haskell.org/99_questions/Solutions/3> Problem
> 3 wants a function elementAt that will take a list and an index and return
> the element for that index. One very odd version in the solutions is
>
> elementAt xs n = head $ foldr ($) xs $ replicate (n - 1) tail
>
> So the function "passed" is ($) and the accumulator "seed" is the incoming
> list xs and the list to be worked on is (replicate (n-1) tail) which . . .
> and I can't fathom what's happening -- other than perhaps (replicate (n-1)
> (tail xs))
>
> elementAt [1,2] 2 would be
>
> foldr ($) [1,2] (replicate 1 (tail [1,2])
> foldr ($) [1,2] ([2])
>
> . . . now I'm lost. Can someone walk me through this?
>
> LB
> _______________________________________________
> 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/20210114/03a770bd/attachment-0001.html>

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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 150, Issue 6
*****************************************

Reply via email to