haskell

How is Int actually defined in Haskell?

若如初见. 提交于 2021-01-26 19:26:20
问题 The Learn You A Haskell for Great Good! book by Miran Lipovača says in the chapter Making Our Own Types and Typeclasses that the idea of Haskell's Int type could be represented like this: data Int = -2147483648 | -2147483647 | ... | -1 | 0 | 1 | 2 | ... | 2147483647 Nevertheless, it says that it worked just as demonstrative purposes, but it doesn't say how Int is actually defined. Is Int defined especially by the compiler or can it be definable with plain Haskell code? 回答1: http://hackage

Is it possible to display the results of applying a Haskell type family function?

99封情书 提交于 2021-01-26 09:36:06
问题 For example, if I have these weird types: {-# LANGUAGE TypeFamilies #-} type family WeirdFamily a type instance WeirdFamily () = Int type instance WeirdFamily (a, b) = (a, WeirdFamily b) Can I display (e.g. in GHCi) the result of WeirdFamily (Bool, (Char, ())) by typing something like: :t WeirdFamily (Bool, (Char, ())) into GHCi? 回答1: Use kind! . :kind! WeirdFamily (Bool, (Char, ())) WeirdFamily (Bool, (Char, ())) :: * = (Bool, (Char, Int)) 回答2: So I have figured out an answer. Type this into

Is it possible to display the results of applying a Haskell type family function?

自闭症网瘾萝莉.ら 提交于 2021-01-26 09:35:27
问题 For example, if I have these weird types: {-# LANGUAGE TypeFamilies #-} type family WeirdFamily a type instance WeirdFamily () = Int type instance WeirdFamily (a, b) = (a, WeirdFamily b) Can I display (e.g. in GHCi) the result of WeirdFamily (Bool, (Char, ())) by typing something like: :t WeirdFamily (Bool, (Char, ())) into GHCi? 回答1: Use kind! . :kind! WeirdFamily (Bool, (Char, ())) WeirdFamily (Bool, (Char, ())) :: * = (Bool, (Char, Int)) 回答2: So I have figured out an answer. Type this into

Is it possible to display the results of applying a Haskell type family function?

泪湿孤枕 提交于 2021-01-26 09:33:22
问题 For example, if I have these weird types: {-# LANGUAGE TypeFamilies #-} type family WeirdFamily a type instance WeirdFamily () = Int type instance WeirdFamily (a, b) = (a, WeirdFamily b) Can I display (e.g. in GHCi) the result of WeirdFamily (Bool, (Char, ())) by typing something like: :t WeirdFamily (Bool, (Char, ())) into GHCi? 回答1: Use kind! . :kind! WeirdFamily (Bool, (Char, ())) WeirdFamily (Bool, (Char, ())) :: * = (Bool, (Char, Int)) 回答2: So I have figured out an answer. Type this into

haskell

ぐ巨炮叔叔 提交于 2021-01-26 09:19:37
mac install after install, open file:///Library/Haskell/ghc-8.4.3-x86_64/doc/start.html docs 中文书籍 haskell趣学指南 来源: oschina 链接: https://my.oschina.net/u/916854/blog/2253417

Haskell语言为什么值得你去学习

馋奶兔 提交于 2021-01-26 09:19:13
摘自http://www.vaikan.com/why-haskell-is-worth-learning/ Haskell语言为什么值得你去学习 当我向一些新手推荐学习Haskell语言时,得到的反应通常是:“为什么要学Haskell?”,“这是一种实用的语言吗?”或者“这是一种我可以真正 用 的语言吗?”我的回答是明确的 YES 。 Haskell并不是我工作时的主要语言(我基本上是为嵌入式系统写C程序),但我仍然发现Haskell难以置信的有用。虽然我的工作从来不用Haskell,我仍然认为花时间去学习它是值得的。那么,为什么要学习Haskell语言呢? 1. Haskell语言能很好的帮助运用C语言 事实证明,Haskell语言是一种非常强大的工具,能帮助你写好C程序。我使用Haskell能做的事情经常是我平时认为用常规方法根本不可行的事。 比如说,我的同事想在代码库里找到所有用在 if 条件语句里的叫做 foo 的变量。感谢神奇的 language-c 程序包以及Haskell语言的泛型,我可以写出一个Haskell函数,让它接受一个预处理的C源代码路径作为输入值,输出找到的位置(如果有的话),就像下面: parseAndFindFoos :: FilePath -> IO ( Either ParseError [ Position ] )

[Haskell] 为什么列表操作++很昂贵?

不想你离开。 提交于 2021-01-26 08:43:27
博主是haskell新手。学习haskll的时候遇到了一些问题,在寻求答案的过程中产生了一些思考,可能理解存在偏差,希望各位不吝赐教。 提出问题 《 Learn you a haskell for great good 》里第六章关于函数 foldl (左fold)的部分提到, ++ 操作符比 : 要昂贵很多,所以我们一般用 foldr 来构造一个list 第一次看这段话的时候我并没有深究(实际上我认为这句话根本就有毛病,因为foldr也得用++,原文作者根本没把核心问题指出来),因为haskell用都没用过几次,自然理解不了内在机制。最近刚好遇上了一个用 (++ )的机会,我想把一个Char数组转成String数组,实现如下(不重要,大致就是用foldl将剩余数组的内容塞进累加器数组里,用++来连接,不想看的直接跳过代码吧): chars2String :: [Char] -> [String] chars2Str chars = foldl (\strs c -> strs ++ [[c]]) [] chars -- 当时没想到的更简便实现 chars2String :: [Char] -> [String] chars2Str chars = map (\c -> [c]) chars 突然想到这不正是使用 ++ 的最坏情况吗?所以我花了不少时间去SOF等地方看别人的回答

Hughes' Fibonacci stream

戏子无情 提交于 2021-01-26 08:23:12
问题 I am trying to understand the "Streams as arrows" section in John Hughes' famous "Generalising Arrows to Monads". To be more precise, I am interested in writing down the Fibonacci stream. I tweaked Hughes' definition a bit: data StreamProcessor a b = Get (a -> StreamProcessor a b) | Put b (StreamProcessor a b) | Halt put = Put get = Get First of all, I treat stream processors as lists which may block (waiting for input). That is: Put :: b -> StreamProcessor a b -> StreamProcessor a b matches

What is the suggested way of setting up Haskell on Archlinux?

眉间皱痕 提交于 2021-01-24 10:46:28
问题 Long story short, I'd like some guidance on what's the (best) way to have Haskell work on Archlinux. By work I mean all, in terms of the ghci command line tool, installing packages I don't have - such as vector-space, which this answer to a question of mine refers to -, and any other thing that could be necessary to a Haskell ostinate learner. Archlinux wikipage on Haskell lists three (alternative?) packages for making Haskell work on the system, namely ghc , cabal-install , and stack . I

Convert List of Tuples to List of Lists Haskell

女生的网名这么多〃 提交于 2021-01-22 07:36:43
问题 I have [("m","n"),("p","q"),("r","s")] . How can I convert it to [["m","n"],["p","q"],["r","s"]] ? Can anyone please help me? Thanks. 回答1: Write a single function to convert a pair to a list: pairToList :: (a, a) -> [a] pairToList (x,y) = [x,y] Then you only have to map pairToList : tuplesToList :: [(a,a)] -> [[a]] tuplesToList = map pairToList Or in a single line: map (\(x,y) -> [x,y]) 回答2: Using lens you can do this succinctly for arbitrary length homogenous tuples: import Control.Lens map