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: How to initialize a C struct with FFI (Maciej Piechotka)
   2. Re:  Just clarifying the "pred" and "succ"        functions in
      Haskell (Andy Elvey)
   3. Re:  Re: How to initialize a C struct with FFI (Marco De Oliveira)
   4.  Re: Re: How to initialize a C struct with FFI (Maciej Piechotka)
   5.  Re: Installing hmatrix on OS X (Heinrich Apfelmus)
   6. Re:  define action getInt like getLine ([email protected])
   7.  defining expression (John Moore)


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

Message: 1
Date: Sat, 06 Feb 2010 18:58:10 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: How to initialize a C struct with FFI
To: [email protected]
Message-ID: <1265482690.5402.9.ca...@picard>
Content-Type: text/plain; charset="UTF-8"

On Sat, 2010-02-06 at 12:28 +0100, Marco De Oliveira wrote:
> Hi,
> 
> I try to initialize a C struct using the FFI API and ghc stays blocked
> when I execute my code (and I got sometimes the error "out of
> memory").
> I do not understand, I do not find any mistake in my code:
> 
> {-# OPTIONS -XTypeSynonymInstances #-}
> 
> import Foreign.C.Types
> import Foreign.Marshal.Alloc
> import Foreign.Ptr
> import Foreign.Storable
> 

It can be written as:
import Foreign
import Foreign.C

> 
> type CStruct = (CULong, CULong)
> 

I guess that you may want to use newtype or data rather then type to
avoid clashes (and have just 98 + FFI):

newtype CStruct = CStruct (CULong, CULong)
data CStruct = CStruct CULong CULong

> instance Storable CStruct where
>   sizeOf _ = 2*sizeOf (undefined::CULong)
>   alignment _ = alignment (undefined::CULong)
>   

To begin with - where's peek and poke?

> test = alloca (\pStruct -> do
>          poke pStruct ( (fromIntegral 1)::CULong, (fromIntegral
> 1)::CULong )
>          )

If you just do this at the beginning you may want to use with (notation
as with newtype/data):

with (CStruct (1, 1)) (\ptr -> ...)


Also is main = test?
> Thanks for your help.
> 
> Marco

Regards




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

Message: 2
Date: Sun, 07 Feb 2010 09:45:14 +1300
From: Andy Elvey <[email protected]>
Subject: Re: [Haskell-beginners] Just clarifying the "pred" and "succ"
        functions in Haskell
To: Felipe Lessa <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Felipe Lessa wrote:
 
 (snip )
>
> Although we can't have a pointer to the middle of a list [a], we
> can have another data struture that behaves sort of as if we had
> that pointer: a zipper.  For example,
>
> http://hackage.haskell.org/packages/archive/ListZipper/1.1.1.0/doc/html/Data-List-Zipper.html
>
> To understand a list zipper you could think of it as something like
>
>   struct zipper {
>     list left, right;
>   }
>
> So we track our "position" by tracking what's on the left and
> what's on the right.  By using the API of the link above you
> could have something similar to what you wanted:
>
>   lpred :: Zipper a -> a
>   lpred = focus . left
>
>   lsucc :: Zipper a -> a
>   lsucc = focus . right
>
> --
> Felipe. 
>   
Hi Felipe -
Thanks very much for that!  That's very handy to know.  I think that 
I'll base my lpred and lsucc on this approach. 
Bye for now -
- Andy



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

Message: 3
Date: Sun, 7 Feb 2010 00:04:06 +0100
From: Marco De Oliveira <[email protected]>
Subject: Re: [Haskell-beginners] Re: How to initialize a C struct with
        FFI
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi Maciej,

Thanks to have take some time to read this code.

I rewrite using your comments:

import Foreign
import Foreign.C

data CStruct = CStruct (CULong, CULong)

instance Storable CStruct where
  sizeOf _ = 2*sizeOf (undefined::CULong)
  alignment _ = alignment (undefined::CULong)

test = with (CStruct (1,1)) (\ptr -> return ())

The class Storable does not need to make your own peek and poke method (the
class provide a default implementation).
But with, i have still the behavior when I try this code in ghci.

Please, can you test this code and tell me why ghci stay blocked after
executing the method test.

BR

Marco

2010/2/6 Maciej Piechotka <[email protected]>

> On Sat, 2010-02-06 at 12:28 +0100, Marco De Oliveira wrote:
> > Hi,
> >
> > I try to initialize a C struct using the FFI API and ghc stays blocked
> > when I execute my code (and I got sometimes the error "out of
> > memory").
> > I do not understand, I do not find any mistake in my code:
> >
> > {-# OPTIONS -XTypeSynonymInstances #-}
> >
> > import Foreign.C.Types
> > import Foreign.Marshal.Alloc
> > import Foreign.Ptr
> > import Foreign.Storable
> >
>
> It can be written as:
> import Foreign
> import Foreign.C
>
> >
> > type CStruct = (CULong, CULong)
> >
>
> I guess that you may want to use newtype or data rather then type to
> avoid clashes (and have just 98 + FFI):
>
> newtype CStruct = CStruct (CULong, CULong)
> data CStruct = CStruct CULong CULong
>
> > instance Storable CStruct where
> >   sizeOf _ = 2*sizeOf (undefined::CULong)
> >   alignment _ = alignment (undefined::CULong)
> >
>
> To begin with - where's peek and poke?
>
> > test = alloca (\pStruct -> do
> >          poke pStruct ( (fromIntegral 1)::CULong, (fromIntegral
> > 1)::CULong )
> >          )
>
> If you just do this at the beginning you may want to use with (notation
> as with newtype/data):
>
> with (CStruct (1, 1)) (\ptr -> ...)
>
>
> Also is main = test?
> > Thanks for your help.
> >
> > Marco
>
> Regards
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100206/03455403/attachment-0001.html

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

Message: 4
Date: Sat, 06 Feb 2010 23:44:38 +0000
From: Maciej Piechotka <[email protected]>
Subject: [Haskell-beginners] Re: Re: How to initialize a C struct with
        FFI
To: [email protected]
Message-ID: <1265499878.5402.47.ca...@picard>
Content-Type: text/plain; charset="UTF-8"

On Sun, 2010-02-07 at 00:04 +0100, Marco De Oliveira wrote:
> Hi Maciej,
> 
> Thanks to have take some time to read this code.
> 
> I rewrite using your comments:
> 
> import Foreign
> import Foreign.C
> 
> data CStruct = CStruct (CULong, CULong)
> 

Hmm. You combined both newtype and data. While technically it is correct
you have now one level more for program to consider. 

> instance Storable CStruct where
>   sizeOf _ = 2*sizeOf (undefined::CULong)
>   alignment _ = alignment (undefined::CULong)
>   
> test = with (CStruct (1,1)) (\ptr -> return ())
> 
> The class Storable does not need to make your own peek and poke method
> (the class provide a default implementation).

Not quite. It does in terms of peek/pokeElemOff. Which has in terms of
peek/pokeByteOff which has implementation in terms of peek/poke itself.

Therefore you have infinite recursion - poke calls pokeElemOff which
calls pokeByteOff which calls poke etc. 

Even the documentation specifies this:
"Minimal complete definition: sizeOf, alignment, one of peek,
peekElemOff and peekByteOff, and one of poke, pokeElemOff and
pokeByteOff."[1]

Sorry - I forgot about this (I only noticed that something is missing).

> But with, i have still the behavior when I try this code in ghci.
> 

My other suggestions only caused to avoid extensions/have type safety
and shorten the function respectively - they did not alter behaviour.

Regards

[1]
http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Foreign-Storable.html#t%3AStorable




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

Message: 5
Date: Sun, 07 Feb 2010 11:40:19 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: Installing hmatrix on OS X
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

Duncan Mortimer wrote:
> Ooops.  Cancel that --- I didn't get any errors on running "cabal
> install", and "hmatrix-0.8.2.0" is listed in my ghc-pkg list.
> However, on attempting to run the tests listed on:
> 
> http://sites.google.com/site/wwwhmatrix/installation
> 
> i.e.:  (in ghci):
> 
>> import Numeric.LinearAlgebra.Tests
>> runTests 20
> 
> I get the error:
> 
> ....
> ....
> Loading package hmatrix-0.8.2.0 ... linking ... <interactive>:
> unknown symbol `_zgemm_'
> ghc: unable to load package `hmatrix-0.8.2.0'
> 
> Does anyone have any suggestions as to what's going on now?

Looks like you're missing a function from LAPACK. Did you add

   frameworks: Accelerate

too?

>> Anyway --- the above appears to have worked for me.  I thought I'd
>> send it back to the list in the hopes that it helps someone else.  And
>> if anyone can explain where "extra-include-dirs" should go in the
>> hmatrix.cabal file, please let me know! :)

My bad, the relevant field in the cabal file is called

    include-dirs: /opt/local/include


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



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

Message: 6
Date: Sun, 07 Feb 2010 21:03:00 +0100
From: [email protected]
Subject: Re: [Haskell-beginners] define action getInt like getLine
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

thanks so far.
I used "getInt = fmap read getLine", because I think it's enough for me

I have to do it for a more complex case of reading an own data type 
(myDatatype) with is deriving Show and return an error otherwise.
I tried the following which didn't work:

readMyDatatype :: IO myDatatype
readMyDatatype = do
  if readLn == (show readLn)
  then return readLn
  else do error "input error"


-------- Original-Nachricht --------
> Datum: Tue, 2 Feb 2010 22:39:07 +0100
> Von: Daniel Fischer <[email protected]>
> An: [email protected]
> CC: [email protected]
> Betreff: Re: [Haskell-beginners] define action getInt like getLine

> Am Dienstag 02 Februar 2010 22:20:03 schrieb [email protected]:
> > Hi,
> > how can I write an action
> > getInt :: IO Int
> > that works like
> > getLine :: IO String
> > but for Int instead of String. I know that I can read the input with
> > getLine and then convert it by using read, but don't know how to write
> > it as an action. I tried getInt :: IO Int
> > getInt read <- getLine
> > but that doesn't work.
> 
> There are many possibilities.
> 
> The shortest is
> 
> getInt :: IO Int
> getInt = readLn
> 
> another short and sweet is
> 
> getInt :: IO Int
> getInt = fmap read getLine  -- or liftM read getLine
> 
> But neither of these deals well with malformed input, if that's a 
> possibility to reckon with, use e.g. the reads function
> 
> getInt :: IO Int
> getInt = do
>     inp <- getLine
>     case reads inp of
>       ((a,tl):_) | all isSpace tl -> return a
>       _ -> handle malformed input

-- 
NEU: Mit GMX DSL über 1000,- ¿ sparen!
http://portal.gmx.net/de/go/dsl02


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

Message: 7
Date: Sun, 7 Feb 2010 20:14:17 +0000
From: John Moore <[email protected]>
Subject: [Haskell-beginners] defining expression
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Hi,
   This gives me the explaination that it expecting a Expression but it been
giving a IO b can some explain whats going on please.

import Maybe
data Expression = Val Double
               | Add Expression Expression
               | Subtract Expression Expression
               | Multiply Expression Expression
               | Divide Expression Expression
        | Var String
        | Let String Expression Expression
        deriving Show
demo1 = (Add(Multiply(Divide(Subtract(Val 25)(Val 5))(Val 10))(Val 7))(Val
30))
type Dict =[(String,Expression)]
emptyDict :: Dict
emptyDict = []
addEntry :: String->Expression ->Dict -> Dict
addEntry n e d = (n,e): d
lookupEntry :: String -> Dict -> Maybe Expression
lookupEntry n [] = Nothing
lookupEntry n (x:xs) = if (n == k)
   then (Just v)
                else lookupEntry n xs
   where (k,v) = x
evalStep :: Dict -> Expression ->  Expression
evalStep d(Val x)=  (Val x)

evalStep d(Add x y)
  = case x of
      (Val a) -> case y of
                   (Val b) -> Val (a+b)
                   left -> Add x (evalStep d y)
      right -> Add (evalStep d x)y
evalStep d(Subtract x y)
  = case x of
      (Val a) -> case y of
                   (Val b) -> Val (a-b)
                   left -> Subtract x (evalStep d y)
      right -> Subtract (evalStep d x)y
evalStep d(Multiply x y)
  = case x of
      (Val a) -> case y of
                   (Val b) -> Val (a*b)
                   left -> Multiply x (evalStep d y)
      right -> Multiply (evalStep d x)y
evalStep d (Divide x y)
  = case x of
      (Val a) -> case y of
                   (Val b) -> Val (a/b)
                   left -> Divide x (evalStep d y)
      right -> Divide (evalStep d x)y
evalStep d (Let n e1 e2) = do
       putStrLn ("Adding definition for "++n)
       v <- evalStep d (addEntry n e1 d) e2
       putStrLn ("Removing definition for "++n)
       return v

evaluate :: Dict-> [Expression] -> Expression -> IO ()
evaluate d(x:xs) e = do
     putStrLn (show e)
     putStrLn "Do another step (y/n) or rollback (r)? :"
     c <- getLine
     case c of
       "y" -> let e'= (evalStep d e)in evaluate d (e:x:xs) e'-- build up
history

       "r" ->  case (x:xs) of
  (x:xs)-> evaluate d xs x
  []-> do { putStrLn "Empty"
   ;evaluate d(x:xs) e
  }
       "n"  ->  putStrLn $ "Ok you said no :" ++ c


John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20100207/7a684da5/attachment.html

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

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


End of Beginners Digest, Vol 20, Issue 13
*****************************************

Reply via email to