【问题标题】:if-then statements involving signals涉及信号的 if-then 语句
【发布时间】:2015-09-13 19:26:24
【问题描述】:

我在 Elm 中编写一个涉及信号的简单 if-then 语句时遇到了困难。

如果条件本身是Signal 类型怎么办?我想更改 Elm 网站上的 Mouse Down 示例:

import Graphics.Element exposing (..)
import Mouse

main : Signal Element
main =
  Signal.map show Mouse.isDown

根据鼠标是向上还是向下,它会显示 TrueFalse。如果我想让它说“向上”或“向下”怎么办?我的布尔函数可以说:

<!-- language: haskell -->

f : Bool -> String
f x = 
 if x then "&uarr;" else "&darr;"

但是当我更改主函数时,我得到类型不匹配。

<!-- language: haskell -->

main : Signal Element
main =
  Signal.map show ( f Mouse.isDown)

错误 #1:

The 2nd argument to function `map` has an unexpected type.

10|   Signal.map show ( f Mouse.isDown)
As I infer the type of values flowing through your program, I see a conflict
between these two types:

    Signal a

    String

错误 #2:

The 1st argument to function `f` has an unexpected type.

10|   Signal.map show ( f Mouse.isDown)
As I infer the type of values flowing through your program, I see a conflict
between these two types:

    Bool

    Signal Bool

【问题讨论】:

    标签: if-statement frp elm


    【解决方案1】:

    这与show :: Bool -&gt; Element 基本相同。您没有将信号传递给该函数,而是将信号传递给 map 函数。它与您的f 相同:

    import Mouse
    import Graphics.Element exposing (Element, show)
    
    f : Bool -> String
    f x = if x then "&uarr;" else "&darr;"
    
    updown : Signal String
    updown = Signal.map f Mouse.isDown
    
    main : Signal Element
    main = Signal.map show updown
    

    或者简而言之,作文:main = Signal.map (show &lt;&lt; f) Mouse.isDown

    【讨论】:

      猜你喜欢
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多