Say I have things like: data LongDec = LongDef a b c ... x y z values = [ 'a', 'b', 'c', ... 'x', 'y', 'z' ] Now I want them to be "LongDef 'a' 'b' 'c' ... 'x' 'y' 'z'". In form, this is something like folding. But since the type changes, so code like following won't work: foldl (\def value -> def value) LongDef values Is it possible to do this in some way? -- 竹密岂妨流水过 山高哪阻野云飞 And for G+, please use magiclouds#gmail.com.
Forgot to mention, solution without TemplateHaskell. On Tue, Dec 25, 2012 at 4:59 PM, Magicloud Magiclouds < magicloud.magiclouds@gmail.com> wrote:
Say I have things like:
data LongDec = LongDef a b c ... x y z values = [ 'a', 'b', 'c', ... 'x', 'y', 'z' ]
Now I want them to be "LongDef 'a' 'b' 'c' ... 'x' 'y' 'z'". In form, this is something like folding. But since the type changes, so code like following won't work:
foldl (\def value -> def value) LongDef values
Is it possible to do this in some way? -- 竹密岂妨流水过 山高哪阻野云飞
And for G+, please use magiclouds#gmail.com.
-- 竹密岂妨流水过 山高哪阻野云飞 And for G+, please use magiclouds#gmail.com.
Try folding over data type constructor with $? вторник, 25 декабря 2012 г. пользователь Magicloud Magiclouds писал:
Forgot to mention, solution without TemplateHaskell.
On Tue, Dec 25, 2012 at 4:59 PM, Magicloud Magiclouds < magicloud.magiclouds@gmail.com <javascript:_e({}, 'cvml', 'magicloud.magiclouds@gmail.com');>> wrote:
Say I have things like:
data LongDec = LongDef a b c ... x y z values = [ 'a', 'b', 'c', ... 'x', 'y', 'z' ]
Now I want them to be "LongDef 'a' 'b' 'c' ... 'x' 'y' 'z'". In form, this is something like folding. But since the type changes, so code like following won't work:
foldl (\def value -> def value) LongDef values
Is it possible to do this in some way? -- 竹密岂妨流水过 山高哪阻野云飞
And for G+, please use magiclouds#gmail.com.
-- 竹密岂妨流水过 山高哪阻野云飞
And for G+, please use magiclouds#gmail.com.
-- Best Timur DeTeam Amirov Moscow, Russia
Thinking from subway (: foldl ($) LongDef values ? вторник, 25 декабря 2012 г. пользователь Тимур Амиров писал:
Try folding over data type constructor with $?
вторник, 25 декабря 2012 г. пользователь Magicloud Magiclouds писал:
Forgot to mention, solution without TemplateHaskell.
On Tue, Dec 25, 2012 at 4:59 PM, Magicloud Magiclouds < magicloud.magiclouds@gmail.com> wrote:
Say I have things like:
data LongDec = LongDef a b c ... x y z values = [ 'a', 'b', 'c', ... 'x', 'y', 'z' ]
Now I want them to be "LongDef 'a' 'b' 'c' ... 'x' 'y' 'z'". In form, this is something like folding. But since the type changes, so code like following won't work:
foldl (\def value -> def value) LongDef values
Is it possible to do this in some way? -- 竹密岂妨流水过 山高哪阻野云飞
And for G+, please use magiclouds#gmail.com.
-- 竹密岂妨流水过 山高哪阻野云飞
And for G+, please use magiclouds#gmail.com.
-- Best Timur DeTeam Amirov Moscow, Russia
-- Best Timur DeTeam Amirov Moscow, Russia
{-# LANGUAGE DeriveDataTypeable, ScopedTypeVariables #-}
Hi MagicCloud, A worse, but perhaps simpler alternative to Oleg's solution uses Data.Dynamic:
import Data.Dynamic
data LongDec a = LongDec a a a a a a a a deriving (Show, Typeable)
values = "abcdefgh"
mkLongDec :: forall a. Typeable a => [a] -> Maybe (LongDec a) mkLongDec = (fromDynamic =<<) . foldl (\f x -> do f' <- f dynApply f' (toDyn x)) (Just (toDyn (\x -> LongDec (x :: a))))
main = do print (mkLongDec values) print (mkLongDec [1 .. 8 :: Integer])
*Main> main Just (LongDec 'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h') Just (LongDec 1 2 3 4 5 6 7 8) There is no check that all arguments of LongDec are the same type (in this case a specific instance of Typeable): you'd only be able to get Nothing out of mkLongDec was defined as: data LongDec a = LongDec a Int a a a Char Regards, Adam Vogt
On 12/25/2012 09:59 AM, Magicloud Magiclouds wrote:
Say I have things like:
data LongDec = LongDef a b c ... x y z values = [ 'a', 'b', 'c', ... 'x', 'y', 'z' ]
Now I want them to be "LongDef 'a' 'b' 'c' ... 'x' 'y' 'z'". In form, this is something like folding. But since the type changes, so code like following won't work:
foldl (\def value -> def value) LongDef values
Is it possible to do this in some way? -- 竹密岂妨流水过 山高哪阻野云飞
And for G+, please use magiclouds#gmail.com <http://gmail.com/>.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
This hack works, in case that helps: {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-} data LongDec = LongDef Char Char Char Char Char Char deriving Show values = [ 'a', 'b', 'c', 'x', 'y', 'z' ] class Apply a b c where apply :: b -> [a] -> c instance Apply a b b where apply = const instance (Apply a b c) => Apply a (a -> b) c where apply f (x:xs) = apply (f x) xs main = print (apply LongDef values :: LongDec) It requires an explicit type annotation to fix type parameter 'c'. It cannot be a function type. (I am not sure why though.)
You guys are great! Thanks. On Wed, Dec 26, 2012 at 9:04 AM, Timon Gehr <timon.gehr@gmx.ch> wrote:
On 12/25/2012 09:59 AM, Magicloud Magiclouds wrote:
Say I have things like:
data LongDec = LongDef a b c ... x y z values = [ 'a', 'b', 'c', ... 'x', 'y', 'z' ]
Now I want them to be "LongDef 'a' 'b' 'c' ... 'x' 'y' 'z'". In form, this is something like folding. But since the type changes, so code like following won't work:
foldl (\def value -> def value) LongDef values
Is it possible to do this in some way? -- 竹密岂妨流水过 山高哪阻野云飞
And for G+, please use magiclouds#gmail.com <http://gmail.com/>.
______________________________**_________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/**mailman/listinfo/haskell-cafe<http://www.haskell.org/mailman/listinfo/haskell-cafe>
This hack works, in case that helps:
{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses #-}
data LongDec = LongDef Char Char Char Char Char Char deriving Show
values = [ 'a', 'b', 'c', 'x', 'y', 'z' ]
class Apply a b c where apply :: b -> [a] -> c instance Apply a b b where apply = const instance (Apply a b c) => Apply a (a -> b) c where apply f (x:xs) = apply (f x) xs
main = print (apply LongDef values :: LongDec)
It requires an explicit type annotation to fix type parameter 'c'. It cannot be a function type. (I am not sure why though.)
______________________________**_________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/**mailman/listinfo/haskell-cafe<http://www.haskell.org/mailman/listinfo/haskell-cafe>
-- 竹密岂妨流水过 山高哪阻野云飞 And for G+, please use magiclouds#gmail.com.
participants (4)
-
adam vogt -
Magicloud Magiclouds -
Timon Gehr -
Тимур Амиров