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. Re: Reducing local variable duplication (Michael Orlitzky)
2. haskell stack trace.. (Sunil S Nandihalli)
3. Re: to modify the eagerness level of haskell compiler..
(Heinrich Apfelmus)
4. Re: Help me improve my code (Neuman Vong)
5. Re: haskell stack trace.. (Henk-Jan van Tuyl)
----------------------------------------------------------------------
Message: 1
Date: Tue, 30 Aug 2011 14:35:18 -0400
From: Michael Orlitzky <[email protected]>
Subject: Re: [Haskell-beginners] Reducing local variable duplication
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 08/30/11 08:15, Michael Orlitzky wrote:
>
> It /works/, but gives me that "what the hell is he doing.." feeling when
> re-reading my own code.
For what it's worth, this is the most sane thing I could devise that
still allows me to use Haddock:
trilinear_c0_t0_tests :: Test.Framework.Test
trilinear_c0_t0_tests =
testGroup "trilinear c0 t0"
[testGroup "coefficients"
[testCase "c0030 is correct" test_trilinear_c0030,
testCase "c0003 is correct" test_trilinear_c0003,
testCase "c0021 is correct" test_trilinear_c0021,
testCase "c0012 is correct" test_trilinear_c0012,
testCase "c0120 is correct" test_trilinear_c0120,
testCase "c0102 is correct" test_trilinear_c0102,
testCase "c0111 is correct" test_trilinear_c0111,
testCase "c0210 is correct" test_trilinear_c0210,
testCase "c0201 is correct" test_trilinear_c0201,
testCase "c0300 is correct" test_trilinear_c0300,
testCase "c1020 is correct" test_trilinear_c1020,
testCase "c1002 is correct" test_trilinear_c1002,
testCase "c1011 is correct" test_trilinear_c1011,
testCase "c1110 is correct" test_trilinear_c1110,
testCase "c1101 is correct" test_trilinear_c1101,
testCase "c1200 is correct" test_trilinear_c1200,
testCase "c2010 is correct" test_trilinear_c2010,
testCase "c2001 is correct" test_trilinear_c2001,
testCase "c2100 is correct" test_trilinear_c2100,
testCase "c3000 is correct" test_trilinear_c3000],
testGroup "face0 vertices"
[testCase "v0 is correct" test_trilinear_f0_t0_v0,
testCase "v1 is correct" test_trilinear_f0_t0_v1,
testCase "v2 is correct" test_trilinear_f0_t0_v2,
testCase "v3 is correct" test_trilinear_f0_t0_v3]
]
where
g = make_grid 1 trilinear
cube = cube_at g 1 1 1
t = tetrahedron0 cube
test_trilinear_c0030 :: Assertion
test_trilinear_c0030 =
assertAlmostEqual "c0030 is correct" (c t 0 0 3 0) (17/8)
...
<other tests using g, cube, t>
------------------------------
Message: 2
Date: Wed, 31 Aug 2011 04:47:49 +0530
From: Sunil S Nandihalli <[email protected]>
Subject: [Haskell-beginners] haskell stack trace..
To: beginners <[email protected]>
Message-ID:
<cap0fd73cdgaor1gq_chou2q6j9vcyd68komn+jvkr5jqpaw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hello everybody,
I was wondering if there is a way to get the haskell stack trace when I
throw the error. I found this
http://hackage.haskell.org/trac/ghc/wiki/ExplicitCallStack
but it seems like it is only a proposal to do something.. has anything
happened?
Thanks,
Sunil.
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110831/d013e50c/attachment-0001.htm>
------------------------------
Message: 3
Date: Wed, 31 Aug 2011 09:29:39 +0200
From: Heinrich Apfelmus <[email protected]>
Subject: Re: [Haskell-beginners] to modify the eagerness level of
haskell compiler..
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Sunil S Nandihalli wrote:
> the code for the same can be found at https://github.com/sunilnandihalli/is2
> it
> is a single main.hs file so compilation is simply
> * ghc main.hs --make*
> thanks,
> Sunil.
>
> Sunil S Nandihalli wrote:
>
>> Hi everybody,
>> May be I am doing it wrong being a beginner.. but here is what I want.. I
>> am trying to debug a stack overflow problem by printing a lot using "trace"
>> commands. But it does not seem to seem to be printing in the expected order.
>> so it has been misleading quiet a bit. Can somebody guide me as to how to
>> modify the laziness of the program or some pointers on how to debug the
>> stack overflow errors.
Woah, your code is way too complicated. I doubt that debugging support
will help you much.
My recommendation is to use abstract data types and to keep your
functions much shorter. For instance, what is the type
Integral a, Integral b => M.Map (Ratio a,Ratio a) b
supposed to mean? Give it a descriptive name
type Length = Ratio Integer -- coordinate
type Node = Int -- unique id's for points
type VoronoiSomething = M.Map (Length, Length) Node
and create many small utility functions that operate on this new
abstraction.
One thing that strikes out is the repeated test M.valid . You should
never need to call that. Likewise, you should never need to write or
call a function validTrapeziaMap if you have made good use of abstract
data types and the type system.
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 4
Date: Wed, 31 Aug 2011 01:28:36 -0700
From: Neuman Vong <[email protected]>
Subject: Re: [Haskell-beginners] Help me improve my code
To: Brent Yorgey <[email protected]>
Cc: [email protected]
Message-ID:
<CAGkxwxS-2-qnJ3GfLFu97gc2S=6z+dpqanwunhr6qkuhccb...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1
Hi Brent, thanks for your reply. I had another go at the code
following your advice, but I ended up going down a different path
eventually taking out the filter and replacing it with a recursive
function (see below). I like that this version is clearer to read, but
unfortunately I've doubled the number of lines. I have an equivalent
of it in python which is even more terse than the first version but
reads better than the second, which makes me suspect again that I'm
not taking full advantage of Haskell. But... is this the wrong way to
think?
(Thanks, for replying too, Antoine! I forget the exact errors I was having.)
module Main
where
import Control.Monad (guard, when)
import Data.Text (unpack, pack, splitOn)
import Data.List (maximumBy)
import Data.Function (on)
import System (getArgs, getProgName)
data Rate = Rate { prefix::String, price::String } deriving (Show)
find :: Int -> String -> [Rate] -> Maybe Rate
find _ _ [] = Nothing
find index key rates
| length rates == 1 = Just (last rates)
| length key <= index = Just (longestPrefix rates)
| otherwise = find (index + 1) key (match index key rates)
match :: Int -> String -> [Rate] -> [Rate]
match index key rates = do
rate <- rates
when (index < length (prefix rate))
(guard $ (prefix rate !! index) == (key !! index))
return rate
longestPrefix :: [Rate] -> Rate
longestPrefix = maximumBy (compare `on` (length . prefix))
toRates :: [String] -> [Rate]
toRates [] = []
toRates rates = map toRate rates
toRate :: String -> Rate
toRate line = let prefix:price:_ = splitOn' ", " line in Rate prefix price
splitOn' :: String -> String -> [String]
splitOn' delim str = fmap unpack (splitOn (pack delim) (pack str))
main = getArgs >>= \args ->
case args of
[] -> getProgName >>= \progName -> error ("Usage: " ++
progName ++ " <key>")
key:_ -> interact ((++ "\n") . show' . (find 0 key) . toRates . lines)
where show' Nothing = "Couldn't find a match"
show' (Just rate) = show rate
On Tue, Aug 30, 2011 at 7:48 AM, Brent Yorgey <[email protected]> wrote:
> On Mon, Aug 29, 2011 at 09:52:28PM -0700, Neuman Vong wrote:
>> Hi Haskell people,
>>
>> I'm pretty new to Haskell still. There were a bunch of things I didn't
>> know how to do in the following script, I'm hoping some people on this
>> list can help with. For example, I had trouble returning an ExitCode
>> and using getProgName without getting a compile-time type error. I
>> feel like I'm doing something wrong with the Text/[Char] conversions
>> too. I'd also really appreciate any style tips. Thanks in advance!
>>
>
> Hi Neuman,
>
> This looks pretty good. ?I'm not very familiar with the Text library,
> so perhaps someone else can comment on the conversions between Text
> and String. ?But I can offer a few comments on style:
>
>> {-# LANGUAGE OverloadedStrings #-}
>> module Main where
>>
>> import System (getArgs)
>> import System.IO (hPutStrLn, stderr)
>> import Data.Text (pack, splitOn, length, isPrefixOf, Text)
>> import Prelude hiding (length)
>>
>> data Rate = Rate Text Text deriving (Show)
>
> If you use record syntax:
>
> ?data Rate = Rate { getPrefix :: Text, getPrice :: Text }
>
> then you get the selector functions getPrefix and getPrice for free
> (so you don't have to write them in the 'where' clause below).
>
>>
>> findBestPrefix number rates = foldl1 longestPrefix $ matching rates
>> ? ?where
>> ? ? ? ?getLength rate = length $ getPrefix rate
>> ? ? ? ?getPrefix (Rate prefix _) = prefix
>> ? ? ? ?getPrice (Rate _ price) = price
>> ? ? ? ?longestPrefix r1 r2 = if getLength r1 > getLength r2 then r1
>> else r2
>> ? ? ? ?matching rates = [ rate | rate <- rates, getPrefix rate
>> `isPrefixOf` number ]
>
> The matching function can also be implemented with a call to 'filter'. The
> following three definitions are all equivalent, showing a progression
> of simplification:
>
> ?matching rates = filter (\rate -> getPrefix rate `isPrefixOf` number) rates
>
> ?matching = filter (\rate -> getPrefix rate `isPrefixOf` number)
>
> ?matching = filter ((`isPrefixOf` number) . getPrefix)
>
> At this point you could even inline the definition of matching if you
> wanted. ?You should use whichever of these definitions you find
> clearest, I just wanted to show what is possible.
>
>>
>> makeRates = map $ \line ->
>> ? ?let (prefix:rate:_) = splitOn ", " (pack line) in Rate prefix rate
>>
>> main = getArgs >>= \args ->
>> ? ?let findBest number rates = findBestPrefix number $ makeRates rates
>> ? ?in case args of
>> ? ? ? ?(arg:_) -> interact $ (++ "\n") . show . findBest (pack arg) . lines
>> ? ? ? ?_ -> hPutStrLn stderr "Pass in a number as the first argument"
>
> One general tip: it helps a lot, especially when learning, to give
> explicit type signatures to all your top-level functions. ?In fact, I
> still do this. ?I *first* write down a type signature, and *then*
> write an implementation. ?This may help you with your issues using
> getProgName and and ExitCode, although without more information about
> exactly what you were trying it's hard to know.
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
neuman
------------------------------
Message: 5
Date: Wed, 31 Aug 2011 11:04:28 +0200
From: "Henk-Jan van Tuyl" <[email protected]>
Subject: Re: [Haskell-beginners] haskell stack trace..
To: beginners <[email protected]>, "Sunil S Nandihalli"
<[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=iso-8859-15; format=flowed;
delsp=yes
On Wed, 31 Aug 2011 01:17:49 +0200, Sunil S Nandihalli
<[email protected]> wrote:
> Hello everybody,
> I was wondering if there is a way to get the haskell stack trace when I
> throw the error. I found this
> http://hackage.haskell.org/trac/ghc/wiki/ExplicitCallStack
>
> but it seems like it is only a proposal to do something.. has anything
> happened?
>
> Thanks,
> Sunil.
There was recently a message from Simon Marlow, that he figured out, how
to do a stack trace; according to
http://hackage.haskell.org/trac/ghc/ticket/3693
, this will be solved in GHC 7.2.1
Regards,
Henk-Jan van Tuyl
--
http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
--
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 38, Issue 50
*****************************************