How do you debug in codeworld? - haskell

In http://codeworld.info/, you can only draw/print/display data in types of Text and Number (and you need Show to convert Number to Text). So what can I do to let codeworld draw something that is neither a Text or Number?

Chris recently added the ability to import the Haskell base library into CodeWorld, so you can do this:
import Prelude hiding (show)
import HaskellPrelude (show)
showT(x) = fromString(show(x))
main = pictureOf(text(showT([1,2,3,4,5])))

Related

Trying to get each search result to show up in a newline instead having it all cluttered up

How do I get each search result to go from top to bottom instead of all cluttered up left to right?
picture of how it is right now:
how I want it to be:
code:
from youtubesearchpython import VideosSearch
videosSearch = VideosSearch('wonderwall', limit=100)
print(videosSearch.result())
Pretty Print is the tool you seek.
from pprint import pprint
from youtubesearchpython import VideosSearch
videosSearch = VideosSearch('wonderwall', limit=100)
pprint(videoSearch.result())
May need to cast result to JSON. Let me look up the YouTube API.

Capture group names with regex

I'm trying to use the regex package (with TDFA) and the only reference I can find for named captures group is a quasi quoter (cp) and no explanation.
Basically I have regexs in a config files, compiled at runtime with compileRegex, applied to lines and I'm looking to extract a couple of specific captures group out of that, with names I do know at compile time.
So I'm looking for a function that would take the Matches a, a string (the capture group name) and would return I guess a Maybe a, depending on whether that capture group did match or not ? Or even just a way to extract the capture group names from the Match a, that would be enough.
The doc mentions the quasi quoter cp, but no explanation as to how it's supposed to be used, and I'm not even sure I could use if if I knew because I compile my regex at runtime.
Would anyone have examples with named capture groups ?
Thanks
You'll find the API you need in the Text.RE.Replace docs; I think one of the functions with names that start with capture will be what you're looking for. Here's an example:
module Main where
import Data.Text
import Text.RE.Replace
import Text.RE.TDFA.String
needle :: CaptureID
needle = IsCaptureName . CaptureName . pack $ "needle"
main :: IO ()
main = do
re <- compileRegex "-${needle}([a-z]+)-"
let match = "haystack contains the -word- you want" ?=~ re
if matched match
then print $ captureText needle match
else print "(no match)"

Replacing a word by randomly selected synonyms in a string?

I have found the following code in Python that is doing the same work but it only replaces with manually selected synonym.
import nltk
from nltk.corpus import wordnet
synonyms = []
string="i love winter season"
for syn in wordnet.synsets("love"):
for l in syn.lemmas():
synonyms.append(l.name())
print(synonyms)
rep=synonyms[2]
st=string.replace("love",rep, 1)
print(st)
rep=synonyms[2] will be taking any synonym at index 2
What i want is to replace the selected word with its randomly selected synonym?
If I understand your question correctly, what you need is to select a random element from a list. This can be done in python like so:
import random
random.choice (synonyms)
As answered here

Dynamic linking Haskell

I'm looking for a way of dynamic linking.
Outline is:
Lets have an app with many data filters that have all the same outlines (function names, internally used datatypes, some exported datatypes of single class).
It would be great for me to check present .so files, load only those needed, based on command line arguments, and run then.
I dont want to change or recompile app everytime new module is added.
Is something like this possible today?
Tried some hacking with System.Plugins, failed every time. Sometimes one hate strong typechecking.
EDIT
If I wrote something like this directly and gave him type hint on calling makeChunks, it is ok, otherwise nothing
--SharedClass --should create common interface
class (Eq c, Show c) => Chunks c where
makeChunks :: String -> [c]
--Plugin --one concrete implementation
import SharedClass
data MyChunk = SmallChunk Char deriving (Eq)
instance Chunks MyChunk where
makeChunks _ = [SmallChunk 's']
instance Show MyChunk where
show (SmallChunk s) = [s]
--main
import SharedClass
--load plugin somehow
print $ makeChunks "abcd"

How do I convert from unixtime to a date/time in Haskell?

So, the setup is that I have the time, in seconds, since the epoch and I want to turn this into a date I can actually understand.
How do I do this in Haskell? If it is possible, how would I extract days/hours/minutes out of this new encoding?
Data.Time.Clock.POSIX has posixSecondsToUTCTime (you can convert another numeric type to the input expected POSIXTime with realToFrac).
Data.Time.Calendar has a lot of the things you need to extract day, month, etc from the Day that forms a part of UTCTime, the rest of the package has other useful utilities.
Example:
Prelude> import Data.Time.Format.ISO8601
Prelude Data.Time.Format.ISO8601> import Data.Time.Clock.POSIX
Prelude Data.Time.Format.ISO8601 Data.Time.Clock.POSIX> iso8601Show $ posixSecondsToUTCTime $ 100
"1970-01-01T00:01:40Z"
Use time library which is installed by default:
import Data.Time.Clock.POSIX
import Data.Time.Format
import System.Locale
main = print $ formatTime defaultTimeLocale "%c" $ posixSecondsToUTCTime 10
The library has wide range of date manipulation functions (e.g. date subtraction, getting components such as day-month etc). If you want to extract components just for converting them to string you can use formatTime.

Resources