我最近发现了FP错误(试图学习Haskell),到目前为止,我对我所看到的(一流函数、惰性求值和所有其他好东西)印象深刻。我还不是专家,但我已经开始发现对基本算法进行“功能性”推理比命令式推理更容易(而且我很难回到我必须回到的地方)。

The one area where current FP seems to fall flat, however, is GUI programming. The Haskell approach seems to be to just wrap imperative GUI toolkits (such as GTK+ or wxWidgets) and to use "do" blocks to simulate an imperative style. I haven't used F#, but my understanding is that it does something similar using OOP with .NET classes. Obviously, there's a good reason for this--current GUI programming is all about IO and side effects, so purely functional programming isn't possible with most current frameworks.

My question is, is it possible to have a functional approach to GUI programming? I'm having trouble imagining what this would look like in practice. Does anyone know of any frameworks, experimental or otherwise, that try this sort of thing (or even any frameworks that are designed from the ground up for a functional language)? Or is the solution to just use a hybrid approach, with OOP for the GUI parts and FP for the logic? (I'm just asking out of curiosity--I'd love to think that FP is "the future," but GUI programming seems like a pretty large hole to fill.)


当前回答

自从第一次提出这个问题以来,函数式响应式编程已经被Elm变得更加主流。

我建议你去http://elm-lang.org上看看,那里也有一些非常优秀的交互式教程,教你如何制作一个功能齐全的浏览器内GUI。

它允许您制作功能齐全的GUI,其中您需要自己提供的代码仅由纯函数组成。我个人认为它比各种Haskell GUI框架更容易上手。

其他回答

函数式响应式编程背后的一个开放思想是让事件处理函数同时产生对事件的反应和下一个事件处理函数。因此,一个进化的系统被表示为事件处理函数的序列。

对我来说,学习Yampa成为正确理解函数生成函数的关键。有一些关于扬帕的不错的论文。我推荐The Yampa Arcade:

http://www.cs.nott.ac.uk/~nhn/Talks/HW2003-YampaArcade.pdf(幻灯片,PDF) http://www.cs.nott.ac.uk/~nhn/Publications/hw2003.pdf(全文,PDF)

在Haskell.org上有一个关于Yampa的维基页面

http://www.haskell.org/haskellwiki/Yampa

原Yampa首页:

http://www.haskell.org/yampa(不幸的是目前坏了)

像XUL这样的标记语言允许您以声明式的方式构建GUI。

自从第一次提出这个问题以来,函数式响应式编程已经被Elm变得更加主流。

我建议你去http://elm-lang.org上看看,那里也有一些非常优秀的交互式教程,教你如何制作一个功能齐全的浏览器内GUI。

它允许您制作功能齐全的GUI,其中您需要自己提供的代码仅由纯函数组成。我个人认为它比各种Haskell GUI框架更容易上手。

你可以看看Don Syme在f#上的系列,他演示了如何创建一个gui。下面的链接指向本系列的第三部分(您可以从那里链接到其他两部分)。

使用f#进行WPF开发将是一个非常有趣的GUI范例……

http://channel9.msdn.com/shows/Going+Deep/C9-Lectures-Dr-Don-Syme-Introduction-to-F-3-of-3/

Windows Presentation Foundation证明了函数式方法非常适合GUI编程。它有许多功能方面,“好的”WPF代码(搜索MVVM模式)强调功能方法而不是命令式方法。我可以勇敢地宣称WPF是现实世界中最成功的功能GUI工具包:-)

WPF在XAML中描述了用户界面(尽管你也可以把它重写成函数式的c#或f#),所以要创建一些用户界面,你可以这样写:

<!-- Declarative user interface in WPF and XAML --> 
<Canvas Background="Black">
   <Ellipse x:Name="greenEllipse" Width="75" Height="75" 
      Canvas.Left="0" Canvas.Top="0" Fill="LightGreen" />
</Canvas>

此外,WPF还允许你使用另一组声明性标记来声明性地描述动画和对事件的反应(同样,同样的事情也可以写成c# / f#代码):

<DoubleAnimation
   Storyboard.TargetName="greenEllipse" 
   Storyboard.TargetProperty="(Canvas.Left)"
   From="0.0" To="100.0" Duration="0:0:5" />

事实上,我认为WPF与Haskell的FRP有很多共同之处(尽管我相信WPF的设计者并不知道FRP,这有点不幸——如果你使用函数的观点,WPF有时会感觉有点奇怪和不清楚)。