Re: [Haskell-cafe] Stupid question #374: why is MaybeT not in the standard library?

2008-12-22 Thread J. Garrett Morris
On Mon, Dec 22, 2008 at 2:31 PM, Brian Hurt bh...@spnz.org wrote:
 But I'm wondering why it's not in the standard library.  The standards
 committee just hasn't gotten around to it yet?  Or was there some discussion
 of this in the past on some (public) maillist, that my admittedly shallow
 googling failed to uncover, that someone could point me at?

It's equivalent to ErrorT ()  - but ErrorT String is almost always a
better option anyway.

 /g

-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Time for a new logo?

2008-12-14 Thread J. Garrett Morris
On Sun, Dec 14, 2008 at 2:38 PM, Don Stewart d...@galois.com wrote:
 ketil:
 Nice.  For some more hubris, replace 'A' with 'The'.

 I had the very same thought :)

It certainly wouldn't do to let, say, the existence of Concurrent
Clean get in the way of our self-promotion.

 /g

-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Instances that shouldn't overlap

2008-11-26 Thread J. Garrett Morris
On Wed, Nov 26, 2008 at 1:54 PM, Miguel Mitrofanov
[EMAIL PROTECTED] wrote:
 Maybe it'd be more intuitive if written backwards:

 AppEq f a = (Applicative f, Eq a)

 or even

 AppEq f a = (Applicative f, Eq a)

The first is good, the second isn't.  The first says the right thing:
if you can prove Applicative f and Eq a, you have a way to prove AppEq
f a.  The second has the implication the wrong way around.

Classes get the implication wrong too:  class Eq a = Ord a doesn't
say that if you can prove Eq a you can prove Ord a; it says that if
you can prove Ord a you can prove Eq a

 /g

-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Type question in instance of a class

2008-11-18 Thread J. Garrett Morris
On Tue, Nov 18, 2008 at 1:38 AM, Reiner Pope [EMAIL PROTECTED] wrote:
 ATs are Associated Types, aka Type Families. They can be found in
 the GHC 6.10 manual here:
 http://haskell.org/ghc/docs/6.10.1/html/users_guide/type-families.html

 As a starting point, you might want to try something like:

 class Complex c where
   type RealType c
   realPart :: c - RealType c
   imagPart :: c - RealType c

I imagine that the generalized newtype deriving might be trickier to
get working for this formulation.

 /g

-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Type question in instance of a class

2008-11-17 Thread J. Garrett Morris
On Mon, Nov 17, 2008 at 10:38 AM, Maurí­cio [EMAIL PROTECTED] wrote:
 newtype ComplexWithDouble = ComplexWithDouble (ComplexNumber Double)
  deriving ...

Perhaps you want something like:

class Complex r c | c - r
where makeComplex :: r - r - c
  realPart :: c - r
  imagPart :: c - r

data ComplexNumber t = CN t t
instance Complex t (ComplexNumber t)
where makeComplex = CN
  realPart (CN r _) = r
  imagPart (CN _ i) = i

newtype ComplexWithDouble = CWD (ComplexNumber Double)
deriving (Complex Double)

Having the parameters backwards is somewhat annoying, I suppose, but
it's unavoidable if you're hoping to use generalized newtype deriving
I believe.

 /g

-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Type question in instance of a class

2008-11-16 Thread J. Garrett Morris
On Sun, Nov 16, 2008 at 1:32 PM, Maurí­cio [EMAIL PROTECTED] wrote:
 Hi,

 Why is this wrong?

 
 class MyClass r where function :: r - s

 data MyData u = MyData u

 instance MyClass (MyData v) where function (MyData a) = a
 

 GHC says that the type of the result of 'function' is both determined by
 the rigid type from MyClass and  the rigid type from MyData. But why
 can't both be the same?

As Bulat said, your type signature is equivalent to:

function :: forall r s. r - s

whereas the result you're producing can't produce any s, but only
particular s's.  In essence, the result type is determined by the
input type.  One way to code this would be to use functional
dependencies:

class MyClass r s | r - s where function :: r - s
data MyData u = MyData u
instance MyClass (MyData v) v where function (MyData a) = a

 /g

-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] getLine and ^C on Windows

2008-11-13 Thread J. Garrett Morris
I've had the same experience with runghc in GHC 6.10.1 on Vista.

 /g

2008/11/12 Lyle Kopnicky [EMAIL PROTECTED]:
 Hi folks,
 I'm using System.IO.getLine to read input in my program. I've compiled it on
 Windows Vista with ghc-6.10.1. I've noticed that if I press Ctrl+C while the
 program is waiting for input, it will get stuck. No keypresses can get me
 out of the program - I have to kill the shell. I've tried cmd.exe,
 Powershell and cygwin - same behavior.
 I'm sure editline would work better, but it's overkill. I just want to read
 lines of text and don't need fancy edit behavior. And I think editline
 complicates the build process. I also want my program to work right in
 non-interactive mode, reading redirected input.
 Am I missing something?
 Thanks,
 Lyle
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe





-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] [Somewhat OT] Speed

2008-10-28 Thread J. Garrett Morris
On Tue, Oct 28, 2008 at 12:31 PM, Andrew Coppin
[EMAIL PROTECTED] wrote:
 This isn't specifically to do with Haskell, but... does anybody have any
 idea roughly how fast various CPU operations are?

Yes: it's architecture dependent.  I imagine you'll need to make your
questions at least somewhat more specific to get decent answers.

 /g

-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Crash!

2008-10-23 Thread J. Garrett Morris
On Thu, Oct 23, 2008 at 11:00 AM, Andrew Coppin
[EMAIL PROTECTED] wrote:
 I was under the impression that this is impossible, so I'm now slightly
 worried.

I'm not sure why you'd think that:

import Foreign

fail :: IO Int
fail = peek nullPtr

main = fail = print

 /g

-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Foreign.Marshal.Error.void

2008-10-14 Thread J. Garrett Morris
mapM and mapM_ have different complexity - I don't know if the
compiler would be smart enough to infer mapM_ style behavior for (void
. mapM).

 /g

On Tue, Oct 14, 2008 at 10:52 AM, Mauricio [EMAIL PROTECTED] wrote:
 Hi,

 Wouldn't it be nice if we had something
 like 'void' in Foreign.Marshal.Error in
 standard monad functions, so that we could
 use it instead of, for instance, mapM_
 or sequence_?

 Best,
 Maurício

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe




-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Improving MTL instances (was: Overlapping/Incoherent instances)

2008-10-13 Thread J. Garrett Morris
On Mon, Oct 13, 2008 at 12:29 AM, Ryan Ingram [EMAIL PROTECTED] wrote:
 Of course, the point of this message isn't just to complain.  The
 overlap implementation was abhorrent and it *is* better now than it
 was before.

I'm curious what you find abhorrent about the overlap implementation
that was there before - in particular, it seems like it was designed
to handle both the combinatorial explosion and the corner cases you
mentioned.  Did you find writing the MonadTrans instances unpleasant?
Was it the presence of overlapping instances at all?

On Mon, Oct 13, 2008 at 12:35 AM, Don Stewart [EMAIL PROTECTED] wrote:
 I just want to make one small point here encouraging people to try out
 new 'mtl' libraries. There are lots of *new* monad libraries,

Without knowing better myself: do any of these libraries address the
issue Ryan's brought up?  I know that monadLib takes the same approach
the MTL does to this, and so is likely to have the same difficulties.

 /g

-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Overlapping/Incoherent instances

2008-10-12 Thread J. Garrett Morris
On Sun, Oct 12, 2008 at 2:12 PM, Don Stewart [EMAIL PROTECTED] wrote:
 Though I note mtl doesn't actually list OverlappingInstances in its
 .cabal file,

Indeed - MTL seems to have been rewritten at some point in the past to
prefer exhaustive enumeration to overlap.

Thank you for the other suggestions!  Presumably this also doesn't
cover if they're enabled by LANGUAGE pragmas in individual files?

 /g

-- 
I am in here
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell PNG Writer

2008-05-09 Thread J. Garrett Morris
As long as you don't mind producing two-color images,
http://haskell.org/haskellwiki/Library/PNG is an option.  I found it
very easy to extend it to eight-bit grayscale - I didn't need
fullcolor images.

 /g

On Sat, May 3, 2008 at 11:12 PM, Nahuel Rullo [EMAIL PROTECTED] wrote:
 Hi list, i am new in Haskell. I need to make images (PNG, JPEG) with
 haskell, if you can give me a tutorial, thanks!

 --
 Nahuel Rullo
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe




-- 
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Tim Sweeney (the gamer)

2008-01-09 Thread J. Garrett Morris
I imagine you can get in touch with him through Epic
(www.epicgames.com) if you can't find another way to contact him.

 /g

On Jan 9, 2008 4:21 PM, Galchin Vasili [EMAIL PROTECTED] wrote:
 Hello,

  I have been reading with great interested Tim Sweeney's slides on the
 Next Generation Programming Language. Does anybody know his email address?

 Kind regards, Vasili


 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe





-- 
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] A small question

2007-11-21 Thread J. Garrett Morris
On Nov 21, 2007 5:16 AM, Jeremy O'Donoghue [EMAIL PROTECTED] wrote:
 Not just Windows Vista. Applications and DLLs compiled with Visual
 Studio 2005 (Express or full version) seem to need it to run on XP as
 well.

