LINUX.ORG.RU

История изменений

Исправление Waterlaz, (текущая версия) :

В Template Haskell я нуб нубом

$ cat Ololo.hs

{-# LANGUAGE TemplateHaskell #-}

module Ololo where

import Language.Haskell.TH

(++|) (ListE a) (ListE b) = ListE (a++b)

less (LitE (IntegerL a)) (LitE (IntegerL b)) = a < b

sortE (ListE ys) = ListE $ sortL ys
    where sortL (x:xs) = let ps = filter (less x) xs
                             qs = filter (not.(less x)) xs
                          in (sortL ps) ++ [x] ++ (sortL qs)
          sortL _ = []


a = [| [3, 2, 7] |]

b = [| [2, 2, 1] |]

$ cat tst.hs

{-# LANGUAGE TemplateHaskell #-}

import Ololo
import Control.Monad

main =
    print $(liftM sortE $ liftM2 (++|) a b)
$ runhaskell tst.hs

[7,3,2,2,2,1]

Исходная версия Waterlaz, :

В Template Haskell я нуб нубом

$ cat Ololo.hs

{-# LANGUAGE TemplateHaskell #-}

module Ololo where

import Language.Haskell.TH

(++|) (ListE a) (ListE b) = ListE (a++b)

less (LitE (IntegerL a)) (LitE (IntegerL b)) = a < b

sortE (ListE ys) = ListE $ sortL ys
    where sortL (x:xs) = let ps = filter (less x) xs
                             qs = filter (not.(less x)) xs
                          in (sortL ps) ++ [x] ++ (sortL qs)
          sortL _ = []


a = [| [3, 2, 7] |]

b = [| [2, 2, 1] |]

$ cat tst.hs

{-# LANGUAGE TemplateHaskell #-}

import Ololo
import Control.Monad

main =
    print $(liftM sortE $ liftM2 (++|) a b)
$ runhaskell tst.hs [7,3,2,2,2,1]