How to import standard Haskell functions like `mod` in code.world?

106 views
Skip to first unread message

Brandon Barker

unread,
Aug 26, 2018, 9:26:08 AM8/26/18
to codeworld-discuss
Hi,

We haven't gotten very far with code.world but it has already been a lot of fun. We hit a snag when trying to use `mod` though:

Variable not in scope: mod :: Number -> Number -> Number

Any suggestions on how to do this would be welcome!

Thanks,

Chris Smith

unread,
Aug 26, 2018, 10:22:06 AM8/26/18
to codeworl...@googlegroups.com
Two answers:

1. If you want to work in standard haskell, you can do that with http://code.world/haskell, which puts you in standard Haskell mode.  This is just like normal Haskell, so mod is already in scope.  You can use a selection of commonly used packages, including base, containers, unordered-containers, text, bytestring... even QuickCheck.  In this mode, to use CodeWorld's graphics library, you'll need to import the CodeWorld module, and the functions are more like standard Haskell.  For example, translated has the type Double -> Double -> Picture -> Picture, instead of (Picture, Double, Double) -> Picture.  The Guide button when you're in standard Haskell mode shows you API documentation for the CodeWorld module.

Here's an example of using CodeWorld mode: https://code.world/haskell#PqQfveu7eXf5m2xrXGH3RaQ

2. If you want to stay in the CodeWorld educational dialect, the function you're looking for is called remainder.

Thanks,
Chris

--
You received this message because you are subscribed to the Google Groups "codeworld-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codeworld-disc...@googlegroups.com.
To post to this group, send email to codeworl...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/codeworld-discuss/9eb60b3d-9a44-43c3-961a-f3b89b5ba1d5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Smith

unread,
Aug 26, 2018, 10:22:40 AM8/26/18
to codeworl...@googlegroups.com
Oops, that should have said "here's an example of using standard Haskell mode"

Brandon Barker

unread,
Aug 30, 2018, 4:25:43 PM8/30/18
to codeworld-discuss
Thanks! It is hard to choose, I can see some advantages for both (maybe I'll do both for now; teaching Code World while learning more Haskell myself :-)). Is there an idiomatic alternative to printed :: Number -> Text in http://code.world/haskell , or some way to import it? It occurs to me that if there were a way to convert from standard Haskell number types (e.g. Int, Double) to Number, maybe some of the existing CodeWorld functions would be easier to use (of course, I'm probably just not looking in the right places).

Chris Smith

unread,
Aug 30, 2018, 4:44:42 PM8/30/18
to codeworl...@googlegroups.com
I'm not sure I understand.  If you use http://code.world/haskell, then the Number type does not even exist.  The alternative is Double, which is used for coordinates in the CodeWorld module.  Double has a Show instance, so show is the idiomatic function that replaces printed.

Brandon Barker

unread,
Aug 30, 2018, 5:14:01 PM8/30/18
to codeworld-discuss
Got it thanks; converted 

mynum   = text(printed(five_doubled))

to 

mynum   = text(pack(show(five_doubled)))

Chris Smith

unread,
Aug 30, 2018, 6:04:53 PM8/30/18
to codeworl...@googlegroups.com
Ah yes, sorry, I forgot about the conversion to Text.  Perhaps it was a mistake to use Text instead of String in the Haskell mode.  Using Text in production code is a best practice, but String is perhaps the better choice for a learning environment.  But the way it is, you do need a Data.Text.pack there as you discovered.

Reply all
Reply to author
Forward
0 new messages