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: Looking for more information about Pattern Matching
(Sean Perry)
2. Re: Looking for more information about Pattern Matching
(Daniel Fischer)
3. Antw.: Question about CFloat and Float
( [email protected] )
----------------------------------------------------------------------
Message: 1
Date: Thu, 28 Apr 2011 17:57:46 -0700
From: Sean Perry <[email protected]>
Subject: Re: [Haskell-beginners] Looking for more information about
Pattern Matching
To: Graham Hopper <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
On Apr 28, 2011, at 5:35 PM, Graham Hopper wrote:
> Hi,
>
> I am very much a newbie to Haskell and currently working through
> http://learnyouahaskell.com and trying some of the
> http://www.haskell.org/haskellwiki/99_questions. I am struggling to get my
> head around pattern matching. I think I can understand the individual
> patterns presented in LYAH, but can't seem to find any information about
> generalising the formats.
>
> Is there a good resource that describes the patterns that are available or
> provides more details about how to create them.
>
> For example the format of [x] and (x:xs) seem very different and I cant quite
> get a handle on the syntax(?) behind them. I can see how patterns work when
> shown an example, but wouldn't know how to create one from scratch.
>
> I hope you can understand what I am trying to say - its a little difficult to
> explain.
[x] means a list with one element
(x:xs) means give me the head as x and the rest as xs, you only know there is
at least one element.
What is happening is Haskell is using the type constructor to create and
deconstruct the value for you.
Look also at the example of 'tell' in LYAH's Pattern Matching with Lists and
List Comprehensions. He uses (x:[]) as a synonym for [x].
(I put this in sample.hs)
foo [x] = "Just " ++ show x
foo (x:xs) = "Head " ++ show x ++ foo xs
foo [] = ""
bar (Just x) = show x
bar Nothing = "Nothing"
data Example = Ex1 Int | Ex2 String
deriving (Show)
examplePlay (Ex1 n) = "An Int " ++ show n
examplePlay (Ex2 s) = "A String " ++ show s
anotherExample = let e = Ex1 5 in
putStrLn $ examplePlay e
$ ghci
Prelude> :load sample
[1 of 1] Compiling Main ( sample.hs, interpreted )
Ok, modules loaded: Main.
*Main> foo [1..10]
"Head 1Head 2Head 3Head 4Head 5Head 6Head 7Head 8Head 9Just 10"
*Main> foo [1]
"Just 1"
*Main> foo []
""
*Main> bar $ Just 5
"5"
*Main> bar $ Just [1..10]
"[1,2,3,4,5,6,7,8,9,10]"
*Main> bar Nothing
"Nothing"
*Main> examplePlay (Ex1 5)
"An Int 5"
*Main> examplePlay (Ex2 "Foo")
"A String \"Foo\""
*Main> anotherExample
An Int 5
------------------------------
Message: 2
Date: Fri, 29 Apr 2011 03:27:48 +0200
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] Looking for more information about
Pattern Matching
To: [email protected]
Cc: Graham Hopper <[email protected]>
Message-ID: <[email protected]>
Content-Type: Text/Plain; charset="utf-8"
On Friday 29 April 2011 02:35:13, Graham Hopper wrote:
> Hi,
>
> I am very much a newbie to Haskell and currently working through
> http://learnyouahaskell.com and trying some of the
> http://www.haskell.org/haskellwiki/99_questions. I am struggling to get
> my head around pattern matching. I think I can understand the
> individual patterns presented in LYAH, but can't seem to find any
> information about generalising the formats.
>
> Is there a good resource that describes the patterns that are available
> or provides more details about how to create them.
>
> For example the format of [x] and (x:xs) seem very different and I cant
> quite get a handle on the syntax(?) behind them.
The second is an ordinary constructor-application-pattern, the first is not
a true pattern, it's syntax sugar for lists.
Basically, a pattern is
- a wildcard, '_'
- a variable pattern, var
these two match everything, a wildcard does no binding, a variable pattern
binds the matched value to the name and makes it available for use (on the
rhs of the definition, after the '->' in a case expression, ...)
Or
- a constructor application, (Con arg1 arg2), (x:xs), (a,b,c,d), ((:) x xs)
the constructor has to be saturated, so if it takes n arguments to
construct a value of type t, it has to be supplied with n arguments in a
pattern-match. These arguments are themselves patterns (variable patterns
in the example), so patterns can be nested (to arbitrary depth, where
arbitrary means limited by the available memory).
Also there are
- as-patterns, var@(a1:a2:rest), which matches against the detailed pattern
and binds the entire value to var and the components to the corresponding
names in the as-pattern
- lazy patterns, ~(x:xs), which do no matching immediately and only try to
match and bind when a component is demanded (that can lead to run-time
errors if e.g. the value is in fact an empty list).
Then there are some special cases which are not really patterns but quasi-
patterns, supplied for convenience:
- number literals, 0, 1, 1.245, ...
foo 1 = bar
is in fact
foo n | n == 1 = bar
- record patterns, Con{ field = value }, Con{ field1 = value1, field2 =
value2 }
those are shorthand for (Con _ _ value _ _) etc. with the appropriate
number of wildcards and the value-pattern in the correct place
One can use the special form of no supplied values, Con{}, even for
datatypes declared without record-syntax.
- special forms of list patterns, "string", [a,b], which are desugared to
('s':'t':'r':'i':'n':'g':[]) resp. (a:b:[])
- tuple patterns, (a,b), (a,b,c,d,e)
these are strictly ((,) a b), ((,,,,) a b c d e)
> I can see how patterns
> work when shown an example, but wouldn't know how to create one from
> scratch.
>
> I hope you can understand what I am trying to say - its a little
> difficult to explain.
>
>
> Thanks
>
> Graham
------------------------------
Message: 3
Date: Fri, 29 Apr 2011 08:20:21 +0200
From: " [email protected] " <[email protected]>
Subject: [Haskell-beginners] Antw.: Question about CFloat and Float
To: " Alberto Ruiz " <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Hi Alberto,
thanks for the hint and the link. Do you have any information on whether that
will continue to work in the future? I somehow have a bad feeling about it
because the C* versions of the types would then be unnecessary.
Thanks again,
Christian
----- Reply message -----
Von: "Alberto Ruiz" <[email protected]>
An: "C Gosch" <[email protected]>
Cc: <[email protected]>
Betreff: [Haskell-beginners] Question about CFloat and Float
Datum: Do., Apr. 28, 2011 15:27
Hi Christian,
On 04/28/2011 02:27 PM, C Gosch wrote:
> Dear list members,
> I have the following problem: I have wrapped a C library using the FFI
> and c2hs.
> The C functions all take a bunch of pointers to C arrays of floats,
> doubles, or ints as arguments.
> Naturally, the Haskell representation of these is e.g.
> Ptr CFloat
> or, for single values, CFloat.
> My question is: if I have an array, e.g. allocated with mallocArray, to
> Haskell Float values (Ptr Float),
> is the representation of Float exactly the same as for CFloat? Can I
> safely use Ptr Float where the C function
> expects the C type float* ?
> I assume that I can not, and in that case I would like to find a way to
> have Float on the Haskell side and CFloat on the C side.
> Any hint on how I would do that without copying data and without too
> large performance penalties?
>
> Thanks a lot,
> Christian
>
I think that in ghc you can assume the same representation for CFloat
and CDouble, they are newtypes of Float and Double:
http://www.haskell.org/ghc/docs/7.0.3/html/libraries/base-4.3.1.0/Foreign-C-Types.html#g:4
(I directly use arrays of Float and Double with C libraries without any
problem). But Int may be different from CInt depending on the
architecture, so we need explicit conversions.
Alberto
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110429/5cb22741/attachment-0001.htm>
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 34, Issue 41
*****************************************