arrays - Haskell - Error map function returning a list -


what want apply stoline function on each element (string) of list passed stomap..

type of stoline : stoline :: string -> [obj].

here code :

stomap :: [[string]] -> [obj] stomap [[val]] = stoline val stomap (val:vals) = map (\a -> stoline a) val ++ ... 

example of data passed stomap : [["0","133","2"],["6","0","0"],["656","0","3"]].


error : couldn't match expected type obj actual type [obj] in return type of call of stoline.
problem map function returns list (and shouldn't!), don't know how avoid problem..

your first case unnecessary, , there's error in second case:

map (\a -> stoline a) val ++ ... 

val here [string], map (\a -> stoline a) val [[obj]]. since you're using ++ combine rest of results, ultimate result of type [[obj]], not [obj].

let's rework it:

stomap :: [[string]] -> [obj] stomap lolos = concatmap (\los -> concatmap (\s -> stoline s) los) lolos 

lolos list of lists of strings, los list of strings, , s string.

we use concatmap here, rather normal map, since want generate single list of objects [obj] not nested list of lists of lists of objects: [[[obj]]], , since we're running stoline on each string in input, need collapse structure traverse lolos.

we can clean above code (make more haskelly) making point-free. \s -> stoline s same stoline, have:

stomap lolos = concatmap (\los -> concatmap stoline los) lolos 

\los -> concatmap stoline los same concatmap stoline, reduce to:

stomap lolos = concatmap (concatmap stoline) lolos 

and can drop argument both sides, giving us:

stomap = concatmap (concatmap stoline) 

it may seem illegible @ first, experienced haskeller, can read more original code - it's clear me last version combines results of running stoline on list of lists.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -