I would like to insert a int into the correct position in the sorted list. for example if I insert 2 it would in insert in the second position.
insert :: Int -> [Int] -> [Int
insert x [] = [x]
insert x (y:ys) = if xsy
then x:y:ys else y insert x ys
can anyone point what’s wrong.
thanks
Like Daniel Fischer says, you just have a few typos:
xsyshould presumably bex < y.You have a missing operator in
y insert x ys; what operator do you need to prepend a value to a list? (Hint: You use it when pattern-matching the list.)You missed a
]after[Int.Your indentation is wrong;
thenshould be aligned at least as far as theif, and theelseshould be on a new line, aligned withthen. If you’re indenting with tabs, you should set your editor to indent with spaces instead (or, if you must, set it to display tabs as 8 spaces, which is what Haskell expects).Other than that, you’re good to go.