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: Pattern matching with one,   two or more arguments
      (Tim Attwood)
   2. Re:  recommended way to get haskell 6.10 installed        on ubuntu
      hardy? (Joe Van Dyk)
   3.  Tips for a FFI-wrapped C library? (Patrick LeBoutillier)
   4. Re:  Tips for a FFI-wrapped C library? (Stephen Tetley)
   5. Re:  Tips for a FFI-wrapped C library? (Stephen Tetley)
   6.  Typeclass question (Patrick LeBoutillier)
   7. Re:  Typeclass question (Stephen Tetley)
   8.  how to define records containing finite length   lists.
      (Srikanth K)
   9. Re:  how to define records containing finite      length lists.
      (Stephen Tetley)


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

Message: 1
Date: Sun, 29 Nov 2009 13:52:39 -0800
From: "Tim Attwood" <[email protected]>
Subject: [Haskell-beginners] Re: Pattern matching with one,     two or
        more arguments
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
        reply-type=response


"legajid" <[email protected]> wrote in message 
news:[email protected]...
> Hello,
> I'm writing a little word game that consists of the following : between a 
> consonant and a vowel, insert "AV" so "fabien" becomes "fAVabAVien".
>
> When i type : f "fabine", the result is ok ("fAVabAVinAVe")
> When i type : f "fabien", the result is fAVabAVie *** exception :prelude 
> empty list
>
> Here's my program
>
> voyelles="aeiouy"
> consonnes="bcdfghjklmnpqrstvwxz"
>
> est_voyelle x = x `elem` voyelles
> est_consonne x = x `elem` consonnes
>
> --f []=[] ; unuseful - conflict with f a = a
>
> f (a:b:c) =
>    case trait a b of
>        True -> a : (" AV " ++ f (b:c))
>        False -> a : f (b:c)
> f (a:b) =
>    case trait a (head b) of
>        True -> a : (" EP " ++ f ( b) )
>        False -> a : f ( b)
> f a =  a
>
> -- detect a vowel following a consonant
> trait a b = est_consonne a && est_voyelle b
>
>
>
> f (a:b) is never invoked (i wrote EP instead of AV to trace the problem). 
> f(a:b:c) seems to always take precedence over the other patterns.
> I expect f(a:b:c) to treat words greater than or equal to 3 letters, 
> f(a:b) the last two letters of the word (no third part) and f a the last 
> letter.
>
> Another trick : how can i calculate consonnes with a method that makes the 
> difference between  the two lists [a..z] and  voyelles, rather  than 
> writing each consonant.
> As we can append two lists (++), can we compute the difference between 
> lists, i.e. delete from the first list the elements that are present in 
> the second one ?
>
> Last, with Ghci, is it possible to debug my program, especially for 
> recursive functions, because it's hard to follow the successive calls and 
> the parameters that are passed each turn.

import Char

voyelles = "aeiouyAEIOUY"
est_voyelle x = x `elem` voyelles
est_consonne x = (isAlpha x) && (x `notElem` voyelles)

f :: String -> String
f (a:b:c) | trait a b = [a]++"AV"++[b]++(f c)
          | otherwise = a : f (b:c)
f a = a

trait a b = (est_consonne a) && (est_voyelle b) 




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

Message: 2
Date: Sun, 29 Nov 2009 22:39:14 -0800
From: Joe Van Dyk <[email protected]>
Subject: Re: [Haskell-beginners] recommended way to get haskell 6.10
        installed       on ubuntu hardy?
To: Juan Maiz <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

Is it?  Hardy is supposed for 5 years (as it's a LTS release).  Don't
think Jaunty is.

On Mon, Nov 23, 2009 at 3:31 PM, Juan Maiz <[email protected]> wrote:
> But it's worthy to upgrade. :D
>
> On Mon, Nov 23, 2009 at 8:55 PM, Erik de Castro Lopo <[email protected]>
> wrote:
>>
>> Juan Maiz wrote:
>>
>> > In Jaunty it's available.
>>
>> And in Karmic, but the original poster asked about Hardy.
>>
>> Erik
>> --
>> ----------------------------------------------------------------------
>> Erik de Castro Lopo
>> http://www.mega-nerd.com/
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> --
> Juan Maiz Lulkin Flores da Cunha
> ---------------------------------------------------------------------------
> Softa Consultoria para Desenvolvimento
> http://www.softa.com.br
> http://www.mailee.me - Finalmente e-mail marketing 2.0
> http://www.linkedin.com/in/juanmaiz
> http://workingwithrails.com/recommendation/new/person/9354-juan-maiz
> “The most exciting breakthroughs of the 21st century will not occur because
> of technology but because of an expanding concept of what it means to be
> human”
> John Naisbitt
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>



-- 
Joe Van Dyk
http://fixieconsulting.com


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

Message: 3
Date: Mon, 30 Nov 2009 16:34:17 -0500
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] Tips for a FFI-wrapped C library?
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi all,