I believe the dependency here is version 8 of the Visual C RTL.
Applications that don't touch the RTL don't need to worry about it.

 Basically it contains a list of *exactly* which libraries were used to
 link the application, as well as permissions. It's alleged to
 eliminate DLL hell (which it does by replacing it with Manifest hell).

Since, at least with Microsoft's tools, the manifest is normally
included in the executable image instead of being a separate file, I'm
not sure how they could become confused and/or out of sync, as DLLs
could become.

 /g

-- 
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] A small question

2007-11-15 Thread J. Garrett Morris
http://msdn2.microsoft.com/en-us/library/1w45z383(vs.71).aspx

I believe.

 /g

On Nov 15, 2007 12:56 PM, Andrew Coppin [EMAIL PROTECTED] wrote:
 I notice that in GHC 6.8.1, if I compile a runnably program, as well as
 generating foo.exe, GHC now also generates a file foo.exe.manifest,
 which appears to contain some kind of XML data. Anybody know anything
 about this mysterious file?

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe




-- 
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Memory Leak Help

2007-11-11 Thread J. Garrett Morris
Hello,

I have code which seems to contain a memory leak, but I'm not sure
where it is or what's causing it.  Any help would be greatly
appreciated:

The code is:

data Ratings = Ratings { movieCount :: Int
   , movieLookup :: IOUArray Int Word32
   , movieRatings :: IOUArray Word32 Word32 }

readRatingsFromText :: Int - Int - C.ByteString - IO Ratings
readRatingsFromText movieCount ratingCount text =
do movieLookup - newArray_ (0, movieCount - 1)
   movieRatings - newArray_ (0, fromIntegral ratingCount - 1)
   iter movieLookup movieRatings 0 (C.lines text)
   return (Ratings movieCount movieLookup movieRatings)
where iter :: IOUArray Int Word32 - IOUArray Word32 Word32 -
Word32 - [C.ByteString] - IO ()
  iter !movieLookup !movieRatings !i [] = return ()
  iter !movieLookup !movieRatings !i (s:ss)
  | c == ':' = do writeArray movieLookup movie i
  iter movieLookup movieRatings i ss
  | otherwise = do writeArray movieRatings i rating-- ***
   iter movieLookup movieRatings (i + 1) ss
  where Just (x, rest) = C.readInt s
c  = C.head rest
Just (y, _)= C.readInt (C.tail rest)
movie  = x - 1
rating = fromIntegral x `shiftL` 3 .|.
fromIntegral y :: Word32

main = do args - getArgs
  ratings - readRatingsFromText (read $ args !! 0) (read $
args !! 1) = B.readFile (args !! 2)
  return ()


This attempts to read a file that looks like:

1:
401,2
503,1
...
2:
506,5
230,4

possibly with additional junk at the end of each line into an array.
When I run this, memory usage grows without bound.

If I comment out the line with -- ***, the memory profile is flat.
Without storing the ratings, it's not fantastically useful; however,
it does demonstrate that it should be possible to iterate through the
file with a flat memory profile (which is my goal).

I'm compiling with ghc --make -O2

Any help would be greatly appreciated.  Thanks!

 /g

-- 
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Building production stable software in Haskell

2007-09-13 Thread J. Garrett Morris
I believe that rnf from the Control.Parallel.Strategies library
shipped with GHC 6.6.1 is equivalent to deepSeq, as in:

x `deepSeq` yis equivalent to   rnf x `seq` y

Isn't it?

 /g

