首页 \ 问答 \ 错误的IO操作顺序使用putStr和getLine(Wrong IO actions order using putStr and getLine)

错误的IO操作顺序使用putStr和getLine(Wrong IO actions order using putStr and getLine)

我有以下代码:

main = do
    putStr "Test input : "
    content <- getLine
    putStrLn content

当我运行它(使用runhaskell )或编译它(ghc 6.10.4)时,结果如下所示:

asd
Test input : asd

为什么Test input : asdasd后打印asd

在使用putStr http://learnyouahaskell.com/上的代码示例中, getLine提供的输出与我的不同。 当我使用putStrLn ,程序按预期工作(打印,然后提示和打印)。

它是ghc一个错误,还是它应该工作的方式?


I have the following code:

main = do
    putStr "Test input : "
    content <- getLine
    putStrLn content

When I run it (with runhaskell) or compile it (ghc 6.10.4) the result is like this:

asd
Test input : asd

Why is Test input : asd being printed after asd?

In the code sample on http://learnyouahaskell.com/, which uses putStr, the getLine's presented output is different than mine. When I use putStrLn the program works as expected (print, then prompt, and print).

Is it a bug in ghc, or it is the way that it should work?


原文:https://stackoverflow.com/questions/2500459
更新时间:2023-06-24 10:06

最满意答案

在已经有很多类似/相关的问题上,这些问题值得一读,因为答案中包含了大量有用的信息和建议,但实质上你需要这样做:

  • 将音频数据转换为FFT所需的格式(例如int - > float,单独的L / R通道)
  • 应用合适的窗口功能 (例如Hann aka Hanning窗口
  • 应用FFT(注意:如果使用典型的复数到复数的FFT,则将输入数组的虚部设置为零)
  • 计算第一个N / 2 FFT输出格的大小( sqrt(re*re + im*im)
  • 可选将幅度转换为dB(log)标度( 20 * log10(magnitude)
  • 绘制N / 2(对数)幅度值

请注意,尽管FFTW是一种非常好且非常快的FFT,但对于初学者来说可能会有些压倒性的影响 - 如果您想将其作为商业产品的一部分包含在内,它也非常昂贵 - 我建议从KissFFT开始。


There are quite a few similar/related questions on SO already which are well worth reading as the answers contain a lot of useful information and advice, but in essence you need to do this:

  • convert audio data to format required by FFT (e.g. int -> float, separate L/R channels)
  • apply suitable window function (e.g. Hann aka Hanning window)
  • apply FFT (NB: if using typical complex-to-complex FFT then set imaginary parts of input array to zero)
  • calculate magnitude of first N/2 FFT output bins (sqrt(re*re + im*im))
  • optionally convert magnitude to dB (log) scale (20 * log10(magnitude))
  • plot N/2 (log) magnitude values

Note that while FFTW is a very good and very fast FFT it may be a little overwhelming for a beginner - it's also very expensive if you want to include it as part of a commercial product - I recommend starting with KissFFT instead.

相关问答

更多
  • 在已经有很多类似/相关的问题上,这些问题值得一读,因为答案中包含了大量有用的信息和建议,但实质上你需要这样做: 将音频数据转换为FFT所需的格式(例如int - > float,单独的L / R通道) 应用合适的窗口功能 (例如Hann aka Hanning窗口 ) 应用FFT(注意:如果使用典型的复数到复数的FFT,则将输入数组的虚部设置为零) 计算第一个N / 2 FFT输出格的大小( sqrt(re*re + im*im) ) 可选将幅度转换为dB(log)标度( 20 * log10(magnit ...
  • 如果你的意思是音频频谱的频谱,你可以采取以下步骤: 将音频数据转换为原始音频格式,如16位PCM(如果使用流,可能在Windows中) 使用像JTransforms这样的快速傅里叶变换库来计算窗口上的频谱。 可视化包含光谱值的数组 If you mean by audio spectrum the frequency spectrum you could take these steps: Convert the audio data to a raw audio format like 16-Bit PC ...
  • 对于单边FT而言,只需取FFT算法输出的前半部分即可。 考虑到你的输入是实值的,另一半(负值频率)是多余的。 1/8秒很长。 请注意,如果我记得正确(音乐不是我的专业),相关频率大约在160-1600赫兹之间。 那些将位于英国“金融时报”的最左侧区域。 您计算的最高频率(在丢弃FFT的右半部分后)是采样频率的一半,即44.1 / 2 kHz。 最低频率和样本之间的距离由变换长度(44.1 kHz /样本数)给出。 For a single-sided FT simply take the first hal ...
  • 如果你想要绘制一些东西,你需要使用一个可以绘制图形或为另一个程序生成数据的库。 有很多选择 - 你可以使用的一些选项: Qt的 SFML SDL gnuplot的 C ++标准库不提供任何开箱即用的图形功能。 I converted the code and successfully plotted it using Gnuplot. The code is given below and was done in Visual Studio 2017 (hence the stdafx.h header f ...
  • 在av_free之后调用av_free是不正确的 。 av_rdft_end已经释放了ctx指向的内存空间。 (此外, av_rdft_init后缺少分号。) 编辑: 第二个问题是operator new[]返回不正确对齐的指针。 请改用av_malloc 。 av_malloc分配一个适合所有内存访问(包括SSE指令)的对齐块。 Calling av_free after av_rdft_end is incorrect. av_rdft_end already frees the memory spac ...
  • NAudio: http ://naudio.codeplex.com/(开源) Bass和Bass.Net:http://www.un4seen.com/(非商业免费) Fmod Ex: http ://www.fmod.org/index.html(也可免费用于非商业用途) 使用Bass做你需要的东西非常简单: string filepath =""; Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero, null); ...
  • 有一篇CodeProject文章展示了如何创建一个可视化工具。 如果您想执行更高级的操作,您可以查看诸如FMOD和BASS等库。 他们都可以用几行代码来显示频谱。 但是,如果您的项目用于商业用途,则应阅读其许可证。 或者,您可以使用开源NAudio库,这也使得显示频谱更加容易。 包含示例代码,例如SpectrumAnalyser类。 There's a CodeProject article that shows how to create a visualizer. If you want to perf ...
  • 问题已在5.2版中修复 The problem has been fixed in version 5.2
  • 感谢Severin的帮助,我最终按照本文中提到的指令将四元数FFT分成两个复杂到复杂的二维FFT,并成功地重现了本文所示的结果。 这样的事情:(请告诉我,如果我错了:)) #include #include using namespace std; using namespace utils; using namespace Array; using namespace fftwpp; void SaliencyMapHandler::quaternionFou ...
  • 使用

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。