Send Beginners mailing list submissions to
        [email protected]

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
        [email protected]

You can reach the person managing the list at
        [email protected]

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


Today's Topics:

   1.  Continuations (Shishir Srivastava)
   2. Re:  Continuations (David McBride)
   3. Re:  Continuations (Amy de Buitl?ir)
   4.  Working with Haskell matrices (Dananji Liyanage)
   5. Re:  Using IO values for computations (Jonathan Sk?rstedt)
   6.  Data.Vector in Haskell (Dananji Liyanage)


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

Message: 1
Date: Thu, 28 May 2015 14:57:22 +0100
From: Shishir Srivastava <[email protected]>
To: beginners <[email protected]>
Subject: [Haskell-beginners] Continuations
Message-ID:
        <cale5rtvydkhby_j+fpp_xbnbl565oysqt5t_m0fau7jds4w...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi,

Reading on continuation I've came across this new style of creating the
functions which I guess is not very clear in how it works

---------------
Prelude> let add_cps x y = \k -> k (x+y)
Prelude> add_cps 3 4 $ print
7
---------------

I have some questions as to
1) what is the role of variable 'k' and what eventually happens to it.
2) How does print work after the $ because there is clearly no parameter
being passed to it.

Thanks,
Shishir Srivastava
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150528/c5379dac/attachment-0001.html>

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

Message: 2
Date: Thu, 28 May 2015 12:58:47 -0400
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Continuations
Message-ID:
        <CAN+Tr40JZprEwKomZNMraTq-KkN=krm7a2c-nrov83q9ykm...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

\k -> k (x + y)

is equivalent to

blah k = k (x + y)

Except for the fact that you have no function name to work with, which is
why it is called an anonymous function (or lambda).  You can use it
anywhere in your code without having to bother create a named function.

On Thu, May 28, 2015 at 9:57 AM, Shishir Srivastava <
[email protected]> wrote:

> Hi,
>
> Reading on continuation I've came across this new style of creating the
> functions which I guess is not very clear in how it works
>
> ---------------
> Prelude> let add_cps x y = \k -> k (x+y)
> Prelude> add_cps 3 4 $ print
> 7
> ---------------
>
> I have some questions as to
> 1) what is the role of variable 'k' and what eventually happens to it.
> 2) How does print work after the $ because there is clearly no parameter
> being passed to it.
>
> Thanks,
> Shishir Srivastava
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150528/b39e1533/attachment-0001.html>

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

Message: 3
Date: Thu, 28 May 2015 17:17:11 +0000 (UTC)
From: Amy de Buitl?ir <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Continuations
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Hi Shishir,

Let's first consider a more straightforward version of the function.

?> let add_cps x y k = k (x+y)

So add_cps is a function of x, y, and k, where k is a function that will be
applied to x+y. Effectively it converts a function (k) of one argument into
a function that takes two arguments. Let's examine the type signature.

Now, k could be any function that takes one variable. Why don't we try
"print"? In the next example, x=3, y=4, and k=print. What do we expect
add_cps to do with those arguments? Well, it should apply the function k
(which is "print") to the argument x+y.

?> add_cps 3 4 print
7

And that's what it does!

Remember how partial application works? What would happen if we invoke
add_cps, but leave out the final argument? The result is a function. That
function has no name, and we can't print it, but we can look at its type
signature.

?> :t add_cps 3 4
add_cps 3 4 ? Num a ? (a ? t) ? t

So this unnamed function takes another function of type a ? t, and returns a
function of type t. Effectively it converts a function of one argument into
a function that takes two arguments. Let's try that with "print".

?> let f = add_cps 3 4
?> f print
7

This leads us to an equivalent way to write add_cps:

?> let add_cps x y = \k -> k (x+y)

This is identical to the first definition, even though it looks very
different! Can you see why?

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

Message: 4
Date: Wed, 27 May 2015 12:31:43 +0530
From: Dananji Liyanage <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Working with Haskell matrices
Message-ID:
        <CAAPsY8xX0u+71nyhK6siOg6LPdh=kpbudQyTUEidgURCr=u...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi All,

I'm implementing a puzzle where the input is given as a matrix, using
Data.Matrix package in Haskell.

How do I implement similar functions to `union`, which are available for
lists on a matrix?