On 9/12/07, Peter Verswyvelen [EMAIL PROTECTED] wrote:
 Thanks for all the info.

 It's really good news that code coverage is now part of the GHC compiler!

 Any more info on that deep seq? I can't find it in the libraries that come
 with GHC 6.6.1. It seems to be part of Control.Strategies.DeepSeq of HXT.
 This is a separate download?

 Intuitively, I would say deep seq forces strict evaluation of the complete
 graph of its first argument? Is this correct?

 Peter

 -Original Message-
 From: Don Stewart [mailto:[EMAIL PROTECTED]
 Sent: Tuesday, September 11, 2007 10:11 PM
 To: Peter Verswyvelen
 Cc: Neil Mitchell; Haskell-Cafe
 Subject: Re: [Haskell-cafe] Building production stable software in Haskell

 bf3:
  Well, I actually meant more something like the imperative equivalences
  of code coverage tools and unit testing tools, because I've read
  rumors that in Haskell, unit testing is more difficult because lazy
  evaluation will cause the units that got tested to be evaluated

 We have full control over evaluation though, with bang patterns, seq and
 deep seq.

 Generally unit testing is generalised to property testing with QuickCheck,
 though.

 For code coverage, combined with testing, use HPC, the program coverage tool

 now in GHC head.

 -- Don

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe



-- 
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Re[2]: [Haskell-cafe] Is this haskelly enough?

2007-07-18 Thread J. Garrett Morris

This is probably just me, but I've always mentally separated the list
monad (representing choice) from operations on ordered sets
implemented by lists (which don't always have to represent choice).
In this case, since the remainder of the code wasn't monadic, I find
it much easier to understand what concatMap (or concat . map if you
don't like the merged function) does than what (= tails) would do.

/g

On 7/18/07, Miguel Mitrofanov [EMAIL PROTECTED] wrote:

DFP Yes, but that generality is entirely wasted here and thus an
DFP obscuring element. There is no way that this function can be
DFP generalized to work with other monads.

As for me, concatMap (and concat.map as well) seems much more
obscuring. (=) is so general, that I use it almost everywhere, but
I have to dig into my memory to remember concatMap (or is it
mapConcat?)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Practise fingerspelling with Haskell! (Code cleanup request)

2007-07-18 Thread J. Garrett Morris

On 7/18/07, Dougal Stanton [EMAIL PROTECTED] wrote:

I worked out that [ (a,b) | a - as, b - bs ] must be equivalent to

 comp = concatMap (\x - map ((,) x) ys) xs

but I can't really say how conditions like a /= b get slotted in to
that style. Is there a reference for that?


As I understand it, list comprehensions are equivalent to monadic
expressions in the [] monad.   The only trick is that conditions in
the list comprehension have to be translated into guard expressions.
For instance,


[(x,y) | x - xs, y - ys, x /= y]


translates into:


do x - xs
  y - ys
  guard (x /= y)
  return (x,y)


You're partway there - concatMap is flip (=), so you have the xs =
(\x - stuff) part.

/g

--
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Is this haskelly enough?

2007-07-17 Thread J. Garrett Morris

Hi James.

I would be tempted to write this a little differently than you did.
First, some of the pieces you've written have equivalents in the
standard library; there's no harm in rewriting them, but I figured I'd
point out that they're there.  (Hoogle - haskell.org/hoogle, I believe
- can be a good way to find these.)

Second, I've rewritten it using function composition.  To me, this
makes the combination of different components more obvoius - like the
pipe in Unix.

So, code:

import Data.List

-- I believe this is scheduled for inclusion in the standard library;
-- I find it very useful
f `on` g = \x y - f (g x) (g y)

-- We can find the maximum sublist by comparing the sums
-- of each sublist.
maxsl = maximumBy (compare `on` sum) . sublists
   -- the tails function returns each tail of the given list; the
inits function
   -- is similar.  By mapping inits over tails, we get all the sublists.
   where sublists = filter (not . null) . concatMap inits . tails

That works for your test case; I haven't tried it exhaustively.

/g

On 7/17/07, James Hunt [EMAIL PROTECTED] wrote:

Hi,

As a struggling newbie, I've started to try various exercises in order
to improve. I decided to try the latest Ruby Quiz
(http://www.rubyquiz.com/quiz131.html) in Haskell. Would someone be kind
enough to cast their eye over my code? I get the feeling there's a
better way of doing it!

subarrays :: [a] - [[a]]
subarrays [] = [[]]
subarrays xs = (sa xs) ++ subarrays (tail xs)
 where sa xs = [ys | n - [1..length xs], ys - [(take n xs)]]

maxsubarrays :: [Integer] - [Integer]
maxsubarrays xs = msa [] (subarrays xs)
 where
   msa m [] = m
   msa m (x:xs)
 | sum x  sum m = msa x xs
 | otherwise = msa m xs

--for testing: should return [2, 5, -1, 3]
main = maxsubarrays [-1, 2, 5, -1, 3, -2, 1]

I've read tutorials about the syntax of Haskell, but I can't seem to
find any that teach you how to really think in a Haskell way. Is there
anything (books, online tutorials, exercises) that anyone could recommend?

Thanks,
James
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Is this haskelly enough? -- errm but every answer is wrong(?)

2007-07-17 Thread J. Garrett Morris

On 7/17/07, Anthony Clayden [EMAIL PROTECTED] wrote:

2. The inits . tails approach adds a fault:
   It introduces a sprinkling of empty sub-sequences. These
have sum zero.
   So in case the input list is all negative numbers ...


At least the concatMap inits . tails code that I posted also filtered
empty lists to avoid this problem... it seems like a simple omission
rather than a fault in the approach.

/g

--
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] needsaname :: ([a] - Maybe (b, [a])) - (b - [a]) - [a] - [a]

2007-07-06 Thread J. Garrett Morris

morph :: ([a] - Maybe (b,[a])) - (b - [a]) - [a]  - [a]


Any reason not to call it 'replace'?

/g

On 7/6/07, Jules Bean [EMAIL PROTECTED] wrote:

Hi,

Yet another Function looking for a name post. Here's the type:

  morph :: ([a] - Maybe (b,[a])) - (b - [a]) - [a] - [a]

Here, I am calling ([a] - Maybe (b,[a])) the 'selector'. It is
actually the same type as a simple parser. I am calling (b - [a]) the
'transformer'.

Once you've chosen your selector and transformer, it's a stream
processing function [a] - [a].

The idea is that the 'selector' looks at the front of the stream and
decides if it matches some predicate. (Not necessarily just based on
only the first element, though). If it does not, it will return
Nothing and that element is passed through unchanged. If it matches
then it produces (parses) some representation or calculation 'b'. The
transformer is then responsible to transforming this 'b' back into a
list of as to be spliced back into the stream.

So morph is a general stream manipulation combinator.

Let me give some examples:

* Replace all strings of odd numbers by (just one occurence of) the
   number 0

*Main morph (sAll odd) (const [0]) [1,3,2,3,4,4,3,1,5]
[0,2,0,4,4,0]

* Add one to all even numbers

*Main morph (sAll even) (map (+1)) [1,3,2,3,4,4,3,1,5]
[1,3,3,3,5,5,3,1,5]

Here sAll is the very simply defined:

  sAll p l@(x:xs) | p x   = Just (span p l)
  | otherwise = Nothing


* simple string replacement

*Main morph (sMatch fox) (const ferret) the quick brown fox jumped
over the lazy dog
the quick brown ferret jumped over the lazy dog

* string match + modify

*Main Data.Char morph (sMatch fox) (('++).(++').reverse . map
toUpper) the quick brown fox jumped over the lazy dog
the quick brown 'XOF' jumped over the lazy dog

...in the above two examples, sMatch is defined as:

  sMatch a l | a `isPrefixOf` l = Just (a,drop (length a) l)
 | otherwise= Nothing


I'm fairly sure that the active hive-mind of haskell-cafe will be able
to think of many more uses of this function. I actually wrote it to do
HTML fix-up, working with the TagSoup library. A few quick definitions
and it becomes easy to express things like 'remove all FONT, BR and U
tags; replace all instances of B with SPAN CLASS=important...'; the
task of repairing broken HTML and replacing simplistic markup with
semantic markup. It's very powerful to have the separation between
selection and transformation; it's quite easy to build up powerful
libraries of selectors and use them with simple transformers.

Here is the definition of morph:

  morph sel trans [] = []
  morph sel trans l@(x:xs) = case sel l of Nothing  - x : morph
sel trans xs
Just (b,xs') - trans b ++
morph sel trans xs'


I've written it to run over lists, but it would not be difficult to
make it run over ByteStrings instead, and exploit the 'no-copying'
effect on the bits of the stream which were not modified, which would
be very handy for programs processing large bytestrings.

It could be used for a streaming filter, for example; although care
must be taken. A composition of 'morphs' will 'read-ahead' as far as
its most greedy selector, so if did want it to stream, you'd have to
make sure that your selectors don't read ahead indefinitely (like an
XML selector waiting for a closing tag would).

There is a close cousin which is fully recursive and not guaranteed to
terminate, where you call 'morph' again on the trans b part, which
I've been calling 'rmorph'. You need it if you want to, for example,
remove all correctly nested FONT.../FONT tags. (Although you
wouldn't need it if you forgot about nesting, which is arguably more
sane for that particular case).

Has anyone spotted this function or a close analogue elsewhere in the
haskell canon? Does anyone have a better name for it that 'morph'?

Jules
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Tools for Haskell and COM

2007-06-27 Thread J. Garrett Morris

Microsoft's Component Object Model.

http://en.wikipedia.org/wiki/Component_Object_Model

/g

On 6/27/07, Andrew Coppin [EMAIL PROTECTED] wrote:

Simon Peyton-Jones wrote:

 The biggest Haskell/COM project I know of was Krasimir's
 implementation of Visual Haskell, which was a plug-in for Visual
 Studio. The VS interfaces are COM ones, so he had to do lots of COM
 stuff. He may be able to help you. Sigbjorn Finne knows a lot about
 HDirect, but he's really busy with his day job at Galois!

 There's a bit of a chicken-and-egg problem here; the COM tools are not
 well maintained, so that discourages use, which in turn makes it less
 rewarding to work on them. What I don't know is the level of
 suppressed demand: if there were good tools, would lots of people
 start using them?


 Simon


Is now a bad time to say what is COM??

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Reinvention

2007-06-27 Thread J. Garrett Morris

More generally, that's unfoldr:

Prelude :t Data.List.unfoldr
Data.List.unfoldr :: (b - Maybe (a, b)) - b - [a]

unfoldr represents the end of the unfold explicitly (using Nothing)
instead of implicitly (using the empty list).

/g

On 27 Jun 2007 20:26:56 +0100, Jon Fairbairn [EMAIL PROTECTED] wrote:

Andrew Coppin [EMAIL PROTECTED] writes:

 I seem to be forever writing code that looks like this:

 decode :: String - (SKI,String)
 decode (c:cs) = case c of
   'S' - (S,cs)
   'K' - (K,cs)
   'I' - (I,cs)
   '*' - let (e0,cs0) = decode cs; (e1,cs1) = decode cs1 in (e0 :@: e1, cs1)

This looks like parsing to me.

--
Jón Fairbairn [EMAIL PROTECTED]

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Re[2]: [Haskell-cafe] Re: Keys and Maps [Was: Re: I just don't get it (data structures and OO)]

2007-06-08 Thread J. Garrett Morris

On 6/8/07, Bulat Ziganshin [EMAIL PROTECTED] wrote:

 I second that. I particularly like the elimination of ]'s. We certainly
 need some symbol to separate the map and the key; but we do really need
 to also mark here be the end of the key?

and how (arr ! key ++ data) should be parsed? :)



Roughly the same way you'd parse 2 * 3 + 4.

/g

--
The man who'd introduced them didn't much like either of them, though
he acted as if he did, anxious as he was to preserve good relations at
all times. One never knew, after all, now did one now did one now did
one.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: nested maybes

2007-02-06 Thread J. Garrett Morris

On 2/6/07, Yitzchak Gale [EMAIL PROTECTED] wrote:

J. Garrett Morris wrote:
Well, no, but it is at least no worse than

apply :: Handle - Attribute a -
  ContT (StateT Blargh (ErrorT Fzzt IO)) a

I find that in general, many functions do
not need all of the capabilities. If they do,
you can alias that also:


Well, in this case, the function looked more like:

apply :: Handle - Attribute a - S a

Part of the point here was that S was an abstraction.  Most functions
weren't accessing the state or the continuations directly - and their
interaction with the error type had an intermediary as well.  Instead,
they were using operations that, in turn, used the underlying pieces.


You often need to make changes to the monad stack -
add or remove capabilities, reorder, etc. This way,
you only change the type in one place, and only
fix functions that use the particular capabilities that
were changed.


This is the same with my newtype-deriving alias.


The usual advantages of polymorphism apply -
gives separation of concerns, encourages reuse,
eases maintenance and testing, better expresses
the meaning of the function by not mentioning
unneeded details.


Well, we accomplished this all by having an abstraction barrier
between a set of basic operations (which knew, at some level, about
the internals of S and had their own sets of unit tests) and things
built on top of S (which hypothetically could have gotten to its
internals, but didn't.  It would have been better practice to not
export the instances, but I didn't think of that at the time_.


By the way, are you really doing CPS? If you are
only using ContT to get short-circuiting, you could
probably also simplify things by using ExitT
instead:


We had a threading system which scheduled application threads to a
limited number of IO threads based on data-driven changing priorities.
This was first designed for GHC 6.2.2, when the threaded runtime
wasn't in the shipping versions yet.

I really think we're just talking about two approaches to the same
thing.  I prefer to encapsulate most of the MonadX operations as soon
as possible behind a domain-specific layer, and then write the rest of
my code in terms of that.  In that case, I get isolation of concerns
and testing and such from the fact that the internals of the monad
stack aren't exposed, and if they need to be changed it only affects
the DSL components, not the majority of the code.

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: nested maybes

2007-02-05 Thread J. Garrett Morris

On 2/5/07, Yitzchak Gale [EMAIL PROTECTED] wrote:

J. Garrett Morris wrote:
 First, we'll create a transformed version of the IO monad,

Why go to the trouble of creating a new monad?
The existing ones are fine.


Mainly to keep the type error messages simpler.  A project I was
working on started with

type S = StateT Blargh (ErrorT Fizzt IO)

which was fine and dandy, although it produced somewhat verbose error
messages.  But then we added ContT to the stack, and the end result
was that error messages tended to take more time giving the
transformers than the errors.  On the other hand, using a newtype the
error messages were much easier to read.

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: nested maybes

2007-02-05 Thread J. Garrett Morris

On 2/5/07, Yitzchak Gale [EMAIL PROTECTED] wrote:

J. Garrett Morris wrote:
 Mainly to keep the type error messages simpler.

There are two ways to get around that problem:

1. Make your functions polymorphic, using
MonadState, MonadError, etc. Each function
mentions only the capabilities that it needs,
without having the whole monad stack in its type.


Again, from the earlier example, I'm not sure how typing:

apply :: (MonadCont m, MonadState Blargh m, MonadError Fzzt m, MonadIO m) =
 Handle - Attribute a - m a

is simpler than

apply :: Handle - Attribute a - m a

especially when almost every function in the project would have
required the same constraint list.


2. Use a type alias for the monad stack.


At least as of 6.4.2, GHC printed the expanded types, not the aliases,
in error messages.


(There are other big advantages of both of these.)


Those being?

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] nested maybes

2007-02-04 Thread J. Garrett Morris

Maybe has a Monad instance, so you can write this as follows (untested):

exists str wmap = boolFromMaybe exists'
   where exists' =
 do x - Map.lookup (sort str) wmap
find (== str) (snd x)
 boolFromMaybe (Just _) = True
 boolFromMaybe Nothing  = False

/g


On 2/4/07, Martin DeMello [EMAIL PROTECTED] wrote:

I have a Data.Map.Map String - (Layout, [String]) as follows:

type Anagrams = [String]
type Cell = (Layout, Anagrams)
type WordMap = Map.Map String Cell

exists str wmap =
  let a = Map.lookup (sort str) wmap in
  case a of
   Nothing - False
   Just x - case (find (== str) (snd x)) of
  Nothing - False
  _   - True

the existence test looks ugly - any more compact way to write it?

martin
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] nested maybes

