Haskell 选择判断语句要写一个语句,写在.hs文件里面,然后GHCi调用,就是算一个数的hailstone 值这是

mintfantasy2022-10-04 11:39:541条回答

Haskell 选择判断语句
要写一个语句,写在.hs文件里面,然后GHCi调用,就是算一个数的hailstone 值
这是我写的hs文,想表达一个算式,“如果n为偶数,得出它的一半值,如果为奇数,算三倍n加1”
hailstone n
| rem n 2 = 0 = n/2
| otherwise = 3n+1
但是GHCi一直提示读取错误,在我用:l xxxx.hs 读取时,
显示第二行的第一个等号有错误
请求指导

已提交,审核后显示!提交回复

共1条回复
草棚棚 共回答了20个问题 | 采纳率95%
问题出再你用了=而不是==
不过这个条件你完全可以直接用even n, 而不必写成rem n 2 == 0
1年前

相关推荐

编程:haskell的简单习题.3题
编程:haskell的简单习题.3题
1.Find the sum of all the multiples of 3 or 5 below 1000.
2.Each new term in the Fibonacci sequence is generated by adding the previous two terms.By starting with 1 and 2,the first 10 terms will be:
1,2,3,5,8,13,21,34,55,89,...
Find the sum of all the even-valued terms in the sequence which do not exceed four million.
3.The prime factors of 13195 are 5,7,13 and 29.
What is the largest prime factor of the number 600851475143
完全初学者,请尽量详细,
成天乐呵呵1年前1
D调的不羁 共回答了15个问题 | 采纳率93.3%
这边的人c都还没搞懂,哪还有时间搞haskell
编程 haskell 使用符号 表示加法,起到加号的作用 例如 Main> 1 2 3
clove2101年前1
泊咕噜 共回答了12个问题 | 采纳率91.7%
instance Show (a -> b) where
show f = ""
instance Eq (a -> b) where
f == g = False
instance (Integral a, Integral b) => Num (a -> b) where
f + g = x -> f x + g x
f - g = x -> f x - g x
f * g = x -> f x * g x
fromInteger n = m -> fromIntegral m + fromIntegral n
abs f = undefined
signum f = undefined
() :: Int -> Int -> Int
() x y = x + y
求问一道Haskell题目,是一道英文题目,我害怕翻译不到位,还是把原题po上来了:Give the type and
求问一道Haskell题目,
是一道英文题目,我害怕翻译不到位,还是把原题po上来了:Give the type and definition for a funtion called 'matches' that take an element and a list where the element is of the same type as the elements in the list.You should use recursive function style and may not use any library functions.Take care to deal with empty lists.For example:matches 3 [0,1,3,2,4] == [3] mathches 'a' ''aspidistra'' == " aa" matches 3 [ ] = [ ]
意思是:请给出一个名为‘matches’的函数的type类型和定义.这个函数有一个element和一个list (这一个element和这个list中的所有elements都是相同的类型).需要使用递归函数的形式并且不得使用任何库函数.小心处理空lists.例如:matches 3 [0,1,3,2,4] == [3] mathches 'a' ''aspidistra'' == " aa" matches 3 [ ] = [ ]
搏乐1年前1
迷路的公孙 共回答了12个问题 | 采纳率100%
matches :: (Eq a) => a -> [a] -> [a]
matches a [] = []
matches a (x:xs) = if a == x then x:matches a xs else matches a xs
类型必须是Eq的实例,不然==无法判断,至少例子上的整数和字符都是的,大部分常见的类型也都是Eq的实例
小弟有几个Haskell问题想问大神
小弟有几个Haskell问题想问大神
第一个是如何将一个列表拆开就像这样:
> :type splits
splits ::[a] -> [([a],[a])]
> splits [1..4]
[([1],[2,3,4]),([1,2],[3,4]),([1,2,3],[4])]
> splits "xyz"
[("x","yz"),("xy","z")]
> splits [True,False]
[([True],[False])]
> length (splits [1..50])
49
第二个是如何找到列表中共有的东西,像这样:
> cpfx ["abc","ab","abcd"]
"ab"
> cpfx ["abc","abcef","a123"]
"a"
> cpfx ["xabc","xabcef","axbc"]
""
第三个是用input建立这样的图形:
> street [(3,2,'x'),(2,6,'y'),(5,4,'z')]
yy
yy
yyzzzzz
yyzzzzz
xxxyyzzzzz
xxxyyzzzzz
最后一个是输入指令进行相应的几种操作:
> editstr [("rep"," ","_")] "just a test" 这是替换
"just_a_test"
> editstr [("xlt","aeiou","AEIOU")] "just a test" 这个是将相应位置的替换,就是把aeiou替换成AEIOU
"jUst A tEst" aeiou和AEIOU都是由用户输入的
> editstr [("len","","")] "testing" 这个是求长度
"7"
> editstr [("rev","","")] "testing" 这个是反向
"gnitset"
> editstr [("x","3","")] "xy" 这个是重复
"xyxyxy"
kzxiaxia1年前1
傅风雪 共回答了18个问题 | 采纳率100%
第一道用了一个比较笨的办法,而且也没有管列表长度小于2的情况
```haskell
splits xs = sp ((length xs)-1) xs
sp 1 xs = [splitAt 1 xs]
sp n xs = (sp (n-1) xs) ++ [splitAt n xs]
```
第二道题copy自[stackoverflow](http://stackoverflow.com/a/21722065/2214113)
```haskell
commonPrefix :: (Eq e) => [e] -> [e] -> [e]
commonPrefix _ [] = []
commonPrefix [] _ = []
commonPrefix (x:xs) (y:ys)
| x == y = x : commonPrefix xs ys
| otherwise = []
cpfx :: (Eq a) => [[a]] -> [a]
cpfx = foldl1 commonPrefix
```
该睡了,后面两个改天有时间再说,我才不告诉你我不会.