if-then涉及信号的陈述

时间:2015-09-13 19:26:24

标签: if-statement frp elm

我很难在Elm中写一个简单的if-then语句,涉及信号。

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

import Graphics.Element exposing (..)
import Mouse

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

根据鼠标是向上还是向下,它会说 True False 。如果我想要它说“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

1 个答案:

答案 0 :(得分:3)

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