haskell怎么用while
时间: 2023-11-02 12:07:23 浏览: 69
Haskell是一种纯函数式编程语言,它不支持像while这样的循环控制结构。相反,Haskell鼓励使用递归和高阶函数来实现迭代和循环。
例如,以下是一个使用递归来实现while循环的示例:
```haskell
while :: (a -> Bool) -> (a -> a) -> a -> a
while p f x
| p x = while p f (f x)
| otherwise = x
```
这个while函数接受一个条件函数p、一个转换函数f和一个初始值x作为参数。它将重复应用转换函数f到初始值x上,直到条件函数p返回False。返回值是最后一个应用转换函数f的结果。
例如,要计算1到10的和,可以使用以下代码:
```haskell
sum1to10 = while (\x -> x <= 10) (\x -> x + 1) 0
```
这个代码将不断将x加1,直到x大于10为止,并返回1到10的和。
总之,虽然Haskell没有while这样的循环控制结构,但是可以使用递归和高阶函数来实现类似的迭代和循环。
相关问题
用ruby和haskell,分别写一个21点游戏。
以下是 Ruby 的实现:
```
class Card
attr_reader :value
def initialize(value)
@value = value
end
end
class Deck
FACES = %w[2 3 4 5 6 7 8 9 10 J Q K A].freeze
SUITS = %w[♠ ♥ ♦ ♣].freeze
def initialize
@deck = []
SUITS.each do |suit|
FACES.each do |face|
@deck << Card.new("#{face}#{suit}")
end
end
end
def shuffle
@deck.shuffle!
end
def draw
@deck.pop
end
end
class Game
POINTS_MAP = { 'J' => 10, 'Q' => 10, 'K' => 10, 'A' => [1, 11] }.freeze
attr_reader :player_hand, :dealer_hand
def initialize
@deck = Deck.new
@deck.shuffle
@player_hand = []
@dealer_hand = []
2.times do
@player_hand << @deck.draw
@dealer_hand << @deck.draw
end
end
def hit
@player_hand << @deck.draw
end
def play_dealer
while points(@dealer_hand) < 17
@dealer_hand << @deck.draw
end
end
def points(hand)
hand.map(&:value).reduce(0) do |sum, value|
face = value[0..-2]
if POINTS_MAP.key?(face)
if POINTS_MAP[face].is_a?(Array)
sum + POINTS_MAP[face][1]
else
sum + POINTS_MAP[face]
end
else
sum + face.to_i
end
end
end
def winner
player_points = points(@player_hand)
dealer_points = points(@dealer_hand)
if player_points > 21
:dealer
elsif dealer_points > 21
:player
elsif dealer_points > player_points
:dealer
elsif player_points > dealer_points
:player
else
:tie
end
end
def display
puts "Player has: #{@player_hand.map(&:value).join(', ')}. Total: #{points(@player_hand)}"
puts "Dealer has: #{@dealer_hand.first.value}, [hidden]"
end
end
game = Game.new
game.display
while game.points(game.player_hand) < 21
puts "Do you want to hit or stay? (h/s)"
input = gets.chomp
break if input == 's'
game.hit
game.display
end
if game.points(game.player_hand) > 21
puts "You busted!"
else
game.
Describe and contrast overloading in Java and Haskell.
Overloading in Java and Haskell are similar in concept but differ in implementation.
In Java, method overloading allows multiple methods to have the same name but with different parameters. The compiler determines which method to call based on the number and type of arguments passed. This allows for more flexibility and readability in code.
For example:
```
public int add(int x, int y) {
return x + y;
}
public double add(double x, double y) {
return x + y;
}
```
In Haskell, function overloading is achieved through the use of type classes. Type classes define a set of functions that can operate on a particular type. This allows for polymorphic behavior and avoids the need for explicit type annotations.
For example:
```
class Addable a where
add :: a -> a -> a
instance Addable Int where
add x y = x + y
instance Addable Double where
add x y = x + y
```
In this example, the `Addable` type class defines the `add` function for types that are instances of the type class. The `instance` keyword is used to define specific implementations of the function for different types.
Overall, overloading in Java and Haskell serve similar purposes but differ in syntax and implementation. Java uses method overloading with different parameter types, while Haskell uses type classes to provide polymorphic behavior.
阅读全文