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. question for advice on packages for a retro-style pocket
money ledger program (Benjamin L. Russell)
2. Re: Handling Maybes (Daniel Fischer)
3. Quickcheck Preconditions (Guy)
4. Re: Quickcheck Preconditions (Daniel Fischer)
5. Re: Quickcheck Preconditions (Jonas Almstr?m Dureg?rd)
6. Re: Quickcheck Preconditions (Guy)
----------------------------------------------------------------------
Message: 1
Date: Wed, 25 May 2011 19:19:50 +0900
From: [email protected] (Benjamin L. Russell)
Subject: [Haskell-beginners] question for advice on packages for a
retro-style pocket money ledger program
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
For some time, I have been considering rewriting my original pocket
money ledger program in Haskell, but am not sure as to which packages
would be most useful. My original pocket money ledger program was first
written in circa 1983 in N80-BASIC (a JIS-compatible ROM-based line
BASIC) on an NEC PC-8001 mkII [1].
The main difficulty is that I would like to preserve the original look
and feel of my original program while still rewriting it in Haskell.
However, my original program ran in an age before any kind of
Macintosh-style or Windows-style GUI had become common, and used basic
built-in ROM-based graphics and sound commands to draw a double-line
colored graphical border on a centered-colored-text screen, and saved
files to a floppy disk drive (floppy disks were actually considered
advanced for that age, since most personal computers then used a
cassette tape drive for external file storage).
As a result, I am uncertain as to which packages would be useful for
preserving the original look and feel of my program. At minimum, I
would like my program to exhibit the following behavior:
1. Start out by drawing a lime-green-colored double two-pixel wide box
surrounding the screen, with the title and author name displayed as
separately-colored text centered in the screen.
2. If possible, play a suitable music file while on this screen. Also,
display an option to mute this music, and if this music is once muted,
save this option for future runs of this program, so that the program
will start out with the music muted as the default option. Also,
display an option to control the volume of this music, and if possible,
also display a separate option to choose a different tune to play. The
tunes should be selectable from a selectable, expandable menu.
3. If possible, include a suitable screen saver to be displayed
automatically, separate from the GUI screen saver, upon a user-defined
period in which there has been no user input. The screen saver should
be configurable in a separate configuration menu.
4. Upon the user hitting the Return key, stop playing the music (if
playing), and move to the main menu screen.
5. The options displayed in the menu screen should include, at minimum,
the following:
a. Create a new ledger book.
b. Load a ledger book (preferably from floppy disk).
c. Add an entry to the ledger book.
d. Delete an entry from the ledger book.
e. Erase the ledger book.
f. Save the current ledger book (preferably to floppy disk).
g. Tabulate and display income and expenditures (leads to a separate
screen requesting a time period for tabulation). (The income and
expenditures should be displayed in detail both as numerical charts
and as colored graphs in a preferred form (pie chart, bar graph,
etc.).)
h. Set configuration options (leads to a separate screen for
configuring default background music, default volume, default
application screen saver, default theme, default fonts, default menu
behavior, default keyboard remappings, default screen height/width
ratio, etc.).
i. Exit the application.
5. For each option (except for option i), move to a separate sub-menu
screen. When input/output and processing have been completed on the
separate sub-menu screen, return to the main menu screen.
Most of the functionality of this program is related to some form of
input and output: colored formatted combined graphical and textual
title screen with background music, manual data entry by a user, saving
files to a floppy disk, reading files off a floppy disk, deleting files
from a floppy disk, configuring various default music/volume/screen
saver/theme/font/menu behavior/keyboard remapping/screen height and
width ratio options, and combined numerical and graphical representation
of statistical ledger information. Most of the computation involves
only simple arithmetic (mainly addition and subtraction, with perhaps
some multiplication and/or division, and no matrix manipulation).
The main point of this program is that program usage should be
interactive, and not require a separately entered file. The idea is
that the program will play the role of an interactive personal ledger
assistant.
However, I am not sure as to how to implement this program in Haskell
easily. Because most of the program is mainly concerned with side
effects, it is difficult to write it easily while preserving referential
transparency. This type of program seems relatively straightforward to
write in N80-BASIC (which is no longer available in the original
version), but is relatively less trivial in a purely functional
programming language such as Haskell. However, I am tired of spaghetti
code, and although writing each line of code in N80-BASIC is trivial,
managing control flow is not. It is very difficult to manage control
flow without writing spaghetti code in a dialect of line BASIC.
However, most of the associated graphics and sound commands are
proprietary and implementation-dependent, and I am not sure how to
rewrite that part of the functionality in an implementation-independent
language without spending lots of time on API-related issues.
-- Benjamin L. Russell
[1] _OLD-COMPUTERS.COM Museum ~ NEC PC 8001 MK 2_. NYI (New York
Internet). n.d. Web. May 25,
2011. <http://www.old-computers.com/museum/computer.asp?st=1&c=396>.
--
Benjamin L. Russell / DekuDekuplex at Yahoo dot com
http://dekudekuplex.wordpress.com/
Translator/Interpreter / Mobile: +011 81 90-6526-1406
"Furuike ya, kawazu tobikomu mizu no oto." -- Matsuo Basho^
------------------------------
Message: 2
Date: Wed, 25 May 2011 12:30:18 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Handling Maybes
To: [email protected]
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="iso-8859-1"
On Wednesday 25 May 2011 05:21:51, Elvio Toccalino wrote:
> Your second option may or may not be dangerous, but it sure looks
> tricky. I wouldn't use 'maybe' that way.
> How about this?
>
> main = do val <- someIOFunc -- val is a Maybe String
> maybe (someAction >> someOtherActionEtc)
> (\theValue -> putStrLn ("the string in reverse is " ++
> (reverse theValue))
> val
>
> If you like point-free definitions:
>
> main = do val <- someIOFunc -- val is a Maybe String
> maybe (someAction >> someOtherActionEtc)
> (putStrLn . ("the string in reverse is " ++) . reverse)
> val
>
> The difference is that my use of 'maybe' involves giving it actions, and
> not just pure functions.
I'd do that (perhaps in the form someIOFunc >>= maybe ... ...) or pattern-
match like Erik suggested.
I prefer not to pass long arguments to maybe, so if there's much to do with
someIOFunc's result, either name the branches and use
maybe nothingBranch justBranch val
or pattern-match
case val of
Nothing -> do
something
more
...
Just v -> do
foo v
w <- bar v
baz
(a bit more below)
>
> On Tue, 2011-05-24 at 19:09 -0800, Christopher Howard wrote:
> > Since I started into Haskell, I frequently run into a scenario like
> > this: some function returns a Maybe value. If it is a Nothing, I want
> > to do one thing, but if it isn't, I want to do something with the
> > value encapsulated by the Maybe. But before I can use that value
> > (say, to print it) I've got to pull it out of the Maybe in a way that
> > satisfies the compiler. So one option is to use a separate function
> > with pattern matching, for example:
> >
> > handleMaybeVal Nothing = do someAction
> >
> > someOtherActionEtc
> >
> > handleMaybeVal (Just a) = putStrLn ("The string in reverse is " ++
> >
> > (reverse a))
> >
> > Or I can use the maybe function with a conditional
> >
> > main = do val <- someIOFunc -- val is a Maybe String
> >
> > if val == Nothing
This introduces an Eq constraint, so you can't use that if someIOFunc
produces functions (or anything else without an Eq instance).
The case pattern-matching avoids that constraint, and it does the unpacking
in the same go too.
> >
> > then do someAction
> >
> > someOtherActionEtc
> >
> > else putStrLn ("The string in reverse is " ++
> >
> > ((reverse . unpack) val))
> >
> > where unpack a = maybe
> >
> > (error "unreachable error")
> > (\b -> b) a
That is
unpack = maybe (error "unrechable error") id
the function `maybe default id' is prominent enough to have its own name,
it's `fromMaybe' (exported from Data.Maybe), so
unpack = fromMaybe (error "unreachable error")
> >
> > The second option I like better, for some reason, but it is a bit
> > dangerous, as I have to double check my code to make sure the default
> > value passed to "maybe" cannot possibly be evaluated.
If you have that checked carefully, you could also use
unpack = fromJust
The difference to fromMaybe yourError is that fromJust produces a generic
error message if your check failed, so if there are several 'fromJust's in
the code, it doesn't tell you which of your checks failed, while your self-
supplied error should be easy to identify.
fromJust is a partial function, as such dangerous. Some people say you
should *never* use it, but even without going so far, fromJust [and head,
tail, ...] should only be used where you are *certain* that you can't get a
Nothing. Generally, using it indicates not-so-good design somewhere.
> > (Alternatively,
> > I could pass in a regular default value, like a blank string, though
> > that is actually more dangerous in my mind because a bad value might
> > be used silently.)
That may be the right thing to do in some circumstances, horribly wrong in
others. But when supplying a default value is sensible, you should probably
use a fromMaybe or maybe anyway.
> >
> > Anyway: how do you pros go about handling situations like these?
------------------------------
Message: 3
Date: Wed, 25 May 2011 11:40:20 +0100 (BST)
From: Guy <[email protected]>
Subject: [Haskell-beginners] Quickcheck Preconditions
To: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8
I have some functions with preconditions. For example, foo::[a]->IO Bool
requires a list of at least three elements, and bar::[a]->Int->IO () requires
that its second argument is some position within the list.
How can I test these with quickcheck? If I use ==>, quickcheck gives up with
very few test cases.
------------------------------
Message: 4
Date: Wed, 25 May 2011 13:04:05 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Quickcheck Preconditions
To: [email protected], Guy <[email protected]>
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="iso-8859-1"
On Wednesday 25 May 2011 12:40:20, Guy wrote:
> I have some functions with preconditions. For example, foo::[a]->IO Bool
> requires a list of at least three elements, and bar::[a]->Int->IO ()
> requires that its second argument is some position within the list.
>
> How can I test these with quickcheck? If I use ==>, quickcheck gives up
> with very few test cases.
>
You could increase the maxDiscard value,
ghci> quickCheckWith stdArgs{ maxDiscard = 5000 } (forAll arbitrary (\xs ->
(length xs > 2) ==> whatever))
or create a generator that produces only output fulfilling the
preconditions:
atLeast3 :: Arbitrary a => Gen [a]
atLeast3 = do
x <- arbitrary
y <- arbitrary
z <- arbitrary
ws <- arbitrary
return (x:y:z:ws)
ghci> quickCheck (forAll atLeast3 whatever)
------------------------------
Message: 5
Date: Wed, 25 May 2011 13:09:16 +0200
From: Jonas Almstr?m Dureg?rd <[email protected]>
Subject: Re: [Haskell-beginners] Quickcheck Preconditions
To: Guy <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
You can write your own generators and use the forall function. Or for these
simple examples you can just test on wrappers:
testFoo :: (a,a,a,[a]) -> IO Bool
testFoo (a1,a2,a3,as) = foo (a1:a2:a3:as)
testBar :: (a,[a]) -> Int -> IO ()
testBar (x,xs) n = bar xs' (n `mod` length xs') where xs' = x:xs
/J
On 25 May 2011 12:40, Guy <[email protected]> wrote:
> I have some functions with preconditions. For example, foo::[a]->IO Bool
> requires a list of at least three elements, and bar::[a]->Int->IO ()
> requires that its second argument is some position within the list.
>
> How can I test these with quickcheck? If I use ==>, quickcheck gives up
> with very few test cases.
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110525/03f86e26/attachment-0001.htm>
------------------------------
Message: 6
Date: Wed, 25 May 2011 13:13:22 +0100 (BST)
From: Guy <[email protected]>
Subject: Re: [Haskell-beginners] Quickcheck Preconditions
To: Jonas Almstr?m Dureg?rd <[email protected]>
Cc: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-1
Thanks, mod is a neat trick for applying the size. Is there a simple way of
specifying bounds for quickcheck, or would I have to write a custom generator
to do that?
>________________________________
>From: Jonas Almstr?m Dureg?rd <[email protected]>
>Sent: Wednesday, 25 May 2011, 14:09
>Subject: Re: [Haskell-beginners] Quickcheck Preconditions
>
>
>You can write your own generators and use the forall function. Or for these
>simple examples you can just test on wrappers:
>
>
>testFoo :: (a,a,a,[a]) -> IO Bool
>testFoo (a1,a2,a3,as) = foo (a1:a2:a3:as)
>
>
>testBar :: (a,[a]) -> Int -> IO ()
>testBar (x,xs) n = bar xs' (n `mod` length xs') where?xs' = x:xs?
>
>
>/J
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 35, Issue 48
*****************************************