haskell

Haskell 笔记 ②

半世苍凉 提交于 2020-01-31 05:07:27
① 如何写一个求阶层函数? fac 0 =1 fac n=n*fac(n-1) 函数自适应匹配参数,可以把特判情况写在前面,注意按 顺序匹配 的, n 这种万能情况写在最前面就完蛋了。同时你也注意到,函数只能 一行写完 ,不能智能识别作用域啊 !! ②灵活的使用你的函数参数 ! 看这个求三元组第三个值的函数,占位符 _ 的神奇使用。 third::(a,b,c)->c third (_,_,c)=c 更神奇的手艹 head 函数 , (x:_) 中占位符模拟出了列表的剩余部分 head’::[a]->a head’ (x:_) =x 若是固定长度的列表,可以这么写 head ’ (x:y:[] ) =x 或者 head [x,y]=x ③ As 模式(填坑中,看不懂) ④利用哨位( Guard) 写出一个优美的递归程序 , | 后面的那个布尔式叫哨位 fac n | n==1 = 1 | n==2 = 2 |otherwise =n*fac(n-1) 尽管函数式语言不允许全局变量存在(全局就变成函数了,函数是确定,不可修改的)但是一个函数的尾部可以用 where 开设一个只赋值一次且作用域只是本函数的变量区,方便值一次计算多次,重复使用,用完销毁,变量区的变量必须对齐了,不然编译错。 fac n | n==1 = x | n==2 = y |otherwise =n*fac(n-1

Haskell tutorial(1) -- Introduction to Haskell

白昼怎懂夜的黑 提交于 2020-01-31 05:00:26
1. What functional programming is and what Haskell is   What is imperative language?     --Give a computer sets of tasks and excute it   What is Functional Programming?     --Don't tell a computer what to do but tell it what a stuff you want to excute is   What is Haskell?     Haskell is a purely functional programming. 2. Properties of Functional programming.   (1). Function has not side effects .     The only thing a function can do is calculate something and return it as a result. One consequence of absence of side effect is Single assignment: A defined   variable can not be redefined.   (2

Haskell function :: [Name] -> [[(Name, Bool)]]

元气小坏坏 提交于 2020-01-30 08:16:50
问题 Given the following: type Name = String envs :: [Name] -> [[(Name , Bool)]] I have to implement 'envs' so that given a list of Names, it returns all possible combinations of Names and booleans My attempt didn't return all possible combinations, this was my code: envs xxs@(x:xs) = [[ (name, value) | name <- xxs , value <- [False, True] ]] the expected results for envs ["P", "Q"] are: [ [ ("P",False) , ("Q",False) ] , [ ("P",False) , ("Q",True) ] , [ ("P",True) , ("Q",False) ] , [ ("P",True) ,

No instance for (Num a) arising from a use of ‘+’ Haskell

烂漫一生 提交于 2020-01-30 08:09:15
问题 I can't figure out why this won't work: final' :: [a] -> a final' lst = foldl(\accum x -> accum - accum + x) 0 lst I always get the error No instance for (Num a) arising from a use of ‘+’ 回答1: The problem has nothing to do with the function itself, but with the signature you attach to it yourself: final' :: [a] -> a Here you basically say that your final' function will work for any a . So I could - if I wanted - add String s together, as well as IO () instances, or anything else. But now

Haskell - Filter Last Element

喜欢而已 提交于 2020-01-30 05:12:35
问题 I want to filter the last element of a list that does not satisfy a property. An example would be smallerOne :: a->Bool smallerOne x = x < 1 The function filterLast should give filterLast smallerOne [1, -2, -3, -4, 5] [1, -2, -3, -4] //Filters the last element that is not smaller than 1 Here is my code (I am a beginner so sorry for the bad attempt) filterLast :: (a -> Bool) -> [a] -> [a] filterLast p [] = [] filterLast p xs | p (last xs) = filterLast p (init xs) : last xs | otherwise = init

Haskell - Filter Last Element

穿精又带淫゛_ 提交于 2020-01-30 05:11:07
问题 I want to filter the last element of a list that does not satisfy a property. An example would be smallerOne :: a->Bool smallerOne x = x < 1 The function filterLast should give filterLast smallerOne [1, -2, -3, -4, 5] [1, -2, -3, -4] //Filters the last element that is not smaller than 1 Here is my code (I am a beginner so sorry for the bad attempt) filterLast :: (a -> Bool) -> [a] -> [a] filterLast p [] = [] filterLast p xs | p (last xs) = filterLast p (init xs) : last xs | otherwise = init

Haskell import declaration

▼魔方 西西 提交于 2020-01-30 04:17:14
问题 I started to read about monad transformers and what puzzles me is Control.Monad.CatchIO 's import declaration which I see in many code examples: import "MonadCatchIO-transformers" Control.Monad.CatchIO (finally) What does this quoted token mean? I took a look at the Haskell 98 Report's section on import declarations, but this didn't help me understand. 回答1: Its a package-qualified import, which is a GHC extension. The string is a package name. See Package-qualified imports, from the ghc docs,

Type comparison in Haskell

99封情书 提交于 2020-01-30 02:26:09
问题 I'm still just learning the basics of Haskell, and I've tried to find an answer to this simple question, so I apologize in advance, because I'm sure it's simple. Given: data Fruit = Fruit| Apple | Orange deriving (Show, Eq) a = Apple How do I check if some a is a Fruit? 回答1: Assuming you really meant type comparison, the simple answer is "you can't". Haskell is statically typed, so the check is done at compile-time, not run-time. So, if you have a function like this: foo :: Fruit -> Bool foo

Is there a compiler-extension for untagged union types in Haskell?

孤人 提交于 2020-01-29 09:30:08
问题 In some languages (#racket/typed, for example), the programmer can specify a union type without discriminating against it, for instance, the type (U Integer String) captures integers and strings, without tagging them (I Integer) (S String) in a data IntOrStringUnion = ... form or anything like that. Is there a way to do the same in Haskell? 回答1: Either is what you're looking for... ish. In Haskell terms, I'd describe what you're looking for as an anonymous sum type . By anonymous, I mean that

Does Haskell have a splat operator like Python and Ruby?

余生长醉 提交于 2020-01-29 04:32:06
问题 In Python and Ruby (and others as well, I'm sure). you can prefix an enumerable with * ("splat") to use it as an argument list. For instance, in Python: >>> def foo(a,b): return a + b >>> foo(1,2) 3 >>> tup = (1,2) >>> foo(*tup) 3 Is there something similar in Haskell? I assume it wouldn't work on lists due to their arbitrary length, but I feel that with tuples it ought to work. Here's an example of what I'd like: ghci> let f a b = a + b ghci> :t f f :: Num a => a -> a -> a ghci> f 1 2 3 ghci