Haskell List concatenation Inferred Type -


trying replace element within list @ given point new element return element.

 setelt :: int -> [a] -> -> [a]  setelt x (yf:(y:yl)) z    | x == (length yf) = (yf:(z:yl)) 

results in error:

inferred type not general enough expression    : setelt expected type : int -> [a] -> -> [a] inferred type : int -> [[a]] -> [a] -> [[a]] 

doesn't seem have problem concatenation of yf:y:yl not sure how solve.

you seem misunderstanding list constructor (:) does. haskell list sequence of (:) constructors ending empty list [], , pattern matching disassembles constructors in same order.

so when pattern match on (yf:(y:yl)) you're matching list of @ least 2 elements, yf , y, , yl rest of list.

the inferred type not expect because length yf implies yf--which first element of input list--is list.

what need instead walk down list recursively, building new list using either current element of input or replacement element x when reach right location. general form should standard library function map, implemented this:

map _ [] = [] map f (x:xs) = f x : map f xs 

except you'll need way track index you're searching for, rather transforming every element.

your current function fail if applied list of 0 or 1 elements, fixing should easy after correcting algorithm whole.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -