<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Le 29/09/2016 à 20:09,
<a class="moz-txt-link-abbreviated" href="mailto:jorgemal1960@gmail.com">jorgemal1960@gmail.com</a> a écrit :<br>
</div>
<blockquote
cite="mid:adbfbf61-76f3-4698-b965-2d1ebec35428@googlegroups.com"
type="cite">
<div dir="ltr">
<div>I have the following function that takes an element and a
list and inserts the element into the list at the first
position where it is less than or equal to the next element.<br>
<div>
<div><br>
</div>
</div>
</div>
</div>
</blockquote>
** This is most probably some pedagogic assignment, and you should
not expect from the community to solve your exercices. <br>
I suspect that you tried to solve the problem without trying to
UNDERSTAND foldr...<br>
But let's see...<br>
<blockquote
cite="mid:adbfbf61-76f3-4698-b965-2d1ebec35428@googlegroups.com"
type="cite">
<div dir="ltr">
<div>
<div>
<div>
<div style="border-width: 1px; border-style: solid;
border-color: rgb(187, 187, 187); background-color:
rgb(250, 250, 250); word-wrap: break-word;"><code>
<div>myInsert :: Ord a => a -> [a] -> [a]</div>
<div>myInsert x [] = [x]</div>
<div>myInsert x (y:ys) = if x < y then x:y:ys else
y:myInsert x ys</div>
</code></div>
<br>
Now, I have to use the above function myInsert and foldr
to implement another function called insertionSort. I have
been able to do it without using foldr as follows and it
works just fine:</div>
</div>
</div>
...<br>
</div>
</blockquote>
OK.<br>
<br>
<blockquote
cite="mid:adbfbf61-76f3-4698-b965-2d1ebec35428@googlegroups.com"
type="cite">
<div dir="ltr">
<div>I have worked for 2 days to use foldr without success, for
example:<br>
</div>
<div><br>
</div>
<div>
<div style="border-width: 1px; border-style: solid;
border-color: rgb(187, 187, 187); background-color: rgb(250,
250, 250); word-wrap: break-word;"><code>
<div>insertionSort :: Ord a => [a] -> [a]</div>
<div>insertionSort [] = []</div>
<div>insertionSort [x] = [x]</div>
<div><span style="font-family: Arial, Helvetica,
sans-serif;">insertionSort (x:xs) = foldr (myInsert) x
(insertionSort xs)</span></div>
</code></div>
<div><br>
</div>
But it does not even compile, I get the following error which
refers to last instruction at position "x" in "<span
style="background-color: rgb(250, 250, 250);">foldr
(myInsert) x (insertionSort xs)":</span></div>
<div><br>
</div>
<div><span style="background-color: rgb(255, 255, 0);">Couldn't
match expected type ‘[a]’ with actual type ‘a’</span></div>
</div>
</blockquote>
<br>
A. You should have learnt that the usage of generic functionals such
as foldr is to avoid explicit recursion. Please, look up some
examples of foldr, for example in <a class="moz-txt-link-freetext" href="https://wiki.haskell.org/Fold">https://wiki.haskell.org/Fold</a>, or
in the Standard Prelude, say:
<a class="moz-txt-link-freetext" href="https://www.haskell.org/onlinereport/standard-prelude.html">https://www.haskell.org/onlinereport/standard-prelude.html</a> (e.g.,
the definition of concat). <br>
<br>
B. So, no need for pattern split x:xs in your main definition,
define just<br>
insertionSort xs = ...<br>
<br>
C. Mind the type of foldr, : (a -> c -> c) -> c -> [a]
-> c . <br>
The type of your function is ok (parentheses redundant), but the
second argument is the initial container (list) while you have
chosen "x", which is an element. Here the typechecker protests, but
this is not your only mistake.<br>
The third element of foldr is the processed container (also list),
just it, no recursion. And your first two clauses of insertionSort
are useless.<br>
<br>
Jerzy Karczmarczuk<br>
</body>
</html>