haskell

Can I use special characters like tabulations and newlines in Show?

我是研究僧i 提交于 2020-01-16 17:35:07
问题 data Pair = P Int Int instance Show Pair where show (P n1 n2) = (show n1) ++ "\t" ++ (show n2) Result: GHCi, version 7.6.3: http://www.haskell.org/ghc/ :? for help \Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling Main ( labn.hs, interpreted ) Ok, modules loaded: Main. *Main> show (P 5 6) "5\t6" OK for a pair of ints this might be an artificial problem, but my actual use case is

Finding the first duplicate element in a list

只谈情不闲聊 提交于 2020-01-16 13:23:58
问题 I am very new to Haskell. I am trying to write code in Haskell that finds the first duplicate element from the list, and if it does not have the duplicate elements gives the message no duplicates. I know i can do it through nub function but i am trying to do it without it. 回答1: This is one way to do it: import qualified Data.Set as Set dup :: Ord a => [a] -> Maybe a dup xs = dup' xs Set.empty where dup' [] _ = Nothing dup' (x:xs) s = if Set.member x s then Just x else dup' xs (Set.insert x s)

From a list of lists to a list of data records

拈花ヽ惹草 提交于 2020-01-16 09:43:47
问题 I'm parsing a file that looks like this: Good id123 ^ Bad id456 ^ Middle id789 Records are separated by ^\n , and fields within that record are separated simply by newlines . Reading this file and splitting, I end up with a list of lists that looks like this: [["Good","id123",""],["Bad","id456",""],["Middle","id789",""]] However, I fail to turn this into a list of Rec types. Here's my code: {-# LANGUAGE DeriveGeneric, OverloadedStrings #-} import Data.Text as T import Data.Text.IO as T data

Multiplying String in a list, Haskell

时光总嘲笑我的痴心妄想 提交于 2020-01-16 09:01:14
问题 I am trying to multiply strings in a list. For instance, when I have a list like ["hello","World"] and I want to 'multiply' it by 2, I want to end up with ["hello","hello","World","World"] . Here's what I came up with: rep :: Int -> [String] -> [String] rep n [] = [] rep n [x:xs] = replicate n [x] ++ rep n [xs] But it gives me exception: (298,1)-(299,44): Non-exhaustive patterns in function rep I am totally new to this language and I do not have any ideas how to sort this out. Can you help?

Syntax confusion (do block)

删除回忆录丶 提交于 2020-01-16 04:37:19
问题 Sorry for a poor title, feel free to edit. I can't understand what the problem is, so it might be altogether wrong. Below is the code (this is after I've done like a hundred of permutations and different sequences of let-do-if and tabulation, and I'm exhausted): -- The last statement in a 'do' construct must be an expression numberOfGoods :: IO String numberOfGoods = do putStrLn "Enter year (2000-2012):\n" let intYear = readYear in if (intYear < 2000 || intYear > 2012) then error "Year must

Operations with user defined Datatype

杀马特。学长 韩版系。学妹 提交于 2020-01-16 00:39:07
问题 I have a datatype data Expr = ExprNum Double -- constants | ExprVar String -- variables | ExprAdd Expr Expr | ExprSub Expr Expr | ExprNeg Expr -- The unary '-' operator | ExprMul Expr Expr | ExprDiv Expr Expr deriving Show If I have (3* 4 + 5) of datatype Expr it should return 17 and if the expression includes a variable: (3 * x) it should return ExprMul (ExprNum 3) (ExprVar "x") I tried doing it this way: calculate (ExprMul a b) = a * b calculate (ExprAdd a b) = a + b calculate (ExprDiv a b)

Retrieving annotations from the same module

陌路散爱 提交于 2020-01-15 23:01:29
问题 Suppose I define my own annotation type: {-# LANGUAGE DeriveDataTypeable #-} module Def where import Data.Data data MyAnn = MyAnn Int deriving (Show, Typeable, Data) and some Template Haskell function to access it: module TH where import Def import Language.Haskell.TH.Syntax myAnn :: Name -> Q Exp myAnn name = do [MyAnn x] <- reifyAnnotations (AnnLookupName name) lift x I would now like to use it like this: {-# LANGUAGE TemplateHaskell #-} module Client where import Def import TH x :: () x =

jQuery——选择器

扶醉桌前 提交于 2020-01-15 20:10:22
1、层级选择器 选择器是jQuery的核心。一个选择器写出来类似 $('#dom-id') 。 公式 : $(选择器).事件(事件函数) 回顾DOM操作中我们经常使用的代码: // 按ID查找: var a = document.getElementById('dom-id'); // 按tag查找: var divs = document.getElementsByTagName('div'); // 查找<p class="red">: var ps = document.getElementsByTagName('p'); // 过滤出class="red": // TODO: // 查找<table class="green">里面的所有<tr>: var table = ... for (var i=0; i<table.children; i++) { // TODO: 过滤出<tr> } 这些代码实在太繁琐了,并且,在层级关系中,例如,查找 <table class="green"> 里面的所有 <tr> ,一层循环实际上是错的,因为 <table> 的标准写法是: <table> <tbody> <tr>...</tr> <tr>...</tr> </tbody> </table> 很多时候,需要递归查找所有子节点。

Converting a BinaryTree to a Tree?

老子叫甜甜 提交于 2020-01-15 18:51:25
问题 I have 2 classes: One which is a Tree which can have N subtrees, and BinaryTree can have at most 2 subtrees. The classes are defined like so: data Tree a = EmptyTree | Tree a [Tree a] deriving (Show, Ord, Eq) data BinTree a = EmptyBin | Node a (BinTree a) (BinTree a) deriving (Show, Ord, Eq) Is there a way I could convert a BinaryTree into a Tree? Thanks 回答1: Sure. The structure of the Tree allows us to put the two subtrees into the list: convert EmptyBin = EmptyTree convert (Node a l r) =

how to modify the i-th element of a Haskell List?

巧了我就是萌 提交于 2020-01-15 11:50:12
问题 I'm looking for a way to modify the i-th element of haskell List. let says foobar is such a function, then the following works. let xs = ["a","b","c","d"] foobar xs 2 "baba" -- xs = ["a","b","baba","d"] thanks for any reply! 回答1: You can do it with splitAt : Prelude> let xs = ["a","b","c","d"] Prelude> (\(l,_:r)->l++"baba":r) $ splitAt 2 xs ["a","b","baba","d"] 回答2: let xs = ["a","b","c","d"] take 2 xs ++ ["baba"] ++ drop 3 xs 回答3: change n x = zipWith (\k e -> if k == n then x else e) [0..]