涉及信号的 if-then 语句

if-then statements involving signals

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

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

import Graphics.Element exposing (..)
import Mouse

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

它会说 TrueFalse 取决于鼠标是向上还是向下。如果我想让它说 "Up" 或 "Down" 怎么办?我的布尔函数可以说:

<!-- 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

这与 show :: Bool -> Element 基本相同。您不是将 Signal 传递给该函数,而是 map 通过 Signal 传递函数。它与您的 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 << f) Mouse.isDown