Haskell append to a list conditionally -
i have 2 lists trying fill items. while reading stdin, depending on value of 1 of things read, want append different list. example,
import control.monad(replicatem) main = n <- getline let l1 = [], l2 = [] in replicatem (read n) (getline >>= (\line -> case line of "yes" -> -- line -- , append value of thing l1 "no" -> -- else -- append value l2 putstrln line)) i realise above code has syntax errors , such, can see trying , suggest something.
this answer came with
while @ it, can explain why gives me infinite list:
let g = [] let g = 1:g -- g contains infinite list of 1's this came with:
import control.monad(replicatem) import data.either geteither::[string] -> [either double double] geteither [] = [] geteither (line:rest) = let [n, h] = words line fn = read f :: double e = case heist of "yes" -> left fn "no" -> right fn in e : geteither rest main = n <- getline lines <- replicatem (read n) getline let tup = partitioneithers $ geteither lines :: ([double], [double]) print tup not sure how fmap have been used in instance
here short ghci session may give ideas:
> :m + control.monad data.either > partitioneithers <$> replicatem 3 readln :: io ([int], [bool]) left 5 right true left 7 ([5,7],[true]) the answer second question let recursive; 2 gs in let g = 1:g referring same in-memory object.
Comments
Post a Comment