I have wrapped, using FFI,  a C-library I am writing that calls into
different programming languages. I put
everything in the IO monad. So far it works as expected but I find the
resulting usage code somewhat
ugly (especially the bits musing 'sequence'). I tried using finalizers
to delete the resources but it couldn't
respect my dependencies.

Can anyone offer any advice on making it feel more natural in Haskell?

Note: The FFI code is available here:
http://svn.solucorp.qc.ca/repos/solucorp/mlp/trunk/lib/haskell/


  import qualified MLP
  import qualified MLP.Error
  import qualified MLP.Context
  import qualified MLP.Language
  import qualified MLP.Runtime
  import qualified MLP.Value
  import qualified MLP.Object


  main = do
    ctx <- MLP.Context.new                            -- create a context
    MLP.Context.with ctx $ do
      lang <- MLP.Language.init ctx "perl"            -- init a
language, i.e. perl
      rt <- MLP.Runtime.new ctx lang                   -- create a
runtime for the language
      MLP.Runtime.loadLibrary ctx rt "calc"           -- load a
library, i.e. calc.pm
      val <- MLP.Object.new ctx rt "calc" [] ""       -- create a calc
object, i.e. my $obj = new calc() ;
      obj <- MLP.Value.takeObject val                 -- extract the
object from the value
      MLP.Value.delete ctx val

      args_add <- sequence [MLP.Value.newLong 2, MLP.Value.newLong 3]
      args_mul <- sequence [MLP.Value.newDouble 3.5, MLP.Value.newDouble 4.6]
      val_add <- MLP.Object.callMethod ctx rt obj "add" args_add ""
      sequence $ map (MLP.Value.delete ctx) args_add
      val_mul <- MLP.Object.callMethod ctx rt obj "mul" args_mul ""
      sequence $ map (MLP.Value.delete ctx) args_mul
      MLP.Object.delete ctx obj

      MLP.Value.getLong val_add >>= putStrLn . show >>
MLP.Value.delete ctx val_add
      MLP.Value.getDouble val_mul >>= putStrLn . show >>
MLP.Value.delete ctx val_mul

      MLP.Runtime.delete ctx rt
      MLP.Language.delete ctx lang


Thanks,

Patrick

-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


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

Message: 4
Date: Tue, 1 Dec 2009 09:50:12 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Tips for a FFI-wrapped C library?
To: Patrick LeBoutillier <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Apologies - typo in copy pasted code - the last line should have been
  else funAction thing


withNewThing :: Init_param1 -> Init_param2 -> (Thing -> IO a) -> IO a
withNewThing param1 param2 funAction =
  bracket (newThing param1 param2) destroyThing
          (\thing -> do { check_null <- isNullThing thing
                        ; if check_null
                            then do  -- SOME FAILURE --
                            else funAction thing })



2009/12/1 Stephen Tetley <[email protected]>:


> withNewThing :: Init_param1 -> Init_param2 -> (Thing -> IO a) -> IO a
> withNewThing param1 param2 funAction =
>   bracket (newThing param1 param2) destroyThing
>           (\thing -> do { check_null <- isNullThing thing
>                         ; if check_null
>                             then do  -- SOME FAILURE --
>                             else funAction face })


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

Message: 5
Date: Tue, 1 Dec 2009 09:46:10 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Tips for a FFI-wrapped C library?
To: Patrick LeBoutillier <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Patrick

One thing you can do is use `bracket` to manage the lifetime of
objects created on the C side:


withNewThing :: Init_param1 -> Init_param2 -> (Thing -> IO a) -> IO a
withNewThing param1 param2 funAction =
  bracket (newThing param1 param2) destroyThing
          (\thing -> do { check_null <- isNullThing thing
                        ; if check_null
                            then do  -- SOME FAILURE --
                            else funAction face })

-- newThing destroyThing isNullThing are defined somewhere.

This might be appropriate for the Context, Language etc. objects in
your library.

For the documentation on bracket see Control.Exception.Base.

Best wishes

Stephen


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

Message: 6
Date: Tue, 1 Dec 2009 10:48:50 -0500
From: Patrick LeBoutillier <[email protected]>
Subject: [Haskell-beginners] Typeclass question
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi all,

I want to make all types that are instances of Integral instances of
another type class, i.e:


