haskell

Haskell Zip Parse Error

给你一囗甜甜゛ 提交于 2019-12-27 04:48:13
问题 I am trying to use the zip function in Haskell to join two lists together. The lists could be defined and info gathered as follows: priority <- getLine let priorityList = [] priority : priorityList name<- getLine let nameList = [] name : nameList After gathering the info, the expected output would be priorityList = [1,2,3] & nameList = [test1, test2, test3]. However, this is unimportant for the purpose of the question, it can be assumed that the two lists are in the following format:

Python进展路径

这一生的挚爱 提交于 2019-12-26 17:31:38
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 我一直在学习,工作和玩Python一年半。 随着生物学家慢慢转向生物信息学,这种语言一直是我在实验室做出的所有主要贡献的核心。 我或多或少地爱上了Python允许我表达美丽解决方案的方式,以及语言的语义,允许从思想到可行代码的这种自然流动。 我想知道的是你对我在这个论坛或其他论坛中很少见到的一个问题的答案。 对于那些正在改进Python的人来说,这个问题对我来说似乎很重要,但是他想知道他的下一步应该是什么。 让我总结一下我不想先问的问题;) 我不想知道如何快速学习Python 我也不想找到熟悉该语言的最佳方式 最后,我不想知道“一招做到这一切”的方法。 我想知道你的意见,是: 您将向Python熟练人员推荐哪些步骤,从学徒到大师状态(随意停止,无论您的专业知识在哪里),以便一个人不断改进,成为一个更好,更好的Python编码器,一步一步。 SO上的一些人几乎看起来值得为他们的Python实力而敬拜,请赐教:) 我会喜欢的答案(但随意给读者带来惊喜:P)的格式或多或少是这样的: 阅读本文(例如:python教程),注意那种细节 代码很多时间/问题/代码行 然后,阅读本文(例如:这本书或那本书),但这一次,请注意这一点 解决一些现实生活中的问题 然后,继续阅读Y. 一定要掌握这些概念 代码为X时间

log2Sim For a Tiny Language In Haskell

早过忘川 提交于 2019-12-25 18:57:17
问题 I am creating a tiny language in Haskell that can perform simple math equations. The set up looks like this: data E = IntLit Int | BoolLit Bool | Plus E E | Minus E E | Multiplies E E | Exponentiate E E | Equals E E deriving (Eq, Show) I have all of these working, but now I need to define a function for E that preserves the truth value of the expression by taking logs to base 2 if it is possible. If it isn't possible, the program may abort (e.g., with a Non-exhaustive pattern exception).

How to use '(<)' as a variable? [duplicate]

泄露秘密 提交于 2019-12-25 18:33:01
问题 This question already has an answer here : Sorting in haskell with parameter using higher order function (1 answer) Closed 6 years ago . How would I use the greater or less than sign as a variable so that when I enter Main> mySort (<) [1,5,3,6,4,1,3,3,2] or Main> mySort (>) [1,5,3,6,4,1,3,3,2] it will sort the list from highest to lowest or lowest to highest depending on which sign I chose to enter? 回答1: You can just pass (<) in and use it compare each value. A similar function mySort :: (a -

Number of loops in recursion

只谈情不闲聊 提交于 2019-12-25 17:25:54
问题 I would like to count the number of positive integers/elements in the list. This returns the elements with positive values, how can I count the elements? would like to construct something like count(array(...)). I would like to see a version with i++ and foldl . That would be very helpful. countPositivesRec :: [Int] -> [Int] countPositivesRec [] = [] countPositivesRec (x:xs) | x >= 0 = x : tl | otherwise = tl where tl = countPositivesRec xs 回答1: Here's a hint: follow the same recursion scheme

Binary Tree type constructor in Haskell

廉价感情. 提交于 2019-12-25 16:56:00
问题 I'm trying binary tree type constructor which is: data Tree a = Leaf a | Branch a (Tree a) (Tree a) How we prove that not all kinds of binary tree can be represented by this constructor? How we improve this definition to cover all types of binary tree? And how it works? 回答1: Your Tree a has labels of type a at every Branch and every Leaf constructor. So, for example, Branch 'u' (Branch 'n' (Leaf 'i') (Leaf 'p')) (Leaf 'z') looks like this: +-'u'-+ | | +-'n'-+ 'z' | | 'i' 'p' That excludes,

How to write a “which day is it after x days” recursion in Haskell?

三世轮回 提交于 2019-12-25 16:55:39
问题 I am starting to learn Haskell and I'm trying to get this code to work but I cannot understand where is my mistake. I would really appreciate it if you could explain it to me. :) I want to type for example Mon 8 and get Tue. data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun < - [1..7] next :: Day -> Day next Mon = Tue next Tue = Wed next Wed = Thu next Thu = Fri next Fri = Sat next Sat = Sun next Sun = Mon gez n :: (Ord a) => a -> a -> Bool | n > 7 = n - 7 | n <= 7 = n 回答1: You don't have

Creating a Coordinate class in Haskell

ε祈祈猫儿з 提交于 2019-12-25 13:20:46
问题 I'm attempting to add two polymorphic tuples together in a pairwise manner. (The types of the first element in one tuple should be the same as the first in the second and likewise for the second element) Here's my code: module Main where class Coordinate a where createCoordinate :: a getFirst :: (a,b) -> a getSecond :: (a,b) -> b addCoordinates :: (a,b) -> (a,b) -> (a,b) instance Coordinate () where createCoordinate = () getFirst (a,b) = a getSecond (a,b) = b addCoordinates a b = (getFirst a

how to read haskell type signature?

故事扮演 提交于 2019-12-25 12:42:18
问题 So I put this statement on my ghci jkl x f y = f (map (+y) x) And I got this out put back. jkl :: Num b => [b] -> ([b] -> t) -> b -> t But I'm confused when I read it. From my understanding jkl is type num that takes [b], [b] and t, and b. in the end it will output t. is that the right way to read it? 回答1: jkl :: Num b => [b] -> ([b] -> t) -> b -> t Whoever calls jkl has to choose types b and t guarantee that b was chosen among numeric types (the Num b constraint) pass a [b] (list of b ) as

Haskell No instance for (Fractional a0) arising

荒凉一梦 提交于 2019-12-25 09:14:14
问题 I have the following code in Haskell: powmod base 1 m = mod base m powmod base exp m | even exp = mod (pow2 * pow2) m | otherwise = mod (base * powmod base (exp - 1) m) m where pow2 = powmod base (div exp 2) m part1 j n 0 = 0 part1 j n k = (part1 j n (k-1)) + (powmod 16 (n-k) r)/r where r = 8*k+j The code is loaded without any problem on ghci. The problem comes when I try to call the function part1 as: part1 4 10 4 I get: No instance for (Fractional a0) arising from a use of it' The type