Возник такой вопрос. Как устроены cons-списки, большей части читающих известно :) А мне сейчас пригодилось бы неизменяемое представление списков в неленивом функциональном языке с хвостовой рекурсией, которое хорошо поддерживает две операции
(
Read more... )
Comments 14
Reply
Reply
Reply
Reply
Reply
{-# LANGUAGE RankNTypes #-}
module Main where
import Prelude hiding(iterate)
type Cat a = forall s. (a -> s -> s) -> s -> s
singleton :: a -> Cat a
catenate :: Cat a -> Cat a -> Cat a
iterate :: (a -> s -> s) -> s -> Cat a -> s
singleton a f s = f a s
catenate a b f s = a f (b f s)
iterate f s a = a f s
main = putStrLn (show test)
where
test = iterate (:) [] list
list = singleton 1 `catenate` singleton 2 `catenate` singleton (3::Int)
Reply
Reply
Reply
Reply
Reply
Reply
Reply
Reply
Leave a comment