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: let indenting problems (Heinrich Apfelmus)
2. infix and bind pseudonym (Michael Easter)
3. basic threading (Michael Easter)
4. Re: infix and bind pseudonym (Magnus Therning)
5. Re: infix and bind pseudonym (Daniel Fischer)
6. Re: infix and bind pseudonym (Daniel Fischer)
7. Re: basic threading (Thomas Davie)
8. Re: basic threading (Henk-Jan van Tuyl)
----------------------------------------------------------------------
Message: 1
Date: Tue, 03 Mar 2009 12:20:45 +0100
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: let indenting problems
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
7stud wrote:
> Heinrich Apfelmus <apfelmus <at> quantentunnel.de> writes:
>> How about
>>
>> import Data.Ord (comparing)
>>
>> mySort = sortBy (comparing length)
>>
>
> I just finished chap. 3 of Real World Haskell, which doesn't
> even imports. It took me hours to figure out how to
> access sortBy. The book hasn't introduced "comparing", yet.
It's a handy little function for exactly this sort of thing. It's defined as
comparing p x y = compare (p x) (p y)
See also
http://haskell.org/hoogle/?q=comparing
>> or at least
>>
>> mySort = sortBy myCompare
>> where
>> myCompare x y = compare (length x) (length y)
>>
>
> Very nice. The book mentioned the compare function in
> chap. 2.
>
> I have a question about that code: how come you
> don't have to specify a parameter for mySort, for example:
>
> mySort xs = ...
>
> And doesn't sortBy require two arguments?
>
> sortBy :: (a -> a -> Ordering) -> [a] -> [a]
> (1) (2)
>
> How come you can write it with only one argument?
You can supply arguments one at a time, this is called "currying". In
other words, the expression sortBy myCompare is created by supplying
one argument to sortBy and thus has one argument remaining. More
specifically, it has the type
sortBy myCompare :: [[a]] -> [[a]]
Furthermore, we can simply set
mySort = sortBy myCompare
and the left hand side will be a function with one argument just like
the right hand side is.
> Finally, I'm wondering if anyone can explain why my
> let examples failed?
Your use of let in conjunction with where was not syntactically
correct. While both are used for declaring new things, let is an
expression while where is a declaration, these two don't mix.
In particular, let must always have the form
let
x = ...
y = ...
...
in expr
while where is used like this
foo x y
| ... = expr1
| ... = expr2
where
a = ...
b = ...
...
Construction like
let ... in where ...
let ... in | ...
do not exist, there is no expression after the in . I suggest
consulting a syntax reference for Haskell or using where only for the
time being.
Regards,
apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 2
Date: Wed, 4 Mar 2009 06:25:06 -0600
From: Michael Easter <[email protected]>
Subject: [Haskell-beginners] infix and bind pseudonym
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Folks,
I have a simple type called Beverage and an example that allows me to
construct Maybe Beverage types.
Here are some type signatures for example functions:
request :: String -> Maybe Beverage
addMalt :: Beverage -> Maybe Beverage
I have defined a chain function like so:
chain :: (Maybe a) -> (a -> Maybe b) -> (Maybe b)
chain = (>>=)
I can do this:
(chain (request "beer") addMalt)
and
request "beer" `chain` addMalt
I think I understand why, as I use the back-ticks for infix.
However, I don't have to do that for the true bind function, (>>=)
request "beer" >>= addMalt
I would like to use chain in this way -- that is without back-ticks. I'm
not sure how...
Is there something I'm missing?
thanks
Mike
ps. Thanks to everyone for the great discussion on IO (re: previous
question)
--
----------------------
Michael Easter
http://codetojoy.blogspot.com: Putting the thrill back in blog
http://youtube.com/ocitv -> Fun people doing serious software engineering
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090304/9f5050a2/attachment-0001.htm
------------------------------
Message: 3
Date: Wed, 4 Mar 2009 06:33:37 -0600
From: Michael Easter <[email protected]>
Subject: [Haskell-beginners] basic threading
To: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
hi again everyone,
I am giving a tech talk soon, and aspire to show an example from Chapter 28
of RWH on the STM monad.
My example is just a watered down version of the "transfer wealth" example
in the book. However it is
single threaded.
Q: is there an "easy" example of illustrating 2 threads that do something
(trivial is fine)? I believe that the book constructs a
thread library/manager of sorts but I wonder if there is something easier.
Though I'm not sure how to square this with my example, for now I'd love to
see an example of starting up
2 threads that do something and block until a keystroke is entered in the
terminal.
My apologies if this is too vague or too much "can you do my homework?" !
thanks
Michael
--
----------------------
Michael Easter
http://codetojoy.blogspot.com: Putting the thrill back in blog
http://youtube.com/ocitv -> Fun people doing serious software engineering
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://www.haskell.org/pipermail/beginners/attachments/20090304/3fd091d8/attachment-0001.htm
------------------------------
Message: 4
Date: Wed, 4 Mar 2009 12:59:58 +0000
From: Magnus Therning <[email protected]>
Subject: Re: [Haskell-beginners] infix and bind pseudonym
To: Michael Easter <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=UTF-8
On Wed, Mar 4, 2009 at 12:25 PM, Michael Easter <[email protected]> wrote:
>
> Folks,
>
> I have a simple type called Beverage and an example that allows me to
> construct Maybe Beverage types.
>
> Here are some type signatures for example functions:
>
> request :: String -> Maybe Beverage
>
> addMalt :: Beverage -> Maybe Beverage
>
> I have defined a chain function like so:
>
> chain :: (Maybe a) -> (a -> Maybe b) -> (Maybe b)
> chain = (>>=)
>
> I can do this:
>
> (chain (request "beer") addMalt)
>
> and
>
> request "beer" `chain` addMalt
>
> I think I understand why, as I use the back-ticks for infix.
>
> However, I don't have to do that for the true bind function, (>>=)
>
> request "beer" >>= addMalt
>
> I would like to use chain in this way -- that is without back-ticks. I'm
> not sure how...
>
> Is there something I'm missing?
Yes, there are certain function names that allow infix usage without
the back-ticks, the name 'chain' doesn't. What those function names
are? Roughly you can say that functions that they are functions that
look like binary operations, like + - ++ >>> etc. I'm not sure I read
the pangauage spec correctly, but it looks like operators are made up
of the following characters !...@#$%^&*+-./\|<=>?~ (IIRC ':' has a
special meaning in that it's allowed in "constructors", cf 1:2:[]).
/M
--
Magnus Therning (OpenPGP: 0xAB4DFBA4)
magnusï¼ therningï¼org Jabber: magnusï¼ therningï¼org
http://therning.org/magnus identi.ca|twitter: magthe
------------------------------
Message: 5
Date: Wed, 4 Mar 2009 14:00:21 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] infix and bind pseudonym
To: Michael Easter <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Am Mittwoch, 4. März 2009 13:25 schrieb Michael Easter:
> Folks,
>
> I have a simple type called Beverage and an example that allows me to
> construct Maybe Beverage types.
>
> Here are some type signatures for example functions:
>
> request :: String -> Maybe Beverage
>
> addMalt :: Beverage -> Maybe Beverage
>
> I have defined a chain function like so:
>
> chain :: (Maybe a) -> (a -> Maybe b) -> (Maybe b)
> chain = (>>=)
>
> I can do this:
>
> (chain (request "beer") addMalt)
>
> and
>
> request "beer" `chain` addMalt
>
> I think I understand why, as I use the back-ticks for infix.
>
> However, I don't have to do that for the true bind function, (>>=)
>
> request "beer" >>= addMalt
>
> I would like to use chain in this way -- that is without back-ticks. I'm
> not sure how...
You can't.
Haskell has (infix) operators, whose names are composed of symbols (>, <, |,
:, +, ...) and (prefix) functions, whose names are composed of letters,
underscores (_) and primes (').
If you want to use a function infix or an operator prefix, you must indicate
that to the compiler, which is done by enclosing a function name in backticks
or an operator symbol in parentheses.
If you could use a function name infix or prefix without indicating which you
want, what would
id const even
be? Should it be id `const` even or (id const) even?
>
> Is there something I'm missing?
>
> thanks
> Mike
>
> ps. Thanks to everyone for the great discussion on IO (re: previous
> question)
------------------------------
Message: 6
Date: Wed, 4 Mar 2009 14:20:25 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] infix and bind pseudonym
To: Magnus Therning <[email protected]>, Michael Easter
<[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Am Mittwoch, 4. März 2009 13:59 schrieb Magnus Therning:
> Yes, there are certain function names that allow infix usage without
> the back-ticks, the name 'chain' doesn't. What those function names
> are? Roughly you can say that functions that they are functions that
> look like binary operations, like + - ++ >>> etc. I'm not sure I read
> the pangauage spec correctly, but it looks like operators are made up
> of the following characters !...@#$%^&*+-./\|<=>?~ (IIRC ':' has a
> special meaning in that it's allowed in "constructors", cf 1:2:[]).
':' is the symbol-equivalent of an upper case letter, so it's special only if
it's the first symbol of an operator name, then the operator is a
constructor. It can appear in any place but the first in ordinary operators.
For example:
(:) :: a -> [a] -> [a] -- first symbol is ':' => constructor
(:+) :: (RealFloat a) => a -> a -> Complex a -- constructor
(/:/) :: a -> b -> b -- ':' not first symbol => ordinary operator
>
> /M
------------------------------
Message: 7
Date: Wed, 4 Mar 2009 14:34:47 +0100
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] basic threading
To: Michael Easter <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=WINDOWS-1252; format=flowed;
delsp=yes
On 4 Mar 2009, at 13:33, Michael Easter wrote:
>
> hi again everyone,
>
> I am giving a tech talk soon, and aspire to show an example from
> Chapter 28 of RWH on the STM monad.
>
> My example is just a watered down version of the "transfer wealth"
> example in the book. However it is
> single threaded.
>
> Q: is there an "easy" example of illustrating 2 threads that do
> something (trivial is fine)? I believe that the book constructs a
> thread library/manager of sorts but I wonder if there is something
> easier.
>
> Though I'm not sure how to square this with my example, for now I'd
> love to see an example of starting up
> 2 threads that do something and block until a keystroke is entered
> in the terminal.
>
> My apologies if this is too vague or too much "can you do my
> homework?" !
Certainly, here's a nice one:
map _ [] = []
map f (x:xs) = f x : map f xs
parMap _ [] = []
parMap f (x:xs) = let fx = f x in fx `pseq` fx : map f xs
I realise this isn't what you expected it's not the STM monad but
it *is* basic threading, far more basic, and far nicer than firing up
monads and communication between threads.
Bob
------------------------------
Message: 8
Date: Wed, 04 Mar 2009 14:46:02 +0100
From: "Henk-Jan van Tuyl" <[email protected]>
Subject: Re: [Haskell-beginners] basic threading
To: "Michael Easter" <[email protected]>, [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; format=flowed; delsp=yes;
charset=iso-8859-15
On Wed, 04 Mar 2009 13:33:37 +0100, Michael Easter <[email protected]>
wrote:
> I am giving a tech talk soon, and aspire to show an example from Chapter
> 28
> of RWH on the STM monad.
>
> My example is just a watered down version of the "transfer wealth"
> example
> in the book. However it is
> single threaded.
>
> Q: is there an "easy" example of illustrating 2 threads that do something
> (trivial is fine)? I believe that the book constructs a
> thread library/manager of sorts but I wonder if there is something
> easier.
>
> Though I'm not sure how to square this with my example, for now I'd love
> to
> see an example of starting up
> 2 threads that do something and block until a keystroke is entered in the
> terminal.
>
The page
http://www.haskell.org/haskellwiki/Concurrency_demos
links to several examples of concurrency; maybe you are also interested in
parallelism:
http://groups.google.com/group/fa.haskell/browse_thread/thread/2aa4252637b50e0a
Regards,
Henk-Jan van Tuyl
--
http://functor.bamikanarie.com
http://Van.Tuyl.eu/
--
--
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 9, Issue 6
***************************************