lomeo: (лямбда)
[personal profile] lomeo
Клёвая реализация сабжа от [livejournal.com profile] antilamer:
data Tree a = Nil | Branch (Tree a) a (Tree a)

bfs t = [x | Branch _ x _ <- b]
  where
    b = t:[x | Branch l _ r <- b, x <- [l,r]]

Ленивость, все дела.

отсюда.

Date: 2011-12-21 10:36 pm (UTC)
From: [identity profile] thesz.livejournal.com
Эта штука будет работать даже на бесконечном списке бесконечных списков. ;)

Date: 2011-12-21 10:38 pm (UTC)
From: [identity profile] lomeo.livejournal.com
Это-то понятно, для этого омега и нужна. Но бесконечный список бесконечных списков - это всего два уровня вложенности, а у дерева их может быть больше. Я не вижу как использовать тут омегу.

В общем, если я чего не вижу - ткни носом.

Date: 2011-12-21 10:59 pm (UTC)
From: [identity profile] thesz.livejournal.com
elemTree :: Eq a => a -> Tree a -> Bool
elemTree e tree = or $ map (==e) $ runOmega $ flatten tree

flatten Nil = fail ""
flatten (Branch l a r) = return a `Monad.mplus` flatten l `Monad.mplus` flatten r
Действительно, совсем переставлять порядок в flatten (BRanch ...) не получается. flatten l `mplus` return a `mplus` flatten r не работает.

Date: 2011-12-21 11:19 pm (UTC)
From: [identity profile] lomeo.livejournal.com
Здорово, спасибо!

Date: 2011-12-21 11:20 pm (UTC)
From: [identity profile] lomeo.livejournal.com
Кстати, раз уж mplus, то вместо fail "" лучше mzero использовать.

Date: 2011-12-21 11:28 pm (UTC)
From: [identity profile] thesz.livejournal.com
Да я уж пива выпил. ;)

Haskell is the language of choice for drunken programmers! ;)

Profile

lomeo: (Default)
Dmitry Antonyuk

April 2024

S M T W T F S
 123456
7891011 1213
14151617181920
21222324252627
282930    

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 15th, 2025 07:32 am
Powered by Dreamwidth Studios