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.  help with FFI: passing char* to dll function +   pointer
      freeing (Miro Karpis)
   2. Re:  help with FFI: passing char* to dll function + pointer
      freeing (Brandon Allbery)
   3. Re:  question about pattern guards (TP)
   4. Re:  question about pattern guards (Stuart Gale)


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

Message: 1
Date: Sat, 21 Sep 2013 00:53:54 +0200
From: Miro Karpis <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] help with FFI: passing char* to dll
        function +      pointer freeing
Message-ID:
        <cajnnbxgfc7bu0beiw+eywymrlxm0btvaz2dzzt54p7-y79d...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Please, I just can not figure this out:

I have a method in my C dll:
int somemethod(char *param, int length, double array[], int UB1, int UB2,
bool isValid);

I would like to use this method in Haskell, so I have defined it like:


{-# OPTIONS_GHC -fglasgow-exts #-}
import Foreign
import Foreign.C

foreign import stdcall unsafe "somemethod"  c_somemethod :: Ptr CString
                                                         -> Int
                                                         -> Ptr (Double)
                                                         -> Int
                                                         -> Int
                                                         -> Bool
                                                         -> IO Int
main = do
  let param = ["input"]
  let paramLength = length param
      realTable = [ 1.0, 2.0, 3.0 ] :: [Double]
      ub1 = 0
      ub2 = 2
      isValid = False
  realTablePtr <- newArray realTable
  x <- c_somemethod param paramLength realTablePtr ub1 ub2 isValid
  free realTablePtr
  putStrLn $ "c_somemethod output: " ++ show x
  putStrLn "Done"


When I try to compile this I get error regarding the param string. I have
no idea how to define it. Additionally to this, is the freeing of realTablePtr
correct?

compile error:
 Couldn't match expected type `Ptr CString' with actual type `[t0]'
 In the first argument of `c_somemethod', namely `param'

Please, how can I ix this?

cheers,
m.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130921/62665f8e/attachment-0001.html>

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

Message: 2
Date: Fri, 20 Sep 2013 19:06:34 -0400
From: Brandon Allbery <[email protected]>
To: Miro Karpis <[email protected]>,  The Haskell-Beginners
        Mailing List - Discussion of primarily beginner-level topics related
        to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] help with FFI: passing char* to dll
        function + pointer freeing
Message-ID:
        <cakfcl4wbsu3ihcyyvyaviot0rvjowbcfsxv6nvvr6rubtci...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

On Fri, Sep 20, 2013 at 6:53 PM, Miro Karpis <[email protected]>wrote:

> Please, I just can not figure this out:
>
> I have a method in my C dll:
> int somemethod(char *param, int length, double array[], int UB1, int UB2,
> bool isValid);
>
> I would like to use this method in Haskell, so I have defined it like:
>
> foreign import stdcall unsafe "somemethod"  c_somemethod :: Ptr CString
>

You don't want Ptr CString.  See:

    Prelude> :m +Foreign.C.String
    Prelude Foreign.C.String> :i CString
    type CString = GHC.Ptr.Ptr Foreign.C.Types.CChar
  -- Defined in `Foreign.C.String'

In other words, CString is an alias for Ptr CChar. Ptr CString corresponds
to (char **), not (char *).

-- 
brandon s allbery kf8nh                               sine nomine associates
[email protected]                                  [email protected]
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20130920/03ce491d/attachment-0001.html>

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

Message: 3
Date: Sat, 21 Sep 2013 13:08:56 +0200
From: TP <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] question about pattern guards
Message-ID: <[email protected]>
Content-Type: text/plain; charset="ISO-8859-1"

Daniel Trstenjak wrote:

>> Why is it not possible to combine them with a logical OR, instead of the
>> comma that stands for a logical AND?
> 
> The '|' already acts as the "OR", why should you need another one?

Thanks for your answer.
Consider the following example:

------------------
type Foo = Maybe Int

f :: Foo -> Foo -> Int
f x y
  | Just n <- x = 1
  | Just n <- y = 1
  | otherwise = 2

main = do

print $ f (Just 3) Nothing
print $ f Nothing (Just 3)
print $ f (Just 3) (Just 3)
print $ f Nothing Nothing
------------------

It works correctly, but I am compelled to duplicate `1` even if I know that 
the result of `f x y` will be `1` if either `x` or `y` is `Just n`. Here the 
duplication is limited in terms of number of characters, but this may not 
always be so.
So I would like to do:

------------------
type Foo = Maybe Int

f :: Foo -> Foo -> Int
f x y
  | (Just n <- x) || (Just n <- y) = 1
  | otherwise = 2

main = do

print $ f (Just 3) Nothing
print $ f Nothing (Just 3)
print $ f (Just 3) (Just 3)
print $ f Nothing Nothing
------------------

But the `||` yields a parse error, `||` is not supported in pattern guards.
Whereas it is supported in classical guards:

------------------
f :: Int -> Int
f n | n == 1 = 1
    | n < 1 || n > 4 = 2
    | otherwise = 3

main = do

print $ f 1
print $ f 5
print $ f (-1)
print $ f 2
------------------

What am I missing?

Thanks in advance,

TP



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

Message: 4
Date: Sat, 21 Sep 2013 12:35:57 +0100
From: Stuart Gale <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] question about pattern guards
Message-ID: <[email protected]>
Content-Type: text/plain;       charset=us-ascii

I believe the structure you are looking for is called an "Or Pattern", which 
isn't available in Haskell. Or Patterns are available in other FP languages.

One way you could reduce the duplication from the right hand side, for more 
complicated examples, would be to create a separate function and call it 
instead.

Cheers,
Stuart

On 21 Sep 2013, at 12:08, TP <[email protected]> wrote:

> Daniel Trstenjak wrote:
> 
>>> Why is it not possible to combine them with a logical OR, instead of the
>>> comma that stands for a logical AND?
>> 
>> The '|' already acts as the "OR", why should you need another one?
> 
> Thanks for your answer.
> Consider the following example:
> 
> ------------------
> type Foo = Maybe Int
> 
> f :: Foo -> Foo -> Int
> f x y
>  | Just n <- x = 1
>  | Just n <- y = 1
>  | otherwise = 2
> 
> main = do
> 
> print $ f (Just 3) Nothing
> print $ f Nothing (Just 3)
> print $ f (Just 3) (Just 3)
> print $ f Nothing Nothing
> ------------------
> 
> It works correctly, but I am compelled to duplicate `1` even if I know that 
> the result of `f x y` will be `1` if either `x` or `y` is `Just n`. Here the 
> duplication is limited in terms of number of characters, but this may not 
> always be so.
> So I would like to do:
> 
> ------------------
> type Foo = Maybe Int
> 
> f :: Foo -> Foo -> Int
> f x y
>  | (Just n <- x) || (Just n <- y) = 1
>  | otherwise = 2
> 
> main = do
> 
> print $ f (Just 3) Nothing
> print $ f Nothing (Just 3)
> print $ f (Just 3) (Just 3)
> print $ f Nothing Nothing
> ------------------
> 
> But the `||` yields a parse error, `||` is not supported in pattern guards.
> Whereas it is supported in classical guards:
> 
> ------------------
> f :: Int -> Int
> f n | n == 1 = 1
>    | n < 1 || n > 4 = 2
>    | otherwise = 3
> 
> main = do
> 
> print $ f 1
> print $ f 5
> print $ f (-1)
> print $ f 2
> ------------------
> 
> What am I missing?
> 
> Thanks in advance,
> 
> TP
> 
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners


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

Subject: Digest Footer

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


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

End of Beginners Digest, Vol 63, Issue 30
*****************************************

Reply via email to