Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/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. making function problem (chapter 6 of Programming in
Haskell) (Roelof Wobben)
2. Re: making function problem (chapter 6 of Programming in
Haskell) (Ertugrul Soeylemez)
3. Re: making function problem (chapter 6 of Programming in
Haskell) (Roelof Wobben)
4. Re: Conciseness question (Manfred Lotz)
5. Re: making function problem (chapter 6 of Programming in
Haskell) (Ertugrul Soeylemez)
6. Parse string with optional entries [Parsec]
([email protected])
7. having trouble with helloworld style program (Sunil S Nandihalli)
8. Re: having trouble with helloworld style program
(Michael Snoyman)
9. Re: Parse string with optional entries [Parsec] (Antoine Latter)
----------------------------------------------------------------------
Message: 1
Date: Mon, 8 Aug 2011 10:27:14 +0000
From: Roelof Wobben <[email protected]>
Subject: [Haskell-beginners] making function problem (chapter 6 of
Programming in Haskell)
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello,
After a short holiday I now studying chapter 6 of this book.
For the first exercise I have to make the function for ^ for postitive numbers.
So I did these steps.
Step 1 : Defining the type
^ :: [Int] -> Int
Step 2 : enumarate the cases
^ [] =
^ [n, 0] =
^ [n, ns] =
Step 3 : Define the simple cases
^ [] = []
^ [n.0] = 1
Step 4 : Define the other cases
^[n.ns] = n * ^ns
Step 5 : generalize and simplify :
^[] = []
^[-, 0] = 1
So the whole function would be :
^ :: [Int] -> Int
^[] = []
^[-, 0] = 1
^ [n,ns] = n * ^ns
But the answer in the book only says :
m ^ 0 = 1
m (n+1) = m * m ^n
so no type defenition and a whole different solution to the last rule.
So my question is : Is what I did wrong ?
Roelof
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110808/b311a2e1/attachment-0001.htm>
------------------------------
Message: 2
Date: Mon, 8 Aug 2011 12:51:34 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] making function problem (chapter 6 of
Programming in Haskell)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Roelof Wobben <[email protected]> wrote:
> After a short holiday I now studying chapter 6 of this book.
>
> For the first exercise I have to make the function for ^ for postitive
> numbers.
I assume you mean the exponentiation function, and the only real
indication for that is the solution you quoted later in your post. To
help us help, you really should work on your problem descriptions.
> Step 1 : Defining the type
>
> ^ :: [Int] -> Int
First of all, you have to learn proper Haskell syntax. Your type
signature is invalid. But let me rewrite your type signature to correct
syntax:
(^) :: [Int] -> Int
This is the type signature for a function (^), which expects exactly one
argument, a list. Is this really what you want? Before going any
further, you should come up with the right type signature for your
function. Once you have that, we will continue.
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
------------------------------
Message: 3
Date: Mon, 8 Aug 2011 11:06:03 +0000
From: Roelof Wobben <[email protected]>
Subject: Re: [Haskell-beginners] making function problem (chapter 6 of
Programming in Haskell)
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
> To: [email protected]
> From: [email protected]
> Date: Mon, 8 Aug 2011 12:51:34 +0200
> Subject: Re: [Haskell-beginners] making function problem (chapter 6 of
> Programming in Haskell)
>
> Roelof Wobben <[email protected]> wrote:
>
> > After a short holiday I now studying chapter 6 of this book.
> >
> > For the first exercise I have to make the function for ^ for postitive
> > numbers.
>
> I assume you mean the exponentiation function, and the only real
> indication for that is the solution you quoted later in your post. To
> help us help, you really should work on your problem descriptions.
>
>
> > Step 1 : Defining the type
> >
> > ^ :: [Int] -> Int
>
> First of all, you have to learn proper Haskell syntax. Your type
> signature is invalid. But let me rewrite your type signature to correct
> syntax:
>
> (^) :: [Int] -> Int
>
> This is the type signature for a function (^), which expects exactly one
> argument, a list. Is this really what you want? Before going any
> further, you should come up with the right type signature for your
> function. Once you have that, we will continue.
>
oke,
I don't think I want that.
I want to type this 2^3 and then the outcome will be 8.
So next try
(^) :: Int -> Int -> Int
Because the first and second numbers are integers and the outcome also will be
a integer.
Roelof
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110808/61ebfe6a/attachment-0001.htm>
------------------------------
Message: 4
Date: Mon, 8 Aug 2011 13:10:44 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] Conciseness question
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
On Mon, 8 Aug 2011 09:48:11 +0200
Ertugrul Soeylemez <[email protected]> wrote:
> Manfred Lotz <[email protected]> wrote:
>
> > > import Data.Map (Map, (!), lookup)
> > >
> > > lookup ConfigAppDir myDirs
> > > myDirs ! ConfigAppDir
> >
> > This is the solution I like. I have to accept that here I cannot
> > reach the conciseness of which might be due to Haskell being
> > strongly typed.
>
> Note that likely this solution is no different from the Lua solution.
> You are just coding directly for what you have syntactic sugar in Lua.
Yes, of course. That is syntactic sugar in Lua.
> After all the interpreter keeps a list (or more likely a map) of
> variables and just does indexing and mapping.
Sure.
>
> Also I don't find it terribly less concise. It's just the
> overwhelming import stuff, which makes the code larger in such a
> small example.
>
Yeah, I can live with that.
> > > The former is the safe variant giving you a Maybe String, while
> > > the latter is the unsafe variant, which throws an exception, if
> > > the directory in question is not present.
> >
> > Is the latter one really unsafe? I'm not quite sure how to code
> > something that the compiler accepts and crashes at runtime because
> > mydirs :: Map AppDir FilePath and I would believe that the compiler
> > would detect if the values after the ! is not from AppDir.
>
> The compiler cannot detect that, because you are essentially writing a
> mini-interpreter. Just like Lua cannot predetect the absence of a
> variable at parse time. To be safe you should use lookup:
>
> lookup :: Ord k => k -> Map k a -> Maybe a
>
> This isn't too bad, because Maybe is a monad, and you have convenient
> combinators like 'maybe':
>
> maybe (putStrLn "Configuration directory not specified")
> doSomethingWithConfigDir
> myAppDirs
>
Thanks much for the explanations you gave me (not only this reply). That
was some new stuff for me as a Haskell beginner.
--
Manfred
------------------------------
Message: 5
Date: Mon, 8 Aug 2011 13:26:14 +0200
From: Ertugrul Soeylemez <[email protected]>
Subject: Re: [Haskell-beginners] making function problem (chapter 6 of
Programming in Haskell)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
Roelof Wobben <[email protected]> wrote:
> I don't think I want that.
> I want to type this 2^3 and then the outcome will be 8.
> So next try
>
> (^) :: Int -> Int -> Int
>
> Because the first and second numbers are integers and the outcome also
> will be a integer.
This looks more like it. Now forget about programming for a moment and
think about how you can express the exponentiation operation in terms of
simple equations. Let me show you how you would do that for addition of
natural numbers, if you have only successor (succ) and predecessor
(pred) functions available:
x + 0 = x
x + y = succ x + pred y
Evaluation shows:
3 + 2 = succ 3 + pred 2
= 4 + 1
= succ 4 + pred 1
= 5 + 0
= 5
A similarly simple ruleset can express exponentiation, too.
Greets,
Ertugrul
--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
------------------------------
Message: 6
Date: Mon, 8 Aug 2011 11:39:41 +0000
From: <[email protected]>
Subject: [Haskell-beginners] Parse string with optional entries
[Parsec]
To: <[email protected]>
Message-ID:
<d51f81ac58698f4c9f3819136bfada3f03748...@mucse504.eu.infineon.com>
Content-Type: text/plain; charset="us-ascii"
Hi all,
I'm trying to parse a string in one of the following forms using Parsec:
"1\t1\t123\t456" -> desired output: [Just 1, Just 1, Just 123, Just 456]
or
"1\t\1\t\t456" -> desired output: [Just 1, Just 1, Nothing, Just 456]
i.e., the input is a list of numbers, separated by '\t' characters, with the
possibility of missing entries.
I'm having troubles with the backtracking in case of missing numbers. Can
anybody give me a hint?
Thanks in advance,
Stefan
------------------------------
Message: 7
Date: Mon, 8 Aug 2011 17:09:29 +0530
From: Sunil S Nandihalli <[email protected]>
Subject: [Haskell-beginners] having trouble with helloworld style
program
To: [email protected]
Message-ID:
<CAP0FD73g0i3LSDd=X9sXKF9-mR6UMsVvOr7Dn6pe7gU8Xr5=7...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hello everybody,
All I want to do in the following code is to read an integer from stdin
and print it back to stdout. Can somebody help me fix my code snippet
below..
Thanks,
Sunil
module Main where
import Prelude
stringToInt::String->Int
stringToInt str = read str
main =
do x<-getLine
y<-stringToInt x
print y
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110808/4e18bb69/attachment-0001.htm>
------------------------------
Message: 8
Date: Mon, 8 Aug 2011 14:43:14 +0300
From: Michael Snoyman <[email protected]>
Subject: Re: [Haskell-beginners] having trouble with helloworld style
program
To: Sunil S Nandihalli <[email protected]>
Cc: [email protected]
Message-ID:
<caka2jgjt3sjlxxjmhuvacd6hcd0hqol9rpevnbug1ur2xxd...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
On Mon, Aug 8, 2011 at 2:39 PM, Sunil S Nandihalli
<[email protected]> wrote:
> Hello everybody,
> ?All I want to do in the following code is to read ?an integer from stdin
> and print it back to stdout. Can somebody help me fix my code snippet
> below..
> Thanks,
> Sunil
>
> module Main where
> import Prelude
> stringToInt::String->Int
> stringToInt str = read str
> main =
> ?? ?do x<-getLine
> ?? ? ? y<-stringToInt x
> ?? ? ? print y
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
The code is *almost* right. You just need to change the second to last
line to be:
let y = stringToInt x
Hoping someone else steps in with a good, beginner-friendly
explanation of *why*.
Michael
------------------------------
Message: 9
Date: Mon, 8 Aug 2011 06:44:21 -0500
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] Parse string with optional entries
[Parsec]
To: [email protected]
Cc: [email protected]
Message-ID:
<cakjsnqhcnrc5t58efko-omyean-e4+ykpxte+xrtey58tmy...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
On Mon, Aug 8, 2011 at 6:39 AM, <[email protected]> wrote:
> Hi all,
>
> I'm trying to parse a string in one of the following forms using Parsec:
>
> "1\t1\t123\t456" -> desired output: [Just 1, Just 1, Just 123, Just 456]
>
> or
>
> "1\t\1\t\t456" ?-> desired output: [Just 1, Just 1, Nothing, Just 456]
>
> i.e., the input is a list of numbers, separated by '\t' characters, with the
> possibility of missing entries.
> I'm having troubles with the backtracking in case of missing numbers. Can
> anybody give me a hint?
>
What do you have so far?
> Thanks in advance,
> Stefan
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 38, Issue 16
*****************************************