-- 
Regards,
Dananji Liyanage
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150527/5e7050e4/attachment-0001.html>

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

Message: 5
Date: Mon, 25 May 2015 17:49:52 +0200
From: Jonathan Sk?rstedt <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Using IO values for computations
Message-ID:
        <CAD6iYwYU4zKNmSSxSQ4dBSnocXBHR_GCkn=qrBESoNN=evz...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

When you do computations involving IO, you want to be in IO context.

For instance, let's say we want to count the amount of rows in a file, and
we're going to use readFile for this

First, lets look at the types

readFile :: FilePath -> IO String

This is a computation with an IO result, which means we should only access
it by IO actions.

computeLines :: String -> Int
computeLines = length . lines

Lines is an example computation for calculating the amount of lines in a
file. It's composed of the function lines :: String -> [String] which
splits a string into a list of strings, divided by line break.

This is the function we want to use the result of readFile on, but as it's
IO, the type system won't let us. However, there's hope still

countLines :: FilePath -> IO Int
countLines fp = do
  file <- readFile fp
  return $ computeLines file


By the powers of the do notation and IO actions we can compose the
functions and still live in the comfortable context of IO actions.

If you're not used to IO computations, this snippet will contain some
concepts that probably are new to you:
* The "do" notation; it's a syntactic sugar for doing sequential
computations within a type, like IO actions.
* The left pointing arrow (<-) is a type-safe way to extract a contextual
value (like IO String) into a computable value (like String).
* return packages a computation (like Int) into a contextual value (like IO
Int).

So what we do is fetching the result from readFile with <-, computes it
(computeLines file) and package it from an Int to an IO Int.

Best regards,
Jonathan

2015-05-25 14:29 GMT+02:00 Magnus Therning <[email protected]>:

> On 25 May 2015 at 08:44, Dananji Liyanage <[email protected]> wrote:
> > Hi All,
> >
> > I'm writing a code, where the input is read from a text file using:
> > readValues = readFile "Input.txt"
> >
> > Since the type of this is 'IO String', I can't use this in the consequent
> > functions.
> >
> > For an example: I want to split this as follows within another function
> >
> > extractInput url method template
> >   | isURI url == True = getList values components
> >   | otherwise = []
> >   where components = splitTemplate readValues
> >         values = getURL (splitURL url) method
> >
> > This gives the following error:
> >
> >  Couldn't match type ?IO String? with ?[Char]?
> >     Expected type: String
> >       Actual type: IO String
> >
> > How can I solve this?
>
> Start with reading some basic Haskell book/tutorial.  That should tell
> you how to e.g. use 'do' notation, or `liftM`, to achieve what you
> want.
>
> /M
>
> --
> Magnus Therning                      OpenPGP: 0xAB4DFBA4
> email: [email protected]   jabber: [email protected]
> twitter: magthe               http://therning.org/magnus
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Jonathan Sk?rstedt
Bergsg?rdsg?rdet 39
Lgh 1108
424 32 G?teborg
Mobil: 073 - 76 20 20 7
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150525/7c2ed8d1/attachment-0001.html>

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

Message: 6
Date: Wed, 27 May 2015 13:45:13 +0530
From: Dananji Liyanage <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Data.Vector in Haskell
Message-ID:
        <caapsy8y4ke+vasamqh1urejvjdceqsa1kfdioz4zhg5ihqn...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi All,

I'm trying to extract elements from a Vector using 'head' and 'tail'
function in Data.Vector package, but it gives me the following error now.

<interactive>:41:18:
    Couldn't match expected type ?Data.Vector.Vector a?
                with actual type ?vector-0.10.9.1:Data.Vector.Vector Int?
    NB: ?Data.Vector.Vector?
          is defined in ?Data.Vector? in package ?vector-0.10.12.3?
        ?vector-0.10.9.1:Data.Vector.Vector?
          is defined in ?Data.Vector? in package ?vector-0.10.9.1?
    Relevant bindings include it :: a (bound at <interactive>:41:1)
    In the first argument of ?Data.Vector.head?, namely ?it?
    In the expression: Data.Vector.head it

It was working fine before.

Any help would be appreciated. Thanks in advance!

-- 
Regards,
Dananji Liyanage
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20150527/82470e28/attachment.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 83, Issue 56
*****************************************

Reply via email to