haskell

What is the best way to split a string by a delimiter functionally?

不问归期 提交于 2020-12-24 08:10:16
问题 I tried to write the program in Haskell that will take a string of integer numbers delimitated by comma, convert it to list of integer numbers and increment each number by 1. For example "1,2,-5,-23,15" -> [2,3,-4,-22,16] Below is the resulting program import Data.List main :: IO () main = do n <- return 1 putStrLn . show . map (+1) . map toInt . splitByDelimiter delimiter $ getList n getList :: Int -> String getList n = foldr (++) [] . intersperse [delimiter] $ replicate n inputStr delimiter

What is the best way to split a string by a delimiter functionally?

大憨熊 提交于 2020-12-24 08:08:22
问题 I tried to write the program in Haskell that will take a string of integer numbers delimitated by comma, convert it to list of integer numbers and increment each number by 1. For example "1,2,-5,-23,15" -> [2,3,-4,-22,16] Below is the resulting program import Data.List main :: IO () main = do n <- return 1 putStrLn . show . map (+1) . map toInt . splitByDelimiter delimiter $ getList n getList :: Int -> String getList n = foldr (++) [] . intersperse [delimiter] $ replicate n inputStr delimiter

我的编程能力从什么时候开始突飞猛进

与世无争的帅哥 提交于 2020-12-18 02:38:05
疫情原因回不去学校,作为一个马上毕业,即将入职腾讯的大四生,分享一下自己的学习历程吧。 本人在大学之前从未接触过编程,最开始的编程学习还是在高考完后,从书店买了本C Primer Plus,然后暑假开始啃,前前后后也就看了几十页。 大一上的时候,来到了华中师范大学,还没有转专业到计算机,一直在自学C语言和看一些计算机入门书籍(编码、计算机科学概论)。 当时也很迷茫,不知道以后道路如何,所以也学了一些杂七杂八的东西(前端 python啥的),所幸的是,当时坚持把C Primer Plus结结实实地精读了一遍,而且几乎练习题都做了,算是比较好的开端。 大一下,转专业到计算机了,开始自学数据结构,算法和C++,部分看完了 数据结构与算法分析,并且把书上的数据结构实现了一遍,记得当时五月份给自己的flag是看完C++ Primer,然后每天上课看,晚饭吃完后也跑去七号楼刷书,最后囫囵吞枣似的看完了大部分。 大一暑假,txr大佬给我说他面试通过了华科的联创团队Unique Studio,而且给我说他们团队都特别厉害,有些人在军训的时候就把C++ Primer给蹲着看完了,当时十分钦佩,幻想也能够进入贵团队。 于是打算在大二上的时候,去报名他们的秋招。所以,那个暑假在学校自学,呆了五十多天。最初,拿起一本APUE,看了一章后感觉看不懂又放下了。 然后,又拿起一本红色封面的算法第四版

Using Z3 with parallelization from SBV

南笙酒味 提交于 2020-12-15 05:43:06
问题 I'd like to use Z3 via SBV using multiple cores. Based on this answer I should be able to do that just by passing parallel.enable=true to the z3 executable on the command line. Since I am using SBV, I need to go through SBV's interface to various SMTLib solvers, so here's what I tried: foo = runSMTWith z3par $ do ... where z3par = z3 { SBV.solver = (SBV.solver z3) { SBV.options = \cfg -> SBV.options (SBV.solver z3) cfg ++ ["parallel.enable=true"] } } However, I am not seeing any signs of Z3

Haskell IO: Reading the whole text file

非 Y 不嫁゛ 提交于 2020-12-15 04:23:19
问题 import System.IO import Text.Printf kodable :: IO() kodable = do printf "Please load a map : " file <- getLine mapFile <- openFile file ReadMode let map = loadMap [] mapFile hClose mapFile printf "Read map successfully! \n" printf "Initial:\n" outputMap map loadMap :: [String] -> FilePath -> [String] loadMap map fp = do finished <- hIsEOF fp new_map <- if not finished then map ++ [hGetLine fp] else return () loadMap new_map fp outputMap :: [String] -> IO() outputMap (x) = printf "%s\n" x

making types: Add data type and evaluator that is of an unmatched type

霸气de小男生 提交于 2020-12-15 01:38:42
问题 I have added | Lit Int and | Add Exp Exp to the data type as seen below, along with the evaluation. However I get an error "Couldn't match expected type ‘Var’ with actual type ‘Int’". data Exp = V Var | B Bool | L Exp | A Exp Exp | Lit Int | Add Exp Exp data Var = VZ |VS Var eval:: Exp -> Var eval (Lit n) = n eval (Add e1 e2) = eval e1 + eval e2 How can I add Int and Add to the data type, along with the evaluation, but maintain the following code as is. Is this possible? data Exp = V Var | B

Checking for all Elements in a Set in Haskell using syntactic sugar

回眸只為那壹抹淺笑 提交于 2020-12-13 05:50:11
问题 I try to remove the Integer duplicates of a List of (String, Int) , where I am guaranteed that there is no String duplicate. Is it possible to evaluate something like this in Haskell: I tried: [(a,b) | (a,b) <- bs, (c,k) <- bs, ((k == b) <= (a == c))] but this does not yet work. Edit: I am well aware, that you can achieve that using more complex syntax. For example by recursively searching the List for each elements duplicates... 回答1: (NB: this is a completely new version of this answer.

Checking for all Elements in a Set in Haskell using syntactic sugar

白昼怎懂夜的黑 提交于 2020-12-13 05:48:03
问题 I try to remove the Integer duplicates of a List of (String, Int) , where I am guaranteed that there is no String duplicate. Is it possible to evaluate something like this in Haskell: I tried: [(a,b) | (a,b) <- bs, (c,k) <- bs, ((k == b) <= (a == c))] but this does not yet work. Edit: I am well aware, that you can achieve that using more complex syntax. For example by recursively searching the List for each elements duplicates... 回答1: (NB: this is a completely new version of this answer.

Is there a function in Haskell that would work like 'uniqueBy'? [closed]

元气小坏坏 提交于 2020-12-12 11:54:07
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 19 days ago . Improve this question I need a function that could be called uniqueBy that would remove all elements in a list of tuples that have the same snd value, without keeping even one of them as nubBy would. For example, uniqueBy [(1,1),(2,1)] should return [] , whereas uniqueBy [(1,1),(1,1),(1,2)] would

Haskell pattern matching char in a string

懵懂的女人 提交于 2020-12-12 11:54:06
问题 I have a question on pattern matching: Is it possible to somehow match a (string ++ [char] ++ anotherstring)? I have tried something like: f (s++";"++r) = s++r (the rhs is trivial, but its just for testing ;)) But this results in a parse error. 回答1: No, it's not possible. Pattern matching deconstructs values according to the constructors they were built with, so you can only use constructor applications in pattern matching to describe which values match the pattern and which don't. For