Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/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: Add parenthesis to Sin/Cos expressions (goforgit .)
2. strict evaluation and MIDI playback (Dennis Raddle)
3. Re: mapKeysMaybe (Dennis Raddle)
4. Re: strict evaluation and MIDI playback (Heinrich Apfelmus)
5. Re: strict evaluation and MIDI playback (Dennis Raddle)
6. Re: strict evaluation and MIDI playback (Dennis Raddle)
----------------------------------------------------------------------
Message: 1
Date: Fri, 6 Nov 2015 21:43:02 +0100
From: "goforgit ." <[email protected]>
To: [email protected], The Haskell-Beginners Mailing
List - Discussion of primarily beginner-level topics related to
Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Add parenthesis to Sin/Cos
expressions
Message-ID:
<cahzzbmcxsj2pxh5tzz8rvmes-ari4ztn1_bdqh3jmj8hgmb...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Thanlks!
On Thu, Nov 5, 2015 at 6:16 PM, Sumit Sahrawat, Maths & Computing, IIT
(BHU) <[email protected]> wrote:
> The spirit of declarative programming lies in the fact that you try to
> just tell the compiler what something means. Instead of thinking about
> keeping track of the numbers of parentheses, and then applying them
> afterwards, I would suggest you create a function to do the same job:
>
> bracketed :: String -> String
> bracketed s = "(" ++ s ++ ")"
>
> This would allow you to remove the numeric argument, and also write
> cleaner code.
>
> The second thing that you can improve is the pattern matching. You can use
> isPrefixOf
> <http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-List.html#v:isPrefixOf>
> to
> check functions, or use a take inside a case statement to get cleaner
> handling of different cases.
>
> case take 3 str of
> "sin" -> ...
> "cos" -> ...
> _ -> ...
>
> This would also let you debug your code more easily, just as you require.
>
> On 4 November 2015 at 14:51, Imants Cekusins <[email protected]> wrote:
>
>> > a function that is supposed to add parenthesis to sin/cos expressions.
>> a) Sin 5 + 3 -> Sin(5) + 3
>> b) Sin Cos (5+3) -> Sin (Cos(5+3))
>> c) Sin Cos 5 * 3 -> Sin(Cos(5)) * 3
>>
>> are you looking for textual representation or actual Haskell code which
>> runs?
>> if actual code, these examples may be re-written like this:
>> a) sin 5 + 3
>> b) sin $ cos $ 5 + 3
>> c) sin (cos 5) * 3
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
>
>
> --
> Regards
>
> Sumit Sahrawat
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20151106/d46d7c7b/attachment-0001.html>
------------------------------
Message: 2
Date: Fri, 6 Nov 2015 13:32:36 -0800
From: Dennis Raddle <[email protected]>
To: Haskell Beginners <[email protected]>
Subject: [Haskell-beginners] strict evaluation and MIDI playback
Message-ID:
<CAKxLvorjq02ZNYGu72_EkduGx1Z0TOABzkL2qqFSF3=axlv...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I wrote a program in Haskell that does real-time MIDI playback. This
involves computing what are called MIDI "events" (things like notes and
volume controls) and then using the PortMidi library to play back the
events. PortMidi also provides a time reading function that is accurate to
1 millisecond which is useful for timing the playback accurately.
I used 'evaluate' in the IO Monad to make sure that all events were fully
computed before playback started. However I was still getting delays
(timing errors). I added some code to the program that wrote the data to
disk before playing it back, and the timing errors went away. Apparently
'evaluate' was not forcing a complete evaluation, while writing a disk file
was. Or at least I think that's what happened.
But writing to disk is a hack. What can I do to get 'evaluate' to work?
A simplified version of the code is here: (the actual program is pretty
long and complex so I posted something which is stripped down but I hope
retains the essential features)
http://lpaste.net/144762
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20151106/7480f597/attachment-0001.html>
------------------------------
Message: 3
Date: Fri, 6 Nov 2015 13:33:11 -0800
From: Dennis Raddle <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] mapKeysMaybe
Message-ID:
<cakxlvor2cnxudx30hvjqn0pwdfdigaksw9eo77eaotufzdu...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Thanks, Lyndon!?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20151106/631ad252/attachment-0001.html>
------------------------------
Message: 4
Date: Fri, 06 Nov 2015 23:22:46 +0100
From: Heinrich Apfelmus <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] strict evaluation and MIDI playback
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
Dear Dennis,
note that the `evaluate` function only evaluates to weak head normal
form (WHNF). A list may be in WHNF even when the head and tail are
unevaluated expressions. What you want is to evaluate the list of notes
to normal form (NF).
The `Control.DeepSeq` module collects functions related to obtaining
normal forms. In your case, changing the line to
midiEvents <- evaluate $ force $ computeMidiEvents music
will probably do the trick, but you may need to implement an instance
NFData for your `MidiEvent` type.
For more details on NF, WHNF and lazy evaluation in general, see also [1].
[1]: https://hackhands.com/lazy-evaluation-works-haskell/
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
Dennis Raddle wrote:
> I wrote a program in Haskell that does real-time MIDI playback. This
> involves computing what are called MIDI "events" (things like notes and
> volume controls) and then using the PortMidi library to play back the
> events. PortMidi also provides a time reading function that is accurate to
> 1 millisecond which is useful for timing the playback accurately.
>
> I used 'evaluate' in the IO Monad to make sure that all events were fully
> computed before playback started. However I was still getting delays
> (timing errors). I added some code to the program that wrote the data to
> disk before playing it back, and the timing errors went away. Apparently
> 'evaluate' was not forcing a complete evaluation, while writing a disk file
> was. Or at least I think that's what happened.
>
> But writing to disk is a hack. What can I do to get 'evaluate' to work?
>
> A simplified version of the code is here: (the actual program is pretty
> long and complex so I posted something which is stripped down but I hope
> retains the essential features)
>
> http://lpaste.net/144762
>
>
------------------------------
Message: 5
Date: Fri, 6 Nov 2015 15:26:14 -0800
From: Dennis Raddle <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] strict evaluation and MIDI playback
Message-ID:
<cakxlvoqswjj_kmaqg6v-0ekj-ict++urwzdpzbmfp3zendh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
?Thanks! I have a problem though. I read about automatic derivation of an
NFData instance. I have GHC version 7.10.2. I tried the code below, but I
get
Can't make a derived instance of Generic RawMidiEvent
You need DeriveGeneric to derive an instance of this class.
In the data declaration for 'RawMidiEvent'
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
import GHC.Generics (Generic)
import Control.DeepSeq
data RawMidiEvent = RawMidiEvent
{ rmeStream :: Int
, rmeChan :: Int
, rmeStatus :: Int
, rmeData1 :: Int
, rmeData2 :: Int
}
deriving (Show,Eq,Generic,NFData)
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20151106/737b9083/attachment-0001.html>
------------------------------
Message: 6
Date: Fri, 6 Nov 2015 15:27:42 -0800
From: Dennis Raddle <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] strict evaluation and MIDI playback
Message-ID:
<cakxlvoqdqke7gjuq3zf9a31wha86b_jcxvqkr5jvo0as3fd...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Never mind, I realized that I use the pragma incorrectly -- it wasn't at
the top of the module.
On Fri, Nov 6, 2015 at 3:26 PM, Dennis Raddle <[email protected]>
wrote:
>
> ?Thanks! I have a problem though. I read about automatic derivation of an
> NFData instance. I have GHC version 7.10.2. I tried the code below, but I
> get
>
> Can't make a derived instance of Generic RawMidiEvent
> You need DeriveGeneric to derive an instance of this class.
> In the data declaration for 'RawMidiEvent'
>
>
> {-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}
>
> import GHC.Generics (Generic)
> import Control.DeepSeq
>
> data RawMidiEvent = RawMidiEvent
> { rmeStream :: Int
> , rmeChan :: Int
> , rmeStatus :: Int
> , rmeData1 :: Int
> , rmeData2 :: Int
> }
> deriving (Show,Eq,Generic,NFData)
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20151106/0da39b15/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 89, Issue 7
****************************************