Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Iterate in terms of scanl (Atrudyjane)
----------------------------------------------------------------------
Message: 1
Date: Sun, 23 Oct 2016 21:37:14 -0400
From: Atrudyjane <[email protected]>
To: "[email protected]" <[email protected]>
Subject: [Haskell-beginners] Iterate in terms of scanl
Message-ID:
<P9xqsrIFECqSoeHQogF0-YR9yLhG3UBkj5GOSfnkqoDHL8ru1DIDYOJ5Oql_fPFGQf6DRSTCAijpipOKenzF2YMm2CfgUyj03QGkLoGloyc=@protonmail.com>
Content-Type: text/plain; charset="utf-8"
Hello All,
Sorry about my previous post, email was hosed. I was working on a homework
problem where the task is to write the iterate function in terms of scanl. Came
up with this:
myIterate f x = scanl (const.f) x (repeat x)
I went looking around for other solutions to check my work and found the
following solution on the Haskell Wiki:
iterate f x = scanl f x (repeat x)
myIterate seems to work checked against the Prelude iterate but I don't know if
it's a good solution or not, because the iterate solution on the Haskell Wiki
throws a type error. Types of Prelude iterate and scanl are different, so I can
see why, or am I missing something? Here is some REPL output:
Prelude iterate:
λ> take 10 (iterate (+1) 1)
[1,2,3,4,5,6,7,8,9,10]
myIterate:
λ> take 10 (myIterate (+1) 1)
[1,2,3,4,5,6,7,8,9,10]
Haskell Wiki solution:
λ> take 10 (iterate' (+1) 1)
error:
• Occurs check: cannot construct the infinite type: a ~ a -> a
Expected type: a -> a -> a
Actual type: (a -> a) -> a -> a
• In the first argument of ‘iterate'’, namely ‘(+ 1)’
In the second argument of ‘take’, namely ‘(iterate' (+ 1) 1)’
In the expression: take 10 (iterate' (+ 1) 1)
• Relevant bindings include it :: [a] (bound at <interactive>:63:1)
It will work if you pass it a function that takes two parameters though:
λ> take 10 (iterate' (+) 1)
[1,2,3,4,5,6,7,8,9,10]
Any thoughts would be much appreciated!
Andrea
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20161023/b70ca7a9/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 100, Issue 16
******************************************