{-# LANGUAGE FlexibleInstances #-}

class Intable a where
  toInt :: a -> Int

instance (Integral a) => Intable a where
  toInt l = fromIntegral l


However this gives the following error that I don't understand:

q.hs:6:0:
    Constraint is no smaller than the instance head
      in the constraint: Integral a
    (Use -XUndecidableInstances to permit this)
    In the instance declaration for `Intable a'

Can anyone offer insight on thie error? Also, is there perhaps a
different way to do
what I want to do?


Thanks,

Patrick
-- 
=====================
Patrick LeBoutillier
Rosemère, Québec, Canada


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

Message: 7
Date: Tue, 1 Dec 2009 16:40:21 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] Typeclass question
To: Patrick LeBoutillier <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Hi Patrick

As you guessed, adding UndecidableInstances pragma would make it work.

Loosely speaking, the type of your instance 'contains' all the types
that the class can use anyway. Hopefully, someone more learned than me
will give a proper definition and add an explanation of the Paterson
Conditions.

http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#undecidable-instances


But why not just a function:

toInt :: Integral a => a -> Int
toInt = fromIntegral

Of course just a function won't be ideal if you were wanting to have a
special instance say for Integer and all other instances to use the
general version. There's more to say about this in the context of
Generics, but that's a somewhat advanced topic...

Best wishes

Stephen


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

Message: 8
Date: Tue, 1 Dec 2009 22:38:43 +0530
From: Srikanth K <[email protected]>
Subject: [Haskell-beginners] how to define records containing finite
        length  lists.
To: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi,
   I want to define a record say "Student".  The 'C' equvivalent for the
same could be

struct Person {
     char name[10]
     int age }

The closest I can find for doing such a thing in haskell appears to be

data Person = Person{
     name :: [char]
     age :: Int
}

I have not yet been able to find a suitable way to specify the constraint on
the length of list(name in this case).
So can someone let me know how we can impose this length constratint on the
list  and derive a new type.

If standard haskell doesn't give this flexibility, are there any extensions
in ghc to achieve the same.
- Srikanth
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20091201/26f62c2c/attachment-0001.html

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

Message: 9
Date: Tue, 1 Dec 2009 18:26:45 +0000
From: Stephen Tetley <[email protected]>
Subject: Re: [Haskell-beginners] how to define records containing
        finite  length lists.
To: Srikanth K <[email protected]>
Cc: beginners <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Choices, choices...

Firstly, C uses an array not a list so you can too:

> data Person = Person{
>      name :: UArray Int Char,
>      age :: Int
>   }
>   deriving (Show)

Of course there is no bound on the type to be of length 10, the length
would be bound at creation time:

> stephen = Person { name = array (0,9) (zip [0..] "stephen")
>                  , age  = 0
>                  }

Quite horrible - but not significantly worse than C unless you believe
the array size declator is defining the array type rather than just
its storage size. Hmm.

Sized types are a possibility but are quite advanced, certainly they
give more 'static guarantees' than C provides (or just about anything
else except dependent-types...).

One example is here e.g:
http://hackage.haskell.org/package/sized-types

Tuples are another possibility but quite unwieldy: no special syntax,
you would need to write conversion functions:

> type Name = (Char,Char,Char,Char,Char,Char,Char,Char,Char,Char)


You could wrap a String inside a newtype and provide a special constructor,
hiding the Name10 constructor via a module:


> newtype Name10 = Name10 { getName10 :: String }


> makeName10 :: String -> Name10
> makeName10 s | length s > 10 = Name10 $ take 10 s
>              | otherwise     = Name10 s    -- pad if you like here



Essentially it depends what you want to do with the sized string -
generally you will loose string syntax[*] and all the list processing
functions that work on strings as they are just lists of Char. This
might not be a good balance for the safety you want.

Best wishes

Stephen


[*] But you can overload string syntax for nefarious purposes (adding
more complexity):
http://www.haskell.org/ghc/docs/latest/html/users_guide/type-class-extensions.html#overloaded-strings

2009/12/1 Srikanth K <[email protected]>:
> Hi,
>    I want to define a record say "Student".  The 'C' equvivalent for the
> same could be
>
> struct Person {
>      char name[10]
>      int age }
>
> The closest I can find for doing such a thing in haskell appears to be
>
> data Person = Person{
>      name :: [char]
>      age :: Int
> }
>
> I have not yet been able to find a suitable way to specify the constraint on
> the length of list(name in this case).
> So can someone let me know how we can impose this length constratint on the
> list  and derive a new type.
>
> If standard haskell doesn't give this flexibility, are there any extensions
> in ghc to achieve the same.
> - Srikanth
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>


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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 18, Issue 1
****************************************

Reply via email to