撰写尾部和头部

时间:2015-08-19 01:58:24

标签: elm

问:榆树0.15不会让我(尾巴>>头)xs。如何以优雅的方式解决这个问题?

module Fibonacci where

import List exposing ((::), head, map2, reverse, tail)

fibonacci : Int -> List Int
fibonacci n =
    let fibonacci' n acc =
        if n <= 2
            then acc
            else fibonacci' (n-1) ((head acc + (tail >> head) acc) :: acc)
    in
        fibonacci' n [1,1] |> reverse

自:

head : List a -> Maybe a
tail : List a -> Maybe (List a)

我不知道如何转换Maybe (List a) to be List a

那么,在没有定义新函数的情况下,有没有简单的方法来组合这两个函数?

1 个答案:

答案 0 :(得分:6)

从技术上讲,你想要的是在Maybe monad 中的 Kleisli composition

import List exposing (head, tail)
import Graphics.Element exposing (show)

(>=>) : (a -> Maybe b) -> (b -> Maybe c) -> (a -> Maybe c)
f >=> g = \a -> case f a of Nothing -> Nothing
                            Just b  -> g b

-- or: f >=> g = f >> (\x -> Maybe.andThen x g)

main = show ((tail >=> head) [1,2,3,4,5])

请注意(>=>)的类型与(>>)的类型非常相似,但所有返回类型都包含在Maybe中。您可以通过这种方式链接任意Maybe - 返回函数(例如tail >=> tail >=> tail >=> head)。

您无法将Maybe a安全地转换为a,因为您没有选择,只有在给定Nothing时崩溃。但 可以将Maybe (Maybe a)值展平为Maybe a

flattenMaybe : Maybe (Maybe a) -> Maybe a
flattenMaybe mma = 
  case mma of Just (Just a) -> Just a
              _             -> Nothing