2007-02-04 Thread J. Garrett Morris

On 2/4/07, Udo Stenzel [EMAIL PROTECTED] wrote:

J. Garrett Morris wrote:
Small improvement (Data.Maybe is underappreciated):

 exists str wmap = isJust exists'
where exists' =
  do x - Map.lookup (sort str) wmap
 find (== str) (snd x)


This is true.  Some time ago I swore off the use of fromRight and
fromLeft in favor of maybe, and have been forgetting about the other
functions in Data.Maybe ever since.


and maybe another improvement, though this is dependent on your tastes:

 exists s wmap = isJust $ Map.lookup (sort s) wmap = find (== s) . snd


If you're going to write it all on one line, I prefer to keep things
going the same direction:

exists s wmap = isJust $ find (==s) . snd = Map.lookup (sort s) wmap

Normally, from there I would be tempted to look for a points-free
implementation, but in this case I have a strong suspicion that would
simply be unreadable.

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] nested maybes

2007-02-04 Thread J. Garrett Morris

On 2/4/07, Udo Stenzel [EMAIL PROTECTED] wrote:

J. Garrett Morris wrote:
 On 2/4/07, Udo Stenzel [EMAIL PROTECTED] wrote:
Well, depends on whether we are allowed to define new combinators.  I
sometimes use

-- Kleisli composition
infixl 1 @@
(@@) :: Monad m = (a - m b) - (b - m c) - (a - m c)
f @@ g = join . liftM g . f


I was responding to this, but Dons beat me to it.  Personally, I use
this combinator quite a bit.  (As much as I would rather use , the
Kleisli arrow is a bit verbose to use for my taste.)


and the resulting

 exists s = Map.lookup (sort s) @@ find (== s) . snd  isJust

isn't all that bad.  (To be read as: one can get used to it.)  I also
think, (@@) and () belong in the Prelude and () at type ((a-b) -
(b-c) - (b-c)) should be known under a shorter name.  Unfortunately,
everything short but (?) is already taken...


Presumably you mean (a - b) - (b - c) - (a - c)?

I would personally be fine with Arrows being in the prelude (and, for
instance, (.) defined as flip ()).  I'd support your shorter name
idea if I could think of one...

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: nested maybes

2007-02-04 Thread J. Garrett Morris

On 2/4/07, Martin Huschenbett [EMAIL PROTECTED] wrote:

Hi,

I've often got the same pattern with nested Maybes but inside the IO
monad (sure this could be every other monad too). Assuming that I've got
functions:


This is where my favorite part of the mtl steps in: monad transformers.

First, we'll create a transformed version of the IO monad, which
encompasses the idea of failure.  I've made the failures somewhat more
general by allowing String typed error messages, but you can replace
String with whatever type you'd like (including () if you really don't
want any such information).


newtype MyIO a = MyIO { runMyIO :: ErrorT String IO a }
deriving (Functor, Monad, MonadError String)


This uses GHC's newtype deriving mechanism, and thus requires
-fglasgow-exts.  The same effect can be achieved in Haskell 98 by
using a type synonym instead of a newtype.

Then, we need to have your operations produce their results in MyIO a
instead of IO (Maybe a):


getInput :: MyIO Input
processInput :: Input - MyIO Result
printError :: String - MyIO ()
printResult :: Result - MyIO ()


Finally, we can rewrite your main function without the case statements:


main = runErrorT . runMyIO $
(do input - getInput
result - processInput input
printResult result)
`catchError` printError


However, in this case you don't really need do notation at all.  You
have a very nice pipeline of operations, and we can express it that
way:


main' = runErrorT . runMyIO $ (getInput = processInput = printResult)
  `catchError` printError


which should remove the last vestiges of imperative-feeling code.

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] (a - [b]) vs. [a - b]

2007-02-02 Thread J. Garrett Morris

Uh, apologies.  I got confused between reading your post and playing
for a while, and answered the wrong question.

/g

On 2/2/07, J. Garrett Morris [EMAIL PROTECTED] wrote:

On 2/2/07, Chad Scherrer [EMAIL PROTECTED] wrote:
 So in reality, I'm trying to construct something like
 f :: (a - STM b) - STM (a - b)

 I just figured it was a general monadic kind of problem, more simply
 expressed using lists. But the (!!) solution doesn't make sense in
 this context.

Perhaps this will work for you:

f x = join . liftM f

This typechecks, and seems to work as expected, e.g.:

Prelude Control.Concurrent.STM Control.Monad atomically (f readTVar
(newTVar 'a'))
'a'

 /g

--
It is myself I have never met, whose face is pasted on the underside of my mind.




--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Levels of recursion

2007-02-02 Thread J. Garrett Morris

On 1/31/07, Andrew Wagner [EMAIL PROTECTED] wrote:

  So, a couple of questions to ponder about this: Is this unique to
Haskell, or could the same be said about any functional language? How
can we teach this better to newbies? Most of what I see in the
tutorials is Higher order functions accept as parameters and/or
return other functions. Here's some examples: explanation of map,
explanation of foldr. Moving on, ...   But clearly, this is
something important, and I think we can do a better job of teaching
it. Suggestions?


The first time I really thought about this much was after reading
Meijer, Fokkinga and Patterson's Functional Programming with Bananas,
Lenses and Barbed Wire, which makes the comparison between arbitrary
recursion and goto early on, and develops four replacement operators
(cata-, ana-, hylo-, and paramorphisms).  At the time, I was involved
in teaching an introduction FP class in which we frequently discovered
that students would latch onto primitive recursion early in the course
and never stop using it, even as their functions became more
convoluted and using maps and filters correspondingly easier.  My
natural thought was to wonder if the beginning of the course could be
rewritten without primitive recursion, only introducing it later after
the students were already comfortable with the higher-order
combinators.

Sadly, that never went anywhere.  I'd be interested in hearing if
anybody else got farther in attempting that teaching technique.

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How did you stumble on Haskell?

2007-02-02 Thread J. Garrett Morris

On 1/28/07, Alexy Khrabrov [EMAIL PROTECTED] wrote:

How do people stumble on Haskell?


My story isn't as interesting as some of these.  My first quarter in
school, I took a course taught in Scheme.  I expressed some
dissatisfaction with the lack of types (and, in particular, the
collection of bugs that would have been easily caught with one), and
the grad TA pointed me to ML.  Some time later, I was talking to
another prof. in the hall about ML, and an older guy walking by
suggested that I was wasting my time and should go learn Haskell.  At
the time, the most obvious tutorial was the Gentle Introduction, which
left me confused for about a year or so, but eventually (and I have no
memory the trigger), I started writing code in Haskell and was
completely hooked.

(Incidentally, I eventually found out who the older guy was - Doug
McIlroy - and he ended up advising my honors work.  All in all, a very
convenient meeting.)

/g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] It matters how Type Synonyms are defined?

2007-02-02 Thread J. Garrett Morris

Agreed.  I've written quite a bit of code that way myself.  Looking at
Iavor's monadLib, though, raised a question: has there been any
consider of removing the requirement that the newtype be the last
argument?  The classes for state monads, etc. are rather backwards as
it is, since the independent type (and the primary point of the class)
is always written last.  It seems like it should be equally easy to
support something like:

newtype Scopestate a = ScopeState (State (Scope VVar) a)
 deriving (Monad ScopeState, Functor ScopeState, StateM ScopeState
(Scope VVar))

or even, ideally, a mixture of the two.

/g

On 2/2/07, John Meacham [EMAIL PROTECTED] wrote:

On Fri, Feb 02, 2007 at 04:18:19PM -0600, Bryan Burgers wrote:
 Now, I was going to ask something like, How can I define my type
 synonym so I can do this, but I figured out while writing this email
 that if I define ScopeState a different way:
 type ScopeState = State (Scope VVar)

this is off-topic, but this is a perfect example of where newtype
deriving is great.

 newtype ScopeState a = ScopeState (State (Scope VVar) a)
deriving(Monad,Functor,State (Scope VVar))

I just really like this idiom is all. Using it pervasively pays off
greatly.

John

--
John Meacham - ⑆repetae.net⑆john⑈
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] A function for Maybes

2007-01-25 Thread J. Garrett Morris

fmap.  e.g.:

Prelude fmap ('c':) (Just a)
Just ca
Prelude fmap ('c':) Nothing
Nothing
Prelude

/g

On 1/25/07, John Ky [EMAIL PROTECTED] wrote:

Is there a built-in function that already does this?

foo :: (a - b) - Maybe a - Maybe b
foo f m
  | isNothing m = Nothing
  | otherwise = Just (f (fromJust m))

*Main foo (+2) (Just 3)
Just 5
*Main foo (+2) Nothing
Nothing

If so what is it?

If not, what should I call it?

Thanks

-John


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe






--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] trivial function application question

2007-01-04 Thread J. Garrett Morris

On 1/4/07, brad clawsie [EMAIL PROTECTED] wrote:

lets say i have a string

s = abcdefg

now i have two lists of strings, one a list of patterns to match, and
a list of replacement strings:

patterns = [a,b]
replace = [Z,Y]

from which my intent is that a be replaced by Z, b by Y etc

now using the replace function from MissingH.Str (which i know is now
renamed), i wish to apply replace to s using (pattern[0], replace[0]),
(pattern[1], replace[1])...(pattern[N], replace[N]).


You can create the replacing functions using zipWith :: (a - b - c)
- [a] - [b] - [c] (from the Prelude) as follows:

replacers = zipWith patterns replace

You then need to apply these functions to your starting string s.  I
would probably use foldr for that, something like this:

foldr ($) s replacers

Where ($) performs function application.

As Neil points out, if your replacements overlap, this could cause
replacement text to itself be replaced.

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] trivial function application question

2007-01-04 Thread J. Garrett Morris

Oops, I seem not to have proofread my message.

On 1/4/07, J. Garrett Morris [EMAIL PROTECTED] wrote:

On 1/4/07, brad clawsie [EMAIL PROTECTED] wrote:
 s = abcdefg
 patterns = [a,b]
 replacements = [Z,Y]


I changed the name here so as not to conflict with the replace function.

snip


You can create the replacing functions using zipWith :: (a - b - c)
- [a] - [b] - [c] (from the Prelude) as follows:

replacers = zipWith replace patterns replacements


This line was previously incorrect.

/g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] State separation/combination pattern question

2006-12-23 Thread J. Garrett Morris

On 12/22/06, Reto Kramer [EMAIL PROTECTED] wrote:

What I'm really looking for is not so much the chaining of StateT
compositions, but rather the isolation of StateA from StateB while
they both flow from the search loop into the respective library calls
(foo, bar) transparently to the application programmer.  I'm hoping
there's a way to have the loop be in a State monad whose content is
the sum of the two states that are needed for the foo and bar call
made to the stores from inside the loop. The calls sites for foo and
bar should then extract the right component of the global state and
thread only that state through into the modules. Sounds like magic,
but how close can I get?


My first impulse would be to define classes for each type of state and
have a top-level monad which is instances of each of those.  Using
your example: (your code is  quoted, mine  quoted)


-- ghci -fglasgow-exts ...
--
type StateA = [Integer]


At this point, I would add:

 class Monad m = MonadStateA m
 where getA:: m StateA
   modifyA :: (StateA - StateA) - m ()

 putA :: MonadStateA m = StateA - m ()
 putA = modifyA . const


type StateB = [Integer]


And, similarly here:

 class Monad m = MonadStateB m
 where getB:: m StateB
   modifyB :: (StateB - StateB) - m ()

 putB :: MonadStateB m = StateB - m ()
 putB = modifyB . const


data AppStateRec = AppStateRec { a :: StateA, b :: StateB } deriving
Show


These functions change in two ways: first, their type signatures now
use the new classes I defiend above.  Second, by including the modify
functions, I can make the function bodies somewhat shorter.


foo :: MonadState AppStateRec m = m ()
foo = do st - get
 put $ st { a = 1:(a st) }


 foo :: MonadStateA m = m ()
 foo = modifyA (1:)


bar :: MonadState AppStateRec m = m ()
bar = do st - get
 put $ st { b = 2:(b st) }


 bar :: MonadStateB m = m ()
 bar = modifyB (2:)

At this point, you have several options.  If you're willing to allow
undecidable instances, you can write instances like:

 instance MonadState AppStateRec m = MonadStateA m
 where getA = get = return . a
   modifyA f = do st - get
  put (st { a = f (a st) })

 instance MonadState AppStateRec m = MonadStateB m
 where getB = get = return . b
   modifyB f = do st - get
  put (st { b = f (b st) })

And the remainder of your code will run as you wrote it.  An
alternative without using undecidable instances is to write the
instances manually.  However, in that case, I believe you will have to
write your monad as a newtype instead of a type, and then rely on
either GHC's ability to derive instances of MonadState etc. or else
write those instances yourself as well.

Hope that helps.

/g


type Eval a = StateT AppStateRec Identity a

exec :: Eval ()
exec = do foo
  bar
  foo
  foo
  bar

go = runIdentity $ runStateT exec AppStateRec { a = [], b = [] }

Prints: ((),AppStateRec {a = [1,1,1], b = [2,2]})
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] ST and Transformers

2006-12-22 Thread J. Garrett Morris

Hello everyone,

I recently found myself attempting to use ST at the base of a stack of
transformers, ala:

type Test t r = ErrorT String (ST t) r

I imagined, given the type of ST, that I would need a run function
along the lines of:

runTest :: (forall t. Test t r) - Either String r

(which was, in fact, exactly what I wanted).  Unfortunately, even with
that type signature, I can't find a way to convince the compiler that
the state thread is properly universal in the call to runST.  Is it
possible to write this function, or is ST (and other monads that use
its style of threading) not transformable?

Thanks,
/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ST and Transformers

2006-12-22 Thread J. Garrett Morris

Hmm, that was simpler than I had imagined.  Thank you both!

/g

On 12/22/06, Stefan O'Rear [EMAIL PROTECTED] wrote:

On Fri, Dec 22, 2006 at 06:44:54PM +0100, Pepe Iborra wrote:
 Rank-2 types seem to interact badly with (.) and ($), but my type
 theory educated neuron doesn't know why. I think this is folklore
 knowledge?


You say:
 runErrSt = (.) runST runErrorT

runErrorT has type ErrorT e m a - m (Either e a)
runST has type (forall s. ST s a) - a
(.) has type (b - c) - (a - b) - (a - c)

since runST is the first argument to (.), we must unify (forall s. ST s a) with 
b

Quoting the GHC Manual:
 There is one place you cannot put a forall: you cannot instantiate
 a type variable with a forall-type. So you cannot make a forall-type
 the argument of a type constructor. So these types are illegal:

I am not aware of why this restriction exists, but it is a consequence of a 
documented
restriction.  (I hear rumors about limited support for left-to-right 
impredicative
instantiation in GHC HEAD, but it's way over my head :( )




--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Shrinking the Prelude: The categorical approach

2006-12-20 Thread J. Garrett Morris

On 12/20/06, Diego Navarro [EMAIL PROTECTED] wrote:

 take map for example, and fmap, I don't think they
 should be named different (fmap is ugly, not
 suggestive, and conceptually the same).
 mplus could be renamed (++) (they are conceptually the
 same

Wouldn't this raise the same problems monad comprehensions raise?
Worse yet, beginners can't start off with lists -- and understand
error messages -- without knowing about monads first!


I may be in the distinct minority here, but I greatly miss monad
comprehensions.  Since the objection to monad comprehensions (as well
as more general types for (++) etc) seems to be difficulty for
beginners, has anyone considered providing either language levels as a
command line switch - ala Dr. Scheme - or specifying the meaning of
comprehensions et. al. in regard to whatever operations are in scope
and then having different versions of the Prelude for educational
purpose?

(As a side note, when I was first learning Haskell and learned about
MonadPlus - with the comment that (++) was the monadic plus operator
for lists, I gleefully assumed that (++) :: MonadPlus m = m a - m a
- m a, and was quite happily surprised that something I'd been using
for a while was actually more general than I'd realized.  When I found
out that it wasn't really, I was quite disappointed.)

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Building the community

2006-12-13 Thread J. Garrett Morris

On 12/13/06, Donald Bruce Stewart [EMAIL PROTECTED] wrote:

*  Give tips on how to answer questions
+ Ok. we can put up an article here. Some suggestions:
- Solutions with unsafePerformIO should be discouraged (moreso 
;)


I'd like to at least suggest a slight qualification here, based on
some personal experience.  A colleague of mine learned Haskell after
having spent a while doing systems programming, and (with the danger
of completely misinterpreting him here), the (perceived) lack of
unsafePerformIO et. al. led him to conclude that Haskell was a good
high-level language, but a certain number of projects would at least
require you to write a fair amount of your code in C.  Once he saw
some code that (safely) used unsafePerformIO - particularly in fps, as
I recall - he changed his mind.  To this day, he hasn't written any
code using unsafe*, but is generally somewhat more interested in
Haskell.

So, I'd suggest that demonstrating both the use of unsafePerformIO
when the question is obviously trending that way and mentioning why it
is a bad idea -- perhaps with an example of something that will
obviously segfault or otherwise break type safety - is sometimes a
better idea than simply not admitting its existence.

Also, I strongly support the necromancy/IO analogies.

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Writing Haskell For Dummies Or At Least For People Who Feel Like Dummies When They See The Word 'Monad'

2006-12-12 Thread J. Garrett Morris

Hello everyone,

On 12/12/06, Ketil Malde [EMAIL PROTECTED] wrote:
snip

Some things took a bit of effort to wrap my head
around, but it generally wasn't too hard to get to a level where I could
write useful programs.

snip

* I'm already productive with what I know, so I don't have the direct
motivation


I think this is the major obstacle.  I've had similar experiences with
programmers of all skill levels.  For one example, when I was TA'ing
an introduction to FP course in college, we regularly had students
who, once they had learned primitive recursion, would write every
assignment (that didn't specifically exclude primitive recursion)
recursively for the remainder of the quarter, no matter how much
easier it would have been in terms of (for example) map and filter.
For another examples, I've spent more time than I want to admit
reinventing (or failing to reinvent) various wheels in Haskell
(including arrows and COM's IUnknown) because I was fairly convinced
that I knew everything I needed to solve whatever problem I was
working on.


From my experience, I'd suggest a couple of things: first, I think

starting from simple ideas -- like primitive recursion -- is deadly.
Instead, I'd rather earlier sections focused on less complex
applications of rich ideas.  For example, in a discussion of rewriting
the aforementioned FP class, I rewrote the first project of the
quarter from relying on primitive recursion over the integers to using
function and Kleisli composition.   I'm not necessarily suggesting
that freshmen will understand Kleisli composition, but in this case
the development of the composition operator was fairly natural for the
problem, and when they came back to Kleisli composition later, they
would have already seen it (as opposed to having seen a less useful
variation).

Second, I think that introducing at least monadic programming as early
as possible is a good idea.  My experience with people who've learned
Haskell for jobs or courses (as opposed to for love of the game, so to
speak) is that monadic programming frequently ends up regarded as a
separate set of mysteries that's perhaps convenient for wizards but
not necessary for normal programmers.  This leads to another boundary
when people who have learned a certain amount of Haskell and even
written large amounts of Haskell look at code from more monad-happy
projects and find it written in what looks like a foreign tongue.

Finally, count me among those who would be happy to contribute as soon
as there's a wiki or similar available!

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Stratified monads

2006-12-11 Thread J. Garrett Morris

On 12/11/06, Mark T.B. Carroll [EMAIL PROTECTED] wrote:

I'm not sure I actually understand them properly yet, but I'm already
curious about if anybody's played with them in Haskell, or how useful
it would be to do so. Any comments?


Haskell implementations of the transformers in Espinosa's paper are
discussed in Mark Jones's 1995 paper Functional Programming with
Overloading and Higher-Order Polymorphism, and are available with the
mtl package that is (I believe) usable with all the major Haskell
compilers and distributed with at least ghc and hugs.  (I've never
used yhc or jhc, so I don't know what libraries they distribute.)

I'm personally fond of framing most non-trivial Haskell problems as
defining domain specific languages; as a result, everything over about
200 lines that I've written in the past 3 years has used the mtl in
some form or fashion.  It's great.

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] modelling problem

2006-12-08 Thread J. Garrett Morris

On 12/8/06, Kurt Schelfthout [EMAIL PROTECTED] wrote:

Hi Haskell'ers,

snip


class Activity a c where
 start   :: c - a - Time  --start of the activity (this isn't
actually dependent on c, I guess)
 end :: c - a - Time  --end of the activity
 delta   :: a - Time - c - c --how the constituent is changed at
the given time

snip

How can I now represent the state of the simulation (i.e. all activites on
all constituents). E.g. a list of activities won't do since the list is
heterogeneous (i.e. [Paint Ball White, Move Ball (2,0)])
I know about existentials, but I'm at a loss at how to implement the
wrapper datatype that is exemplified on
http://www.haskell.org/hawiki/ExistentialTypes since my class has two
parameters, in fact I'm at a loss at how to use existentials here
completely.


An existential type will do roughly what you want.  I prefer GADT syntax here:

data AnyActivity
   where AnyActivity :: Activity a c = a - c - AnyActivity

Then, given an activity/constituent pair like, say, Move (2,0) and
Ball, you can say:

AnyActivity (Move (2,0)) Ball

to package the pair.  The resulting object has type AnyActivity, and
can be stored in a list with other AnyActivities.

You can write variations on your class methods that work on the
packaged datatype:

startAny :: AnyActivity - Time
startAny (AnyActivity a c) = start c a

endAny :: AnyActivity - Time
endAny (AnyActivity a c) = end c a

deltaAny :: AnyActivity - Time - AnyActivity
deltaAny (AnyActivity a c) time = AnyActivity a (delta a time c)


Then, how could I go back from the general lists (or whatever datatype)
of [a]'s and [c]'s, to a list of [([a],c)] of all activities a that are
applicable to a certain constituent c? I can't seem to be able to use the
Typeable class for example, since this can not cast to typeclasses, only
to concrete types (I think...).


I'm not exactly sure what you want here.  Since activities of a
different type can affect the same constituent, it's not possible to
go from a constituent c to a list of activities.  You could, for
example, find all the AnyActivity wrappers that contain a given
constituent, if
you added Typeable and Eq constraints:

-- I didn't actually test this code

data AnyActivity
   where AnyActivity :: (Activity a c, Typeable c, Eq c) = a - c -
AnyActivity

activitiesAffecting :: (Eq c, Typeable c) = c - [AnyActivity] - [AnyActivity]
activitiesAffecting c [] = []
activitiesAffecting c (a@(AnyActivity _ c'):as)
   | c `eq` c' = a : activitiesAffecting c as
   | otherwise = activitiesAffecting c as
   where t `eq` t' | Just t'' - cast t' = t == t''
   | otherwise   = False

Extracting the original a and c used in a wrapper can be done as well,
if you also include a Typeable constraint on a.


More straightforward ways of modelling this problem (avoiding multiple
type class parameters and existentials :) )are also welcome.


Without seeing more of your goals, I'm not sure I can suggest anything
else.  I've found myself writing wrapper code like this before, and it
doesn't have to end up completely confused and unusuable.  On the
other hand, it does feel somewhat inelegant to me - looking at FRP,
for instance, might give you some good ideas for other approaches.

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: How to combine Error and IO monads?

2006-12-07 Thread J. Garrett Morris

On 12/7/06, Cat Dancer [EMAIL PROTECTED] wrote:

On 12/7/06, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:
I'm sure from a single example I could understand what was going on
and elaborate from there.

Let's say I want to get a line from the user, and either return an
integer or an error string using ErrorT.

  import Control.Monad.Error
  import Control.Monad.Trans

  foo :: ??


foo :: ErrorT String IO Int

if you're going to use this very often, you can use a line like

type M = ErrorT String IO

and then foo :: M Int


  foo = do  -- something like this?
a - getLine


Since ErrorT String IO Int is not the same as IO, you can't use IO
operations directly.  In this case, you want:

 a - lift getLine


if length a == 1
  then return 123
  else throwError not a single character


This is all fine


  main = do
r - ?? foo ??


You want:

 r - runErrorT foo

and then this will behave as expected:


print r -- prints Left not a single character or Right 123 ?


Your foo operation is in a monad which wraps IO.  lift allows IO
operations inside that wrapper, and runErrorT removes the wrapper.
The same basic pattern works for other wrappers (like StateT or ListT)
or combinations of wrappers (like StateT Int (ErrorT String IO))

/g

--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Picking out elements of a heterogenous list

2006-12-05 Thread J. Garrett Morris

I generally use the Typeable class for this.  In that example, you'd want:

class Typeable a = Shape_ a

instead of just

class Shape_ a

and then your filter predicate would look like:

isSquare :: Shape - Bool
isSquare (Shape s) = typeOf s == typeOf square
  where square :: Square ; square = undefined

(warning: I didn't try this code.)

That adds a little overhead (particularly in that everything must now
derive Typeable) but is one of the better solutions I've seen.

/g

On 12/5/06, Creighton Hogg [EMAIL PROTECTED] wrote:

Hi Haskell-ers,
So I think I understand the idea of creating a heterogenous list using
typeclasses and existentials, but I don't see how to filter the list
to retrieve elements of the list that are of only one type.

More concretely, taking the example here how could we take a list of shapes
[Shape] and pull out all objects that are Squares?
I don't see an obvious way this makes sense.
Is there a way of doing heterogenous lists that would make this possible?

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe






--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Binary code

2006-11-27 Thread J. Garrett Morris

Hello,

First, and forgive me if I'm making unwarranted assumptions, but
http://haskell.org/haskellwiki/Homework_help might be useful.

Second: div, mod, reverse, and the unfoldr function from Data.List
will do what you want.

/g

On 11/25/06, escafia [EMAIL PROTECTED] wrote:


Hi,

i've one fuction receiving an int . The objective is return the respective
binary code of that int.

For example, if i receive 28 the fuction will return 011100.

My question is that there is any fuction in libraries that do this?
If not, anyone has any suggestion?

Thank you.
--
View this message in context: 
http://www.nabble.com/Binary-code-tf2704645.html#a7541561
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] working with lists of couples

2006-11-18 Thread J. Garrett Morris

On 11/17/06, Henning Thielemann [EMAIL PROTECTED] wrote:

On Fri, 17 Nov 2006, Clara Zamolo wrote:
 buildCouples = snd . foldl op (0,[])
where
op (v,xs) x = (v+x,xs++[(x,v)])



You could make something like this that doesn't have quadratic-type
appends by accumulating functions instead of lists:

Prelude snd (foldl (\(s,f) x - (x+s,f . ((x,s):))) (0,id) [1..6]) []
[(1,0),(2,1),(3,3),(4,6),(5,10),(6,15)]

but this is better:


I suggest using 'scanl' and then 'zip' the result together with the
original list.


Or, equivalently, use mapAccumL from the Data.List library:

Prelude Data.List snd $ mapAccumL (\s x - (s + x,(x,s))) 0 [1..6]
[(1,0),(2,1),(3,3),(4,6),(5,10),(6,15)]

/g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Re: [Haskell-cafe] Automatic fixity allocation for symbolic operators

2006-10-14 Thread J. Garrett Morris

On 10/14/06, Nicolas Frisby [EMAIL PROTECTED] wrote:

Perhaps the editor could assume a default precedence when the
user-defined precedence is not yet available. Preferably, the editor
would also somehow yell at the user to indicate that it is making such
an assumption.


Perhaps it could even assume the fixity that is specified in the
prelude for operators without fixity declarations, thus behaving
exactly like the compiler would:

Any operator lacking a fixity declaration is assumed to be infixl 9 (4.4.2)

I agree that changing the language in such an unintuitive way -
breaking existing code in the process - to suit an editor is
counterproductive and ridiculous.

/g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Mis-understanding something in Haskell interpretation

2006-10-03 Thread J. Garrett Morris

On 10/3/06, Edward Ing [EMAIL PROTECTED] wrote:

The source is below. Side is types as Float. My assumption was that
Haskell would know how to convert the Int to a float and all would be
well. I am I mistaken somewhere? The problem is with the last line.


Yes - Haskell does not automatically promote numeric types.  In this
case, the following code compiles:

vertex :: Vertex - Int - Int - Side - Vertex
vertex (a ,b) currentSide totalSides length  =
  let  angle  = (360 / fromIntegral totalSides) *  fromIntegral
currentSide  in
  ( a  + ( length * (sin  angle)), b + ( (*) (cos angle) length ) )

although I'm not sure it's exactly what you want.

/g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] How to round off frational number?

2006-09-08 Thread J. Garrett Morris

I've always used:

roundn n f = fromIntegral (round (f * 10 ^ n)) / 10 ^ n

I may have missed some bugs or subtleties of floating point numbers, though.

/g

On 9/8/06, Sara Kenedy [EMAIL PROTECTED] wrote:

Hello all,

I try to find some functions in Haskell library to deal with numeric
such that the result in the following format (but I did not find)
For example,
1/3
0.33
1/6
0.17
8/3
2.67
9/3
3.00

It seems a so baby question, but I really did not find the answer
after trying on that for some hours.
Thanks for any help.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
It is myself I have never met, whose face is pasted on the underside of my mind.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] beginner's haskell question

2006-08-08 Thread J. Garrett Morris

The ghc flag -fwarn-incomplete-patterns might be what you're looking for.

/g

On 8/8/06, Jens Theisen [EMAIL PROTECTED] wrote:

Hello,

as a haskell newbie I'm wondering about the following question.

Are there options to popular haskell implementations or other means
(haskell lint?) to check for incomplete patterns at compile time for
some? I can't see a reason why this shouldn't be possible or even a
relatively simple thing to implement.

Cheers,

Jens

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
We have lingered in the chambers of the sea 
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] A program which never crashes (even when a function calls error)

2006-08-01 Thread J. Garrett Morris

On 8/1/06, Stephane Bortzmeyer [EMAIL PROTECTED] wrote:

How to do it in Haskell? How can I call functions like Prelude.head
while being sure my program won't stop, even if I call head on an
empty list (thus calling error)?


Try looking at Control.Exception.  For example:


module Test where



import Control.Exception
import Prelude hiding (catch)



example =
  (do print (head (tail a))
  return ok)
  `catch` (\e - do putStrLn (Caught exception:  ++ show e)
return error)


produces:

*Test z - example
Caught exception: Prelude.head: empty list
*Test z
error

This might be the beginning of what you want.

/g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] putStrLn

2006-06-16 Thread J. Garrett Morris

main n = putStrLn (replicate n '*')

main n = putStrLn (take n (repeat '*'))

main n = sequence (take n (repeat (putStr *)))

(but that doesn't have a final new line.  The more complicated:

main n = sequence (take (n - 1) (repeat (putStr *)))  putStrLn *

should solve that problem.)

/g

On 6/16/06, Jenny678 [EMAIL PROTECTED] wrote:


Hello,

Can somebody help me

I want to work with putStrLn
main n = putStrLn *

for example
How must I define the code that:
main 5
*


Thanks for any help
--
View this message in context: 
http://www.nabble.com/putStrLn-t1799896.html#a4905456
Sent from the Haskell - Haskell-Cafe forum at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
We have lingered in the chambers of the sea 
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Separate a string into a list of strings

2006-06-12 Thread J. Garrett Morris

Off the top of my head:

separate :: String - [String]
separate [] = []
separate s =
 case break (',' ==) s of
   (s,[]) - [s]
   (s,',':s') - s : separate s'
   _ - error how did we get here?

There is at least one cunning rewriting with foldl, I think, but I
think this version is clearer.

/g

On 6/12/06, Sara Kenedy [EMAIL PROTECTED] wrote:

Hi all,

I want to write a function to separate a string into a list of strings
separated by commas.

Example:
separate :: String - [String]

separate Haskell, Haskell, and Haskell = [Haskell, Haskell, and Haskell]

If anyone has some ideas, please share with me. Thanks.

S.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
We have lingered in the chambers of the sea 
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] newbie type signature question

2006-06-10 Thread J. Garrett Morris

On 6/9/06, Brandon Moore [EMAIL PROTECTED] wrote:

data DataType m = forall m' . (Monad m') = DataType (TyEq m m') (Char
- m' ())


It appears that the more intuitive formulation:

data DataType m
  where DataType :: Monad m = (Char - m ()) - DataType m

should work in GHC 6.4

/g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] library sort

2006-02-16 Thread J. Garrett Morris
Data.List contains

sort :: Ord a = [a] - [a]

and

sortBy :: (a - a - Ordering) - [a] - [a]

I believe they're currently implemented using merge sort, at least in GHC.

 /g

On 2/16/06, Radu Grigore [EMAIL PROTECTED] wrote:
 Is there a sort function in the libraries that come with GHC (6.4)? My search 
 at
   http://www.haskell.org/ghc/docs/latest/html/libraries/index.html
 has failed, but I can't believe there is none.

 --
 regards,
   radu
 http://rgrig.blogspot.com/

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe





--
We have lingered in the chambers of the sea 
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] round function

2006-02-12 Thread J. Garrett Morris
I think the function you're looking for is:

myRound n places = round (n / fromIntegral factor) * factor
where factor = 10 ^ (places - 1)

In this case, 10 ^ (places - 1) has integral type (either Int or
Integer).  I need it to be a fractional type to divide n by it, so I
use fromIntegral to convert it.

 /g

On 2/12/06, Chatzianastassiou Achilleas [EMAIL PROTECTED] wrote:
 Thanks for the anwser Sebastian,

 this seems to work, however I am trying to implement something like
 Main myfunction 123.456 2
 120.0


 From: Sebastian Sylvan [EMAIL PROTECTED]
 Reply-To: [EMAIL PROTECTED]
 To: Chatzianastassiou Achilleas [EMAIL PROTECTED]
 CC: haskell-cafe@haskell.org
 Subject: Re: [Haskell-cafe] round function
 Date: Sun, 12 Feb 2006 22:23:17 +0100
 
 On 2/12/06, Chatzianastassiou Achilleas [EMAIL PROTECTED]
 wrote:
   Hi all,
  
   I am trying to implement a function that finds the significant figure of
 a
   number to a specified point i.e. 2.5 3 = 2.556. I have implemented
   something like:
  
   sig :: (RealFrac a, Integral b) = a - Int - a
   sig x y = round y
  
   However this doesn't work. Moreover at the Prelude when i type round
 2.
   2 i get an error.
   Any suggestions?
  
 
 Something like:
 
 myround n s = fromIntegral (round (n * factor)) / factor
  where factor = fromIntegral (10^s)
 
 Basically scaling it to bring the specified number of digits to the
 left of the decimal point, then rounding, then scaling back.
 
 /S
 
 --
 Sebastian Sylvan
 +46(0)736-818655
 UIN: 44640862

 _
 FREE pop-up blocking with the new MSN Toolbar – get it now!
 http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/

 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe



--
We have lingered in the chambers of the sea 
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Matching constructors

2006-02-10 Thread J. Garrett Morris
tootieIndices = findIndices isTootie
where isTootie (Pa _) = False
  isTootie (Tootie _) = True

would be my first approach.

 /g

On 2/10/06, Creighton Hogg [EMAIL PROTECTED] wrote:
 Hi,
 If I have something like
 data Patootie = Pa Int | Tootie Int
 and I want to pull out the indices of all elements of a list
 that have type constructor Tootie, how would I do that?

 I thought I might be able to use findIndices, but I don't
 know how to express the predicate.
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe



--
We have lingered in the chambers of the sea 
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] does haskell have plist's ?

2006-02-04 Thread J. Garrett Morris
On 2/4/06, raptor [EMAIL PROTECTED] wrote:
 does Haskell have a property lists. Like Lisp ?
 any pointer to examples ?

Not built in to the language.  It's not hard to get the same
functionality though - I've attached a module that takes a (not
tremendously elegant) approach to the same thing, though.  You'll have
to store PLists explicitly, though, and this requires GHC.

 /g

--
We have lingered in the chambers of the sea 
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
{-# OPTIONS_GHC -fglasgow-exts #-}
module PList (Property, lookup, cons, delete) where

import Data.Typeable

import Prelude hiding (lookup)

class Typeable t = Property a t | a - t
where label :: a - String
  value :: a - t

data AnyProperty 
where AnyProperty :: Property a t = a - AnyProperty

instance Property (String, Int) Int
where label = fst
  value = snd

instance Property (String, String) String
where label = fst
  value = snd

app :: (forall a t. Property a t = a - r) - AnyProperty - r
f `app` (AnyProperty p) = f p

type PList = [AnyProperty]

lookup :: Typeable a = String - PList - Maybe a
lookup prop pl | [anyProp] - property = (cast . value) `app` anyProp
   | otherwise = Nothing
where property = filter ((prop ==) . (label `app`)) pl

cons :: Property a t = a - PList - PList
cons = (:) . AnyProperty

delete :: String - PList - PList
delete prop pl = filter ((prop /=) . (label `app`)) pl___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Statements spanning multiple lines?

2005-12-22 Thread J. Garrett Morris
On 12/22/05, Daniel Carrera [EMAIL PROTECTED] wrote:
 Hi all,

 How do I write a statement that spans multiple lines?

 I have this function:

 pythagoras n = [(a,b,c) | a -[1..n], b -[1..n], c -[1..n], a = b, b
  c, a*a + b*b == c*c]

 This should all be in one line. I know some ways to make the line
 shorter, like defining local functions and using 'filter'. But the code
 would be clearer if I could just make this one statement span several lines.

Indent the second line:

pythagoras n = [(a,b,c) | a -[1..n], b -[1..n], c -[1..n], a = b, b
   c, a*a + b*b == c*c]

 /g

--
We have lingered in the chambers of the sea 
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] FAQ: Why no interactive definitions?

2005-12-22 Thread J. Garrett Morris
In ghci at least, you can enter definitions like that using let binding:

let mysquare x = x * x

 /g

On 12/22/05, Greg Woodhouse [EMAIL PROTECTED] wrote:
 In neither GHCi nor Hugs (so far as I know) is it possible to
 interactively enter definitions. coming from Scheme, this was a bit of
 a surprise, as I'm used to being able to enter, say

 (define mysquare
 (lambda (x)
   (* x x)))

 Is this just a matter of the feature not being implemented, or is there
 a deeper reason for this?

 ===
 Gregory Woodhouse  [EMAIL PROTECTED]
 Interaction is the mind-body problem of computing.
 --Philip L. Wadler
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe



--
We have lingered in the chambers of the sea 
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Verbosity of imperative code (was: Learning Haskell)

2005-12-08 Thread J. Garrett Morris
On 12/8/05, Robin Green [EMAIL PROTECTED] wrote:
 Sure, I take your point. But I just jumped on my latest hobby-horse: verbosity
 of imperative code is not that necessary.

There was a discussion along these lines some time ago, started by
Frederik Eaton with the subject line Mixing monadic and non-monadic
functions.

Personally, I think that automatic lifting is unnecessary. 
Multi-parameter type classes for the numeric prelude, combined with
occasional usage of LiftMn, should handle almost all the lifting
without needing to extend the language (further).

 /g

--
We have lingered in the chambers of the sea 
By sea-girls wreathed with seaweed red and brown
Till human voices wake us, and we drown.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Haskell and OS X

2005-10-21 Thread J. Garrett Morris
On 10/21/05, Joel Reymont [EMAIL PROTECTED] wrote:

 On Oct 21, 2005, at 4:22 PM, Stefan Monnier wrote:

  You mean you'd like
 
   data TableInfo = TableInfo {
  avgPot :: Double,

 No, I would actually like to offset avgPot 4 spaces from TableInfo.

Can I throw a vote in for handling

 data T = T { granularity :: (Int, Int, Int, Int)
, items :: Map (Int, Int, Int, Int) [Item] }

correctly?  That (and case statements) are the only things that really
still bother me about haskell-mode.

 /g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] map in IO

2005-09-25 Thread J. Garrett Morris
the similar function:

mapM :: Monad m = (a - m b) - [a] - m [b]

should do the trick for you, and is in the prelude.

 /g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Basic type classing question.

2005-09-20 Thread J. Garrett Morris
On 9/20/05, Karl Grapone [EMAIL PROTECTED] wrote:
 What I want to be able to do is add and remove fields while the system
 is running, I suppose via hs-plugins, and I should be prevented from,
 for example, accidentally taking an employees first name and using it
 as a departments address.

I know the HList paper - http://homepages.cwi.nl/~ralf/HList/ -
demonstrates type-safe extensible records.  However, the type classery
behind it may not be totally obvious at first depending on how new to
Haskell you are.

 /g
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: OCaml list sees abysmal Language Shootoutresults

2004-10-13 Thread J. Garrett Morris
--- Ketil Malde wrote:
Or, what if String were one?  Could we have painless read/show with
arrays of Char, as well as lists, for instance?
--- end of quote ---

I think with a decent set of type classes for collections, better handling of strings 
would come for free.  If any list function could be made implementation independent, 
then strings could still be treated as lists of chars, but the underlying 
implementation could be completely different (arrays, PackedStrings, etc.)  /gXm
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Language extension idea (was Re: [Haskell-cafe] Re: OCaml list sees...)

2004-10-10 Thread J. Garrett Morris
--- Malcolm Wallace wrote:
As an example, instead of the following list-only code,

f :: List a - ...
f []= ...
f (h:t) = ...

you could write this more general version, which assumes only some
class Sequence with operations null, head, tail, etc.

f :: Sequence s = s a - ...
f list | null list   = ...
   | h - head list, t - tail list  = ...
--- end of quote ---

I guess that's about half way there.  I (and, I think, the original poster) was 
thinking more along the lines of generalizing the existing interface and considering 
lazy lists to be one implementation.  It seems like the list syntax is general 
enough to apply to any ordered collection.  Further, that would only require minimal 
changes in large amounts of existing code that relies on, for example, Strings being 
[Char]s were Strings changed to a more efficient representation.  /gXm
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: Language extension idea (was Re: [Haskell-cafe] Re: OCaml list sees...)

2004-10-09 Thread J. Garrett Morris
--- Tom Pledger wrote:
It could get more convenient, though, if we have data-constructors-as-class-members 
and move the familiar list constructors into the List class. (The following might need 
to be built into the compiler, because of the special [] syntax.)
--- end of quote ---

Changing the subject very slightly, this brings up two questions:

1)  Wouldn't implementing the Views proposal allow this?

and,

2)  Why haven't views been implemented (and why are they a never in the HaskellTwo 
discussion in HaWiki)?

   /gXm
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] The State Monad

2004-10-07 Thread J. Garrett Morris
--- John Goerzen wrote:
tick :: Int - State Int Int
tick newval = do put newval
 return newval

Or this:

tick :: State Int Int
tick = do n - get
  return n

That is even more incomprehensible to me -- why would removing a line
before the return cause a type error?
--- end of quote ---

I can run both of those without a problem - could you post more of your code?  It's 
probably somewhere else.  /gXm
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Parsec Problem

2004-07-27 Thread J. Garrett Morris
--- [EMAIL PROTECTED] wrote:
I wish I knew what that meant.  If someone could explain it and tell me
what's wrong, I'd appreciate it.
--- end of quote ---

Lexeme is actually a selector function over the TokenParser record.  In the previous 
section (also on lexical analysis), he included the code:

module Main where

import Parsec
import qualified ParsecToken as P
import ParsecLanguage( haskellStyle )

lexer :: TokenParser ()
lexer = makeTokenParser
  (haskellDef
  { reservedOpNames = [*,/,+,-]
  })

whiteSpace= P.whiteSpace lexer
lexeme = P.lexeme lexer   -- -- here's lexeme
symbol = P.symbol lexer
natural = P.natural lexer
parens = P.parens lexer
semi = P.semi lexer
identifier= P.identifier lexer
reserved = P.reserved lexer
reservedOp= P.reservedOp lexer

Once you've done that, the sample code should work fine./gXm
